-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc.sh
144 lines (120 loc) · 3.9 KB
/
gc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# File Name: gc.sh
# Description: This script executes a specified Git command in a designated Git repository.
# It can take the repository path and Git command as command-line arguments
# or read them from a specified file.
# Author: Ajay Singh
# Version: 1.1
# Date: 20-10-2024
# Color Codes
COLOR_INFO="\033[1;32m" # Green for info
COLOR_ERROR="\033[1;31m" # Red for errors
COLOR_MAIN="\033[1;35m" # Dark pink for repository path and branch name
COLOR_RESET="\033[0m" # Reset color
# Constants
DETAILS_FILE="repo_details.txt"
# Display help message
show_help() {
echo "Usage: $0 [--git <git_command>] [-p repo_path] [-h]"
echo " --git <git_command> Run a Git command in the repository."
echo " -p <repo_path> Specify Git repository path (overrides $DETAILS_FILE)"
echo " -h Show this help message"
exit 0
}
# Log informational messages in green
log_info() {
echo -e "${COLOR_INFO}INFO:${COLOR_RESET} $1"
}
# Log error messages in red and exit
log_error() {
echo -e "${COLOR_ERROR}ERROR:${COLOR_RESET} $1" >&2
exit 1
}
# Log repository path in dark pink
log_repo_info() {
echo -e "${COLOR_MAIN}REPO PATH:${COLOR_RESET} $1"
}
# Log branch name in dark pink
log_branch_info() {
echo -e "${COLOR_MAIN}BRANCH NAME:${COLOR_RESET} $1"
}
# Check if Git is installed
check_git_installed() {
command -v git &>/dev/null || log_error "Git is not installed. Install Git and try again."
}
# Read repository path from repo_details.txt
read_details_file() {
if [[ ! -f "$DETAILS_FILE" ]]; then
log_error "$DETAILS_FILE not found! Provide details via -p or create $DETAILS_FILE."
fi
repo_path_file=$(awk 'NR==1 {print $0}' "$DETAILS_FILE" | xargs)
# Check if the repo path is empty
if [[ -z "$repo_path_file" ]]; then
log_error "$DETAILS_FILE is empty! Provide a valid repository path."
fi
}
# Convert Windows-style path to Unix-style path for Git Bash
convert_path() {
local path="$1"
[[ "$path" =~ ^([A-Z]): ]] && path="/${BASH_REMATCH[1],,}/$(echo "$path" | sed 's|\\|/|g' | sed 's|^[A-Z]:||')"
echo "$path"
}
# Main logic
main() {
local repo_path=""
local git_command=()
# Parse command-line options
while [[ "$#" -gt 0 ]]; do
case "$1" in
--git)
shift
# Capture the git command
git_command+=("$1")
shift
;;
-p)
shift
repo_path=$(convert_path "$1")
shift
;;
-h)
show_help
;;
*)
log_error "Invalid option: $1"
;;
esac
done
# Read details from the file if not provided via command-line options
read_details_file
repo_path="${repo_path:-$repo_path_file}"
# Ensure the repo path is set
if [[ -z "$repo_path" ]]; then
log_error "Repository path is missing."
fi
# Check if the repo path exists
if [[ ! -d "$repo_path" ]]; then
log_error "Repository path not found: $repo_path"
fi
log_info "Navigating to repository"
log_repo_info "$repo_path"
# Change to the repository directory
(
cd "$repo_path" || log_error "Failed to access directory: $repo_path"
# Determine the current branch name
branch_name=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || log_error "Failed to get current branch name."
log_branch_info "$branch_name"
# Run the Git commands
if [[ ${#git_command[@]} -eq 0 ]]; then
git_command=("status")
fi
for cmd in "${git_command[@]}"; do
log_info "Running command: git $cmd"
git "$cmd" || log_error "Command failed: git $cmd"
done
)
}
# Check if Git is installed
check_git_installed
# Execute main function
main "$@"