-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-smart-commit
executable file
·53 lines (48 loc) · 1.66 KB
/
git-smart-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
#!/bin/bash
# Wrapper for "git commit":
# - automatically adds --all if you didn't say what to commit
# - recognizes paths and options
# - treats other arguments as commit message (no quoting necessary)
#
# Example usage (with suggested alias ci = smart-commit):
#
# git ci This is super simple
CHANGE_SELECTOR=false # does some argument specify what to commit?
COMMIT_OPTS="--verbose" # show diff (double-check what you commit)
# analyze and group arguments into options, paths and message
while [ $# -gt 0 ]; do
arg="$1"; shift
case "$arg" in
# options that take arguments - get the argument, too
-c|-C|--*message|-m|--fixup|--squash|-F|--file|--author|--date|-t|--template)
COMMIT_OPTS="$COMMIT_OPTS $arg $1"; shift
;;
# options mutually exclusive with --all
-p|--patch|-o|--only|-i|--include|--interactive)
COMMIT_OPTS="$COMMIT_OPTS $arg"
CHANGE_SELECTOR=true
;;
--) # treat all remaining args as paths
COMMIT_PATHS="-- $@"
CHANGE_SELECTOR=true
break
;;
-?*) # options without arguments
COMMIT_OPTS="$COMMIT_OPTS $arg"
;;
**) # everything else -> commmit message
COMMIT_MSG="$(echo $COMMIT_MSG $arg)" # echo -> trim extra whtespace
;;
esac
done
# are there staged changes or options mutually exclusive with --all?
if git diff --cached --quiet && ! $CHANGE_SELECTOR; then
echo Assuming you want to commit all changes.
COMMIT_OPTS="$COMMIT_OPTS --all"
fi
# message content must be quoted, but empty quotes would trigger error
if [ -z "$COMMIT_MSG" ]; then
git commit $COMMIT_OPTS $COMMIT_PATHS
else
git commit $COMMIT_OPTS --message "$COMMIT_MSG" $COMMIT_PATHS
fi