Skip to content

Commit de55117

Browse files
committed
Merge branch 'dev'
2 parents 2848234 + 551e0e0 commit de55117

File tree

2 files changed

+115
-39
lines changed

2 files changed

+115
-39
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ This program is a simple interface for commiting to git similar to https://githu
33

44
This program can be used on its own or as a git hook.
55

6-
# Installation | Usage
7-
Usage:
6+
![Preview](https://i.imgur.com/apKuO1k.png)
7+
![Preview](https://i.imgur.com/3rdpRS7.png)
8+
9+
# Installation
10+
To install, simply clone this repo.
11+
12+
`git clone https://github.com/Linus045/git_commit_helper.git git_commit_helper`
13+
14+
# Usage:
815
```
916
bash linus_commit_helper.sh --<install|commit> <project_path>
1017
bash linus_commit_helper.sh --<uninstall|help>
@@ -29,3 +36,23 @@ To install it as a git hook use `--install` e.g.
2936
`bash linus_commit_helper.sh --install /home/me/my_cool_project`
3037

3138
It will now run on every `git commit` call automatically and prepare the commit message.
39+
40+
# Updating
41+
To update this script, simply pull the latest version from `main`
42+
43+
# Notice for usage in git hooks
44+
The hook itself links to the script using the absolute path (after using --install)
45+
46+
Example:
47+
48+
After using `bash linus_commit_helper.sh --install /home/linus/dev/github/git_commit_helper`
49+
50+
`/home/linus/dev/github/git_commit_helper/.git/hooks/prepare-commit-msg` now contains this call:
51+
```
52+
bash /home/linus/dev/github/git_commit_helper/linus_commit_helper.sh --hook $1 $2 $3
53+
```
54+
55+
This makes it easier to update the script since you can just pull the latest changes and it reflects immediately for all repos that use the hook.
56+
If you only need this hook for one git repository and don't care about updating,
57+
you could also move the file `linus_commit_helper.sh` into the `<Git Repo>/.git/hooks/` directory of the respective git repository
58+
and adjust the path in the `<Git Repo>/.git/hooks/prepare-commit-msg` file

linus_commit_helper.sh

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
91102
request_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

135159
request_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+
140165
request_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
235283
else
236284
instructions_usage
237285
fi
238-
286+
287+

0 commit comments

Comments
 (0)