|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright (c) 2024 Project CHIP Authors |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +ROOT_DIR=$(realpath $(dirname "$0")/../..) |
| 19 | +cd $ROOT_DIR |
| 20 | + |
| 21 | +get_repo_and_branch_info() { |
| 22 | + # Input validation |
| 23 | + if [ -z "$1" ]; then |
| 24 | + echo "Please provide a path." |
| 25 | + return 1 |
| 26 | + fi |
| 27 | + |
| 28 | + path="$1" |
| 29 | + repo_friendly_name="Matter SDK" |
| 30 | + |
| 31 | + if [ "$path" != "." ]; then |
| 32 | + title_case_path=$(echo "$path" | awk '{ for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2)); }1') |
| 33 | + repo_friendly_name=$title_case_path |
| 34 | + fi |
| 35 | + |
| 36 | + # Check if the directory exists |
| 37 | + if [ ! -d "$path" ]; then |
| 38 | + echo "Directory '$path' does not exist." |
| 39 | + return 1 |
| 40 | + fi |
| 41 | + |
| 42 | + cd $path |
| 43 | + |
| 44 | + # Get the URL of the remote origin |
| 45 | + remote_url=$(git config --get remote.origin.url) |
| 46 | + |
| 47 | + if [ -n "$remote_url" ]; then |
| 48 | + # Extract the repository name from the URL |
| 49 | + repo_name=$(basename -s .git "$remote_url") |
| 50 | + |
| 51 | + # Calculate the necessary padding to align the end pipe |
| 52 | + total_length=95 # Adjust this based on your frame width |
| 53 | + text_length=${#repo_friendly_name}+${#repo_name}+4 # 4 for the ": " and two spaces around the repo name |
| 54 | + padding_length=$((total_length - text_length)) |
| 55 | + |
| 56 | + echo '+-----------------------------------------------------------------------------------------------+' |
| 57 | + printf "| %s: %s%*s|\n" "$repo_friendly_name" "$repo_name" $padding_length "" |
| 58 | + echo '+-----------------------------------------------------------------------------------------------+' |
| 59 | + else |
| 60 | + # Print error message if there is no remote URL |
| 61 | + echo "Not a Git repository or no remote set" |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + |
| 65 | + # Get the current branch and its tracking branch |
| 66 | + git_status=$(git status) |
| 67 | + tracking_branch_info=$(echo "$git_status" | grep "Your branch is up to date with") |
| 68 | + |
| 69 | + # Extract the fork owner and branch from the tracking branch info |
| 70 | + if [[ $tracking_branch_info =~ Your\ branch\ is\ up\ to\ date\ with\ \'([^\']+)\' ]]; then |
| 71 | + fork_owner_and_branch="${BASH_REMATCH[1]}" |
| 72 | + else |
| 73 | + fork_owner_and_branch="Not set or not a tracking branch" |
| 74 | + fi |
| 75 | + |
| 76 | + # Get the commit SHA of the current HEAD |
| 77 | + commit_sha=$(git rev-parse HEAD) |
| 78 | + echo "Commit SHA: $commit_sha" |
| 79 | + |
| 80 | + # Get the commit message of the current HEAD |
| 81 | + commit_message=$(git log -1 --pretty=format:"%B") |
| 82 | + trimmed_commit_message=$(trim_commit_message "$commit_message") |
| 83 | + echo "Commit Message: $trimmed_commit_message" |
| 84 | + |
| 85 | + # Get the commit author of the current HEAD |
| 86 | + commit_author=$(git log -1 --pretty=format:"%an") |
| 87 | + echo "Commit Author: $commit_author" |
| 88 | + |
| 89 | + # Get the commit date and time of the current HEAD including timezone |
| 90 | + commit_datetime=$(git log -1 --pretty=format:"%cd" --date=format:"%Y-%m-%d %H:%M:%S %z") |
| 91 | + echo "Commit Date: $commit_datetime" |
| 92 | + |
| 93 | + # Attempt to find branches that contain this commit |
| 94 | + branches=$(git branch --contains $commit_sha | sed 's/^/ /') |
| 95 | + |
| 96 | + if [ -n "$branches" ]; then |
| 97 | + echo "Contained in branches:" |
| 98 | + echo "$branches" |
| 99 | + else |
| 100 | + echo "This commit is not on any known branch." |
| 101 | + fi |
| 102 | + |
| 103 | + echo " Tracking: $fork_owner_and_branch" |
| 104 | + |
| 105 | + echo |
| 106 | + |
| 107 | + # Navigate back to the original directory |
| 108 | + cd $ROOT_DIR |
| 109 | +} |
| 110 | + |
| 111 | +trim_commit_message() { |
| 112 | + local commit_message="$1" |
| 113 | + |
| 114 | + # Check if the commit message contains a newline character |
| 115 | + if [[ "$commit_message" == *$'\n'* ]]; then |
| 116 | + # Extract the first line of the commit message |
| 117 | + local first_line=${commit_message%%$'\n'*} |
| 118 | + else |
| 119 | + # If there's no newline, use the entire commit message |
| 120 | + local first_line="$commit_message" |
| 121 | + fi |
| 122 | + |
| 123 | + # Trim leading and trailing whitespace from the first line and echo it |
| 124 | + echo "$first_line" | sed 's/^[ \t]*//;s/[ \t]*$//' |
| 125 | +} |
| 126 | + |
| 127 | +# Print SDK root git status |
| 128 | +get_repo_and_branch_info "." |
| 129 | + |
| 130 | +# Handle arguments |
| 131 | +case "$1" in |
| 132 | +--git-sub) |
| 133 | + # Initialize an array to hold the directories |
| 134 | + declare -a repo_dirs |
| 135 | + |
| 136 | + cd $ROOT_DIR |
| 137 | + |
| 138 | + # Find directories containing a .github folder and store them in the array, excluding the current directory |
| 139 | + while IFS= read -r dir; do |
| 140 | + # Check if the directory is not the current directory |
| 141 | + if [[ "$dir" != "." ]]; then |
| 142 | + repo_dirs+=("$dir") |
| 143 | + fi |
| 144 | + done < <(find . -type d -name .github | awk -F'/[^/]*$' '{print $1}') |
| 145 | + |
| 146 | + # Iterate through the directories and call the function for each |
| 147 | + for dir in "${repo_dirs[@]}"; do |
| 148 | + get_repo_and_branch_info "$dir" |
| 149 | + done |
| 150 | + ;; |
| 151 | +*) ;; |
| 152 | +esac |
0 commit comments