-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit
162 lines (142 loc) · 5.33 KB
/
commit
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# Title : commit
# Date : 16.05.2019
# Author : WessCoby <cobygiven@gmail.com>
# Version : 1.0.2
# Description : A simple bash script to aid in staging and committing files to git
# Options : None
### Git Prefix specifying the current working directory
prefix="$PWD"
### Text Style and Color formatting
black_on_red_bold="\033[1;30;41m"
white_on_red_bold="\033[1;37;41m"
red_on_white="\033[31;47m"
red_on_blue="\033[31;44m"
red_bold="\033[1;31m"
blue_bold="\033[1;34m"
none="\033[0m"
#===========================================================================================
#======================== HELPER FUNCTIONS ===========================
#===========================================================================================
### prompt for boolean response
confirm() {
shopt -s nocasematch # turn on case insensitive matching
if [[ "$1" =~ ^y(es)?$ ]];
then
echo true;
elif [[ "$1" =~ ^no?$ ]];
then
echo false;
else
echo none;
fi
shopt -u nocasematch # turn off case insensitive matching
}
#===========================================================================================
#======================== MAIN FUNCTIONS =============================
#===========================================================================================
### check and initialize (on confirmation) the current working directory as a repo
initRepo() {
read -r -p "Initialize Git Repository? [ Y/N ]: " response;
if [[ $(confirm "$response") == "true" ]]; then
git -C ${prefix} init # Initialize Repository
handleArgs $@
elif [[ $(confirm "$response") == "false" ]]; then
printf "\n${blue_bold}Repository is NOT Initialized.\nFiles NOT Committed.\nGoodbye!!!${none}\n\n";
exit 1;
else
errlog "$Invalid Response. Try Again!!!";
initRepo $@
fi
}
### working with arguments provided at the call of the commit script
handleArgs() {
# if no argument is passed
if [[ $# == 0 ]]; then
echo "";
# prompt user to confirm staging and committing everything in the directory
read -r -p "Commit Everything? [Y/N]: " response
echo "";
if [[ $(confirm "$response") == "true" ]]; then
git -C ${prefix} add . && git -C ${prefix} status; # Stage all files and check status
commit;
elif [[ $(confirm "$response") == "false" ]]; then
# prompt user to add files to be staged and committed
read -r -p "Add Files [ Enter names of files and folders ]: " selected_files;
handleArgs ${selected_files} # invoke handleArgs again with user input as arguments
else
echo -e "${red_bold}Invalid Response. Try Again!!!${none}";
handleArgs $@ # invoke handleArgs again with the initial arguments provided
fi
# if one argument is passed
elif [[ $# == 1 ]]; then
# if argument passed is "."
if [[ $1 == "." ]]; then
git -C ${prefix} add . && git -C ${prefix} status; # Stage all files and check status
commit;
else
stageFiles $1 # stage the single specified file
fi
# if more than one argument is passed
elif [[ $# -gt 1 ]]; then
stageFiles $@;
fi
}
### handle staging
stageFiles() {
declare -a existing_files # indexed array for existing files
declare -a nonexistent_files # indexed array for nonexistent files
# If only one argument
if [[ $# == 1 ]]; then
if [[ $(fe "$1") == "true" ]]; then
git -C ${prefix} add $1 && git -C ${prefix} status; # Stage file and check status
commit;
else
errlog "'$1' NOT found in directory. Check Path or Filename";
handleArgs
fi
else
# More than one argument
for file in "$@"
do
if [[ $(fe "$file") == "true" ]]; then
existing_files+=("$file"); # Add file to array of existent files.
git -C ${prefix} add "${file}"; # Stage file
else
nonexistent_files+=("$file"); # Add file to array of nonexistent files.
fi
done
if [[ "${#nonexistent_files[@]}" -ne 0 ]]; then
errlog "[ ${nonexistent_files[*]} ] NOT found in this directory. Check Path(s) or Filename(s)";
fi
if [[ "${#existing_files[@]}" -ge 1 ]]; then
git -C ${prefix} status; # Check status
commit
else
git -C ${prefix} status; # Check status
handleArgs
fi
fi
}
### Commit files
commit() {
## Prompt for commit message
read -r -p "Commit message: " message
## Track file deletes
git -C ${prefix} add -u
# Committing
git -C ${prefix} commit -m "$message"
}
#===========================================================================================
#============================== START ================================
#===========================================================================================
### Check if current directory is a git repository
if [[ $(fe ".git") == "true" ]];
then
handleArgs $@
else
errlog "This Directory is not a Git repository";
initRepo $@
fi
### Done
exit 0