@@ -88,59 +88,84 @@ is_git_repo() {
8888 fi
8989}
9090
91+ load_current_commit_msg () {
92+ COMMIT_MSG_FILE=$1
93+ COMMIT_SOURCE=$2
94+ SHA1=$3
95+
96+ if [[ -f $COMMIT_MSG_FILE ]]; then
97+ commit_subject=$( head -n 1 $COMMIT_MSG_FILE )
98+ commit_body=$( tail -n +2 $COMMIT_MSG_FILE )
99+ fi
100+ }
101+
91102request_commit_subject () {
92103 # get prefix for commit message
93104 commit_prefix=" "
105+ scope=" "
94106
95- selected=$( gum choose " [feat] - A new feature" " [fix] - A bug fix" \
96- " [docs] - Documentation only changes" \
97- " [style] - Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)" \
98- " [refactor] - A code change that neither fixes a bug nor adds a feature" \
99- " [test] - Adding missing tests or correcting existing tests" \
100- " [perf] - A code change that improves performance" )
101-
102- if [[ -z $selected ]]; then
103- echo " No selection made, aborting..."
104- exit 2
105- fi
106107
107- # text in square brackets will be extracted and used as prefix for the commit subject text e.g.
108- # If you select "[feat] - A new feature"
109- # feat: <Some text here>
110- if [[ $selected =~ \[ (.+)\] ]]; then
111- commit_prefix=${BASH_REMATCH[1]}
112- else
113- echo " Error while parsing string: $selected "
114- exit 1
115- fi
108+ if [[ -z $commit_subject ]]; then
109+ TYPE_NO_PREFIX=" NO PREFIX"
110+ selected=$( gum choose " [feat] - A new feature" " [fix] - A bug fix" \
111+ " [docs] - Documentation only changes" \
112+ " [style] - Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)" \
113+ " [refactor] - A code change that neither fixes a bug nor adds a feature" \
114+ " [test] - Adding missing tests or correcting existing tests" \
115+ " [perf] - A code change that improves performance" \
116+ " [$TYPE_NO_PREFIX ] - Use no prefix" )
117+
118+ if [[ -z $selected ]]; then
119+ echo " No selection made, aborting..."
120+ exit 2
121+ fi
122+
123+ # text in square brackets will be extracted and used as prefix for the commit subject text e.g.
124+ # If you select "[feat] - A new feature"
125+ # feat: <Some text here>
126+ if [[ $selected =~ \[ (.+)\] ]]; then
127+ commit_prefix=${BASH_REMATCH[1]}
128+ if [[ $commit_prefix = " $TYPE_NO_PREFIX " ]]; then
129+ commit_prefix=" "
130+ fi
131+ else
132+ echo " Error while parsing string: $selected "
133+ exit 1
134+ fi
135+
136+ if [[ -n $commit_prefix ]]; then
137+ # get optional scope for change
138+ scope=$( gum input --char-limit=40 --width=40 --prompt=" Scope: " --placeholder=" Enter optional scope here (Issue number/component name/etc.)" )
139+ fi
116140
141+ # combine prefix, scope and subject into one string
142+ if [[ -z $scope ]]; then
143+ if [[ -n $commit_prefix ]]; then
144+ commit_subject=" $commit_prefix :"
145+ fi
146+ else
147+ commit_subject=" $commit_prefix ($scope ):"
148+ fi
149+ fi
117150
118- # get optional scope for change
119- scope=$( gum input --char-limit=40 --width=40 --prompt=" Scope: " --placeholder=" Enter optional scope here (Issue number/component name/etc.)" )
151+ commit_subject=$( gum input --char-limit=80 --width=100 --prompt=" Summary: " --value=" $commit_subject " --placeholder " Enter a short summary here (80 chars max)" )
120152
121- commit_subject=$( gum input --char-limit=50 --width=100 --prompt=" $commit_prefix : " --placeholder " Enter a short summary here (50 chars max)" )
122153 if [[ -z $commit_subject ]]; then
123154 echo " No summary provided, aborting..."
124155 exit 2
125156 fi
126-
127- # combine prefix, scope and subject into one string
128- if [[ -z $scope ]]; then
129- commit_subject=" $commit_prefix : $commit_subject "
130- else
131- commit_subject=" $commit_prefix ($scope ): $commit_subject "
132- fi
133157}
134158
135159request_commit_body () {
136160 echo $commit_subject
137- commit_body=$( gum write --width=100 --show-line-numbers --height=10 --placeholder=" Enter a longer description here (Ctrl+D or Esc to finish|Ctrl+C to cancel)" )
161+ commit_body=$( gum write --width=100 --show-line-numbers --height=10 --value= " $commit_body " -- placeholder=" Enter a longer description here (Ctrl+D or Esc to finish|Ctrl+C to cancel)" )
138162}
139163
164+
140165request_commit_message () {
141166 gum_installed_check
142167 print_info_box
143-
168+
144169 request_commit_subject
145170 request_commit_body
146171
@@ -198,11 +223,10 @@ elif [[ $1 == "--install" ]]; then
198223 echo " Created $repo_path /.git/hooks/prepare-commit-msg file"
199224 fi
200225
201- echo " bash .git/hooks/ linus_commit_helper.sh --hook \$ 1 \$ 2 \$ 3" >> " $repo_path /.git/hooks/prepare-commit-msg"
202- echo " Added hook call in $repo_path /.git/hooks/prepare-commit-msg file"
226+ echo " bash $( realpath " ./ linus_commit_helper.sh" ) --hook \$ 1 \$ 2 \$ 3" >> " $repo_path /.git/hooks/prepare-commit-msg"
227+ echo " Added hook call to $( realpath " ./linus_commit_helper.sh " ) in $repo_path /.git/hooks/prepare-commit-msg file"
203228
204- cp ./linus_commit_helper.sh $repo_path /.git/hooks/linus_commit_helper.sh
205- echo " Installed linus commit helper in $repo_path "
229+ echo " Installed hook in $repo_path successfully."
206230 else
207231 echo " Error: Directory not found: $repo_path "
208232 exit 1
@@ -229,10 +253,35 @@ elif [[ $1 == "--hook" ]]; then
229253 COMMIT_MSG_FILE=$2
230254 COMMIT_SOURCE=$3
231255 SHA1=$4
256+
232257
258+
259+ if [[ -f $COMMIT_MSG_FILE ]]; then
260+ # removes all lines starting with a # sign
261+ existing_commit_message=$( sed " /^#.*$/d" $COMMIT_MSG_FILE )
262+ if [[ -n $existing_commit_message ]]; then
263+ gum style \
264+ --foreground 101 --border-foreground 50 --border normal \
265+ --align left --width 80 --margin " 0 0" --padding " 0 0" \
266+ " $existing_commit_message "
267+
268+ gum confirm --affirmative=" Edit commit" --negative " Continue" " Do you want to edit the commit message?"
269+ confirm=$?
270+
271+ if [[ $confirm == 1 ]]; then
272+ echo " Continuing without editing commit..."
273+ exit 0
274+ fi
275+
276+ load_current_commit_msg $COMMIT_MSG_FILE $COMMIT_SOURCE $SHA1
277+ fi
278+ fi
279+
280+
233281 RUN_VIA_HOOK=1
234282 request_commit_message
235283else
236284 instructions_usage
237285fi
238-
286+
287+
0 commit comments