-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-dcommit
More file actions
executable file
·173 lines (155 loc) · 4.32 KB
/
git-dcommit
File metadata and controls
executable file
·173 lines (155 loc) · 4.32 KB
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
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
#
# Requirements:
# - git
#
# Author: James Cherti
# URL: https://github.com/jamescherti/bash-stdops
#
# Description:
# ------------
# Script to automate common Git commit tasks:
# - Automatically add untracked files (prompted)
# - Display git diff to the user before committing
# - Commit changes to the Git repository
# - Optionally reuse the previous Git commit message if available
#
# Usage: ./script_name.sh
# Run this script from within a Git repository to automate adding,
# reviewing, and committing changes.
#
# License:
# --------
# Copyright (C) 2012-2026 James Cherti
#
# Distributed under terms of the GNU General Public License version 3.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
set -euf -o pipefail
# shellcheck disable=SC2317
error_handler() {
local errno="$?"
echo "Error: ${BASH_SOURCE[1]}:${BASH_LINENO[0]}" \
"(${BASH_COMMAND} exited with status $errno)" >&2
exit "${errno}"
}
git_add() {
if [[ "$(git ls-files --others --exclude-standard | wc -l)" -gt 0 ]]; then
echo "Git repository: $GIT_REPO"
echo
echo "Untracked files detected:"
echo "-------------------------"
git ls-files --others --exclude-standard
echo
read -e -r -p "Would you like to add these files to git? [y,n] " answer
if [[ "$answer" = "y" ]]; then
git add -A
fi
fi
}
git_ask_commit_message() {
if [[ $num_commits -gt 0 ]]; then
git --paginate diff --color --diff-filter=d HEAD || true
fi
git status -s
echo
echo "Git commit message:"
echo "-------------------"
if [[ $dcommit_message != "" ]]; then
echo "$dcommit_message"
echo
else
if [[ $num_commits -gt 0 ]]; then
git --no-pager log -1 --pretty=%B
fi
fi
echo "Git repository: $GIT_REPO"
while true; do
if [[ $num_commits -gt 0 ]]; then
echo "Press enter to commit with message above to" \
"$(git rev-parse --abbrev-ref HEAD)" \
"(author: ${git_author})"
else
echo "Press enter to commit with message above to" \
"(author: ${git_author})"
fi
read -e -r answer
if [[ ${#answer} -ne 0 ]] && [[ ${#answer} -lt 1 ]]; then
echo "Error: the commit message is too short." >&2
echo "" >&2
else
break
fi
done
}
git_ci() {
git_commit_opts=("-a")
git_name="$(git config user.name)"
git_email="$(git config user.email)"
git_author="$git_name <$git_email>"
dcommit_message="$(git config custom.dcommitmessage)" || true
num_commits=$(git rev-list --all --count)
if [[ $num_commits -gt 0 ]]; then
local diff_lines
diff_lines="$(git --no-pager diff --name-only --diff-filter="TXBU" HEAD \
| wc -l)"
if [[ $diff_lines -gt "0" ]]; then
echo "There is an issue in the repository '$GIT_REPO'."
exit 1
fi
git_add
if [[ "$(git status --porcelain | wc -l)" -eq 0 ]]; then
echo "Nothing to commit to '$GIT_REPO'."
exit 0
fi
fi
git_ask_commit_message
if [[ $answer = "" ]]; then
if [[ "$dcommit_message" != "" ]]; then
git_commit_opts+=("-m" "$dcommit_message")
echo "Message: ${dcommit_message}"
else
if [[ $num_commits -gt 0 ]]; then
git_commit_opts+=("--reset-author" "--reuse-message=HEAD")
else
git_commit_opts+=("--reset-author")
fi
fi
else
git_commit_opts+=("-m" "$answer")
echo "Message: ${answer}"
fi
echo "[RUN] git commit" "${git_commit_opts[@]}"
if git commit "${git_commit_opts[@]}"; then
echo
echo "[COMMIT] git commit was SUCCESSFUL."
else
echo
echo "[COMMIT] git commit has FAILED."
exit 1
fi
}
main() {
trap "error_handler" ERR
set -o errtrace
if ! GIT_REPO=$(git rev-parse --show-toplevel); then
exit 1
fi
echo "[GIT REPO] $GIT_REPO"
echo
git_ci
echo
}
main