-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgu
executable file
·89 lines (76 loc) · 2.64 KB
/
gu
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
#!/bin/zsh
print_usage() {
echo 'Usage: gu [-flags] <remote-branch-name> "commit message"'
echo ' [g]it-[u]p - add all files, commit with a message and push the current branch to a <remote-branch-name>.'
echo ' "commit message" - message assigned to the commit'
echo '\n [-flags]:'
echo ' -h Prints out this message'
echo ' -p Uses [p]revious commit message; "commit message" argument is skipped'
echo ' -n <branch> Specify a different <remote-branch-name>. Defaults to the same branch if not provided.'
echo ' -d Debugging mode ON'
echo " -A Commit only staged files, don't Add All files"
}
get_current_branch() {
git branch --show-current
}
get_upstream_branch() {
git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null | sed 's/[^\/]*\///'
}
message=
remoteBranchName=
useLastCommit='false'
debuggingMode='OFF'
NoAddAllFiles='false'
flags=''
while getopts ':hpndA:' flag; do
case "${flag}" in
h) print_usage; return 0 ;;
p) useLastCommit='true' ;;
n) remoteBranchName="${OPTARG}" ;; # Capture the argument for -n
d) debuggingMode='ON' ;;
A) NoAddAllFiles='true' ;;
*) printf "Invalid option: -${OPTARG}.\n\n" >&2; print_usage; return 1 ;;
esac
flags+="${flag} " # Capture the flags for debugging
done
shift $((OPTIND - 1))
# Default to current branch if no remote branch name is specified
if [[ -z $remoteBranchName ]]; then
remoteBranchName=$(get_current_branch)
fi
# Check for commit message
if [[ $useLastCommit == 'false' && -z $1 ]]; then
echo "Error: must provide a commit message unless using -p flag." >&2
print_usage
return 1
fi
# Assign commit message if provided
if [[ $useLastCommit == 'false' ]]; then
message=$1
fi
# Handle file adding
if [[ $NoAddAllFiles == 'true' ]]; then
echo 'Uploading files:'
printf "\033[32m%s\033[0m\n" "$(git diff --name-only --cached)"
else
git add . -A -v
fi
# Commit if not using the last commit
if [[ $useLastCommit == 'false' ]]; then
git commit -m "$message"
fi
# Debugging output
if [[ $debuggingMode == 'ON' ]]; then
echo '\n\n######################### Debugging mode ON #########################\n\n'
echo "WARNING: Debugging mode disables 'git push' command"
echo "Parsed flags: ${flags}"
echo "Local active branch name parsed as: $(get_current_branch)"
echo "Remote branch name is: ${remoteBranchName}"
echo "Don't add all files flag: ${NoAddAllFiles}"
echo "Message: ${message}"
echo "Use last commit: ${useLastCommit}"
echo '\n\n######################### end of debugging messages #########################\n\n'
return
fi
# Push to the specified remote branch
git push -u origin "$remoteBranchName"