-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitcs
executable file
·99 lines (88 loc) · 3.07 KB
/
gitcs
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
#!/bin/bash
# Function to check if the current directory is a git repository
is_git_repository() {
git rev-parse --is-inside-work-tree > /dev/null 2>&1
return $?
}
# Function to display banner
display_banner() {
echo "===================================="
echo "Git Commit Selector Script (v.1.0.1)"
echo "===================================="
echo "Author : wenkhairu"
echo "Email : [email protected]"
echo
}
# Check if correct number of arguments is passed
if [ "$#" -ne 2 ]; then
display_banner
echo "Usage: $0 <branch> <release_version>"
exit 1
fi
# Check if current directory is a git repository
if ! is_git_repository; then
echo "Error: The current directory is not a Git repository."
exit 1
fi
# Assign branch and release_version to variables
branch="$1"
release_version="$2"
# Get the current branch
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: Failed to get the current branch"
exit 1
fi
# Check out to the desired branch if not already on it
if [ "$current_branch" != "$branch" ]; then
echo "Current branch is $current_branch. Switching to $branch..."
if ! git checkout "$branch"; then
echo "Failed to checkout branch $branch. Aborting."
exit 1
fi
echo "Switched to branch $branch"
else
echo "Already on branch $branch"
fi
# Search for commits
echo "Searching for '$release_version' in branch $branch..."
commits=$(git log --pretty=format:"%h - %ad: %s" --date=short | grep "$release_version")
if [ $? -ne 0 ]; then
echo "Error: Failed to get the git log"
exit 1
fi
IFS=$'\n' read -d '' -ra filtered_commits <<< "$commits"
if [ ${#filtered_commits[@]} -eq 0 ]; then
echo "No commits found matching '$release_version'"
exit 0
else
echo "Found the following commits:"
for i in "${!filtered_commits[@]}"; do
echo "$((i+1)). ${filtered_commits[$i]}"
done
echo "$(( ${#filtered_commits[@]} + 1 )). Exit"
# Prompt the user to select a commit or exit
while true; do
read -p "Choose a commit to checkout (1-${#filtered_commits[@]} or $((${#filtered_commits[@]} + 1)) to Exit): " choice
if [[ "$choice" =~ ^[0-9]+$ ]]; then
if [ "$choice" -ge 1 ] && [ "$choice" -le "${#filtered_commits[@]}" ]; then
chosen_commit=$(echo "${filtered_commits[$((choice - 1))]}" | cut -d ' ' -f 1)
echo "Checking out to commit $chosen_commit..."
if git checkout "$chosen_commit"; then
echo "Successfully checked out to commit $chosen_commit"
else
echo "Error: Failed to checkout to commit $chosen_commit"
exit 1
fi
break
elif [ "$choice" -eq "$(( ${#filtered_commits[@]} + 1 ))" ]; then
echo "Exiting."
exit 0
else
echo "Invalid choice. Please choose a number between 1 and ${#filtered_commits[@]} or ${#filtered_commits[@]} + 1 to Exit."
fi
else
echo "Invalid input. Please enter a number."
fi
done
fi