Skip to content

Commit b33ec65

Browse files
authored
Merge pull request #940 from overengineer/feature/magic
git-magic: Automated commit messages
2 parents 9689082 + 3f2ad3f commit b33ec65

File tree

8 files changed

+449
-0
lines changed

8 files changed

+449
-0
lines changed

Commands.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [`git local-commits`](#git-local-commits)
3737
- [`git lock`](#git-lock)
3838
- [`git locked`](#git-locked)
39+
- [`git magic`](#git-magic)
3940
- [`git merge-into`](#git-merge-into)
4041
- [`git merge-repo`](#git-merge-repo)
4142
- [`git missing`](#git-missing)
@@ -1544,3 +1545,7 @@ total 308
15441545
## git abort
15451546
15461547
Abort current rebase, merge or cherry-pick, without the need to find exact command in history.
1548+
1549+
## git magic
1550+
1551+
Commits changes with a generated message.

bin/git-magic

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
3+
4+
# Bash unofficial strict mode
5+
set -eo pipefail
6+
IFS=$'\n\t'
7+
8+
cd "$(git rev-parse --show-toplevel)"
9+
10+
USAGE='git-magic [-a] [-m msg] [-e] [-p] [-f]'
11+
12+
ALL=false
13+
PUSH=false
14+
FORCE=''
15+
ARGS=()
16+
17+
while getopts "m:eapfh" arg; do
18+
case "${arg}" in
19+
m)
20+
ARGS+=("-m")
21+
ARGS+=("${OPTARG}")
22+
;;
23+
e)
24+
ARGS+=("-e")
25+
;;
26+
a)
27+
ALL=true
28+
;;
29+
p)
30+
PUSH=true
31+
;;
32+
f)
33+
FORCE='-f'
34+
;;
35+
h)
36+
echo $USAGE
37+
exit 0
38+
;;
39+
?)
40+
echo "${USAGE}"
41+
exit 1
42+
;;
43+
esac
44+
done
45+
46+
shift $((OPTIND-1))
47+
48+
if [[ $# != 0 ]]; then
49+
echo "Unknown arguments: $@"
50+
echo "${USAGE}"
51+
exit 1
52+
fi
53+
54+
set -- "${ARGS[@]}" # restore positional parameters
55+
56+
if [[ $ALL == true ]]; then
57+
58+
# Check if there is no changes to stage
59+
if [[ -z $(git status --porcelain) ]]; then
60+
echo "No changes to commit"
61+
exit 0
62+
fi
63+
64+
# Get confirmation from user
65+
git status
66+
echo "Everything will be added"
67+
read -p "Press enter to continue"
68+
69+
# Restore staging area so that, for example,
70+
# add and modify will not be separate entries in status
71+
git restore --staged . || true
72+
git add .
73+
fi
74+
75+
# Commit with generated message
76+
git commit --no-edit "$@" -m "$(git status --porcelain -uno)"
77+
78+
if [[ $PUSH == true ]]; then
79+
git push $FORCE
80+
fi
81+
82+
# --no-edit by default. use option -e to override this
83+
84+
# Arguments are passed with quoted "$@" to avoid misparsing
85+
86+
# Generated message comes after user provided arguments
87+
# so that user can insert title before it

etc/git-extras-completion.zsh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ zstyle ':completion:*:*:git:*' user-commands $existing_user_commands \
379379
local-commits:'list local commits' \
380380
lock:'lock a file excluded from version control' \
381381
locked:'ls files that have been locked' \
382+
magic:'commits everything with a generated message' \
382383
merge-into:'merge one branch into another' \
383384
merge-repo:'merge two repo histories' \
384385
missing:'show commits missing from another branch' \

man/git-extras.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ git-extras(1) -- Awesome GIT utilities
6363
- **git-local-commits(1)** List local commits
6464
- **git-lock(1)** Lock a file excluded from version control
6565
- **git-locked(1)** ls files that have been locked
66+
- **git-magic(1)** Automate add/commit/push routines
6667
- **git-merge-into(1)** Merge one branch into another
6768
- **git-merge-repo(1)** Merge two repo histories
6869
- **git-missing(1)** Show commits missing from another branch

man/git-magic.1

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
.\" generated with Ronn/v0.7.3
2+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
3+
.
4+
.TH "GIT\-MAGIC" "1" "October 2021" "" "Git Extras"
5+
.
6+
.SH "NAME"
7+
\fBgit\-magic\fR \- Automate add/commit/push routines
8+
.
9+
.SH "SYNOPSIS"
10+
\fBgit\-magic\fR [\-a] [\-m \fImsg\fR] [\-e] [\-p] [\-f]
11+
.
12+
.SH "DESCRIPTION"
13+
Produces summary of changes for commit message from \fBgit status \-\-porcelain\fR output\. Commits staged changes with the generated commit message and opens editor to modify generated commit message optionally\. Also staging and pushing can be automated optinally\.
14+
.
15+
.SH "OPTIONS"
16+
\-a
17+
.
18+
.P
19+
Adds everything including untracked files\.
20+
.
21+
.P
22+
\-m \fImsg\fR
23+
.
24+
.P
25+
Use the given \fImsg\fR as the commit message\. If multiple \-m options are given, their values are concatenated as separate paragraphs\. Passed to git commit command\. The generated is appended to user\-given messages\.
26+
.
27+
.P
28+
\-e
29+
.
30+
.P
31+
This option lets you further edit the generated message\. Passed to git commit command\.
32+
.
33+
.P
34+
\-p
35+
.
36+
.P
37+
Runs \fBgit push\fR after commit\.
38+
.
39+
.P
40+
\-f
41+
.
42+
.P
43+
Adds \fB\-f\fR option to \fBgit push\fR command\.
44+
.
45+
.P
46+
\-h
47+
.
48+
.P
49+
Prints synopsis\.
50+
.
51+
.SH "EXAMPLES"
52+
This example stages all changes then commits with automatic commit message\.
53+
.
54+
.IP "" 4
55+
.
56+
.nf
57+
58+
$ git magic \-a
59+
[feature/magic dc2a11e] A man/git\-magic\.md
60+
1 file changed, 37 insertions(+)
61+
create mode 100644 man/git\-auto\.md
62+
# git log
63+
Author: overengineer <54alpersaid@gmail\.com>
64+
Date: Thu Sep 30 20:14:22 2021 +0300
65+
66+
M man/git\-magic\.md
67+
.
68+
.fi
69+
.
70+
.IP "" 0
71+
.
72+
.P
73+
\fB\-m\fR option PREPENDS generated message\.
74+
.
75+
.IP "" 4
76+
.
77+
.nf
78+
79+
$ git magic \-am "Added documentation for git magic"
80+
[feature/magic dc2a11e] Added documentation for git magic
81+
1 file changed, 42 insertions(+), 0 deletions(\-)
82+
create mode 100644 A man/git\-auto\.md
83+
$ git log
84+
Author: overengineer <54alpersaid@gmail\.com>
85+
Date: Thu Sep 30 20:14:22 2021 +0300
86+
87+
Added documentation for git magic
88+
89+
M man/git\-magic\.md
90+
.
91+
.fi
92+
.
93+
.IP "" 0
94+
.
95+
.SH "AUTHOR"
96+
Written by Alper S\. Soylu \fI54alpersaid@gmail\.com\fR
97+
.
98+
.SH "REPORTING BUGS"
99+
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
100+
.
101+
.SH "SEE ALSO"
102+
<\fIhttps://github\.com/tj/git\-extras\fR>

man/git-magic.html

Lines changed: 167 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)