Skip to content

Commit 82162ea

Browse files
authored
Merge pull request #585 from tardypad/stamp
Add git stamp
2 parents 15ae263 + c5bd28d commit 82162ea

File tree

8 files changed

+544
-0
lines changed

8 files changed

+544
-0
lines changed

Commands.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- [`git show-merged-branches`](#git-show-merged-branches)
5757
- [`git show-tree`](#git-show-tree)
5858
- [`git show-unmerged-branches`](#git-show-unmerged-branches)
59+
- [`git stamp`](#git-stamp)
5960
- [`git squash`](#git-squash)
6061
- [`git standup`](#git-standup)
6162
- [`git summary`](#git-summary)
@@ -958,6 +959,79 @@ For example, running `git show-tree` will display:
958959

959960
Be free to try it for yourself!
960961

962+
963+
## git stamp
964+
965+
Stamp the last commit message
966+
967+
Commit message is
968+
969+
```bash
970+
Fix timezone bug
971+
```
972+
973+
Reference the issues numbers from your bug tracker
974+
975+
```bash
976+
$ git stamp Issue FOO-123
977+
978+
commit 787590e42c9bacd249f3b79faee7aecdc9de28ec
979+
Author: Jack <[email protected]>
980+
Commit: Jack <[email protected]>
981+
982+
Fix timezone bug
983+
984+
Issue FOO-123
985+
986+
$ git stamp Issue FOO-456 \#close
987+
988+
commit f8d920511e052bea39ce2088d1d723b475aeff87
989+
Author: Jack <[email protected]>
990+
Commit: Jack <[email protected]>
991+
992+
Fix timezone bug
993+
994+
Issue FOO-123
995+
996+
Issue FOO-456 #close
997+
```
998+
999+
Link to its review page
1000+
1001+
```bash
1002+
$ git stamp Review https://reviews.foo.org/r/4567/
1003+
1004+
commit 6c6bcf43bd32a76e37b6fc9708d3ff0ae723c7da
1005+
Author: Jack <[email protected]>
1006+
Commit: Jack <[email protected]>
1007+
1008+
Fix timezone bug
1009+
1010+
Issue FOO-123
1011+
1012+
Issue FOO-456 #close
1013+
1014+
Review https://reviews.foo.org/r/4567/
1015+
```
1016+
1017+
Replace previous issues with a new one
1018+
(Note that the identifier is case insensitive)
1019+
1020+
```bash
1021+
$ git stamp --replace issue BAR-123
1022+
1023+
commit 2b93c56b2340578cc3478008e2cadb05a7bcccfa
1024+
Author: Jack <[email protected]>
1025+
Commit: Jack <[email protected]>
1026+
1027+
Fix timezone bug
1028+
1029+
Review https://reviews.foo.org/r/4567/
1030+
1031+
issue BAR-123
1032+
```
1033+
1034+
9611035
## git standup
9621036

9631037
Recall what you did or find what someone else did in a given range of time.

bin/git-stamp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
3+
4+
init_variables() {
5+
COMMAND=${0#*-}
6+
7+
REPLACE=false
8+
unset ID
9+
unset MSG
10+
}
11+
12+
13+
usage() {
14+
cat << EOF
15+
usage: git ${COMMAND} [<options>] <id> [<messages>]
16+
17+
Options:
18+
-r, --replace replace all previous stamps with same id
19+
EOF
20+
}
21+
22+
23+
error() {
24+
if [[ -n "$1" ]]; then
25+
local msg=$( echo "error: $1" )
26+
echo -e "${msg}" >&2
27+
fi
28+
usage
29+
exit 1
30+
}
31+
32+
33+
stamp() {
34+
local commit_msg=$( git log -1 --pretty=%B )
35+
local stamp_msg
36+
[[ -n "${MSG}" ]] && stamp_msg="${ID} ${MSG}" || stamp_msg="${ID}"
37+
38+
if ${REPLACE}; then
39+
# remove previous stamps with same ID from the commit message
40+
commit_msg=$(
41+
echo "${commit_msg}" \
42+
| grep --ignore-case --invert-match "^${ID}\b" \
43+
| cat --squeeze-blank
44+
)
45+
fi
46+
47+
# append the stamp to the commit message in a new paragraph
48+
git commit --amend \
49+
--message "${commit_msg}" \
50+
--message "${stamp_msg}" \
51+
> /dev/null
52+
53+
# show result
54+
git log -1 --pretty=full
55+
}
56+
57+
58+
parse_options() {
59+
while [[ "$#" -gt 0 ]]; do
60+
case "$1" in
61+
-h)
62+
usage
63+
exit 0
64+
;;
65+
--replace|-r)
66+
REPLACE=true
67+
shift
68+
;;
69+
*)
70+
break
71+
;;
72+
esac
73+
done
74+
75+
ID="$1"
76+
MSG="${@:2}"
77+
}
78+
79+
80+
validate_options() {
81+
# ID should be set to non-empty string
82+
if [[ -z "${ID}" ]]; then
83+
error "missing stamp identifier"
84+
fi
85+
}
86+
87+
88+
init_variables
89+
parse_options "$@"
90+
validate_options
91+
92+
stamp

etc/bash_completion.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ _git_scp(){
134134
__git_complete_remote_or_refspec
135135
}
136136

137+
_git_stamp(){
138+
__gitcomp '--replace -r'
139+
}
140+
137141
_git_rscp(){
138142
__git_complete_remote_or_refspec
139143
}

etc/git-extras-completion.zsh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ _git-squash() {
337337
':branch-name:__gitex_branch_names'
338338
}
339339

340+
_git-stamp() {
341+
_arguments -C \
342+
'(--replace -r)'{--replace,-r}'[replace stamps with same id]'
343+
}
340344

341345
_git-summary() {
342346
_arguments '--line[summarize with lines rather than commits]'
@@ -407,6 +411,7 @@ zstyle ':completion:*:*:git:*' user-commands \
407411
show-tree:'show branch tree of commit history' \
408412
show-unmerged-branches:'show unmerged branches' \
409413
squash:'import changes from a branch' \
414+
stamp:'stamp the last commit message' \
410415
standup:'recall the commit history' \
411416
summary:'show repository summary' \
412417
sync:'sync local branch with remote branch' \

man/git-stamp.1

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
.\" generated with Ronn/v0.7.3
2+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
3+
.
4+
.TH "GIT\-STAMP" "1" "October 2016" "" ""
5+
.
6+
.SH "NAME"
7+
\fBgit\-stamp\fR \- Stamp the last commit message
8+
.
9+
.SH "SYNOPSIS"
10+
\fBgit stamp [<options>] <id> [<messages>]\fR
11+
.
12+
.SH "DESCRIPTION"
13+
Lets you amend the last commit with a stamp message\.
14+
.
15+
.P
16+
The command appends a message with its identifier to the last commit message\.
17+
.
18+
.br
19+
By default all stamps are appended as a new paragraph to the commit message\.
20+
.
21+
.br
22+
You can change this behavior by using the \-\-replace flag\.
23+
.
24+
.br
25+
With this flag, all the related stamps with the same identifier will be removed first before the new one gets appended\.
26+
.
27+
.P
28+
\fBWARNING!\fR If a commit message without stamp have a line starting with the same identifier, it will be interpreted as a stamp
29+
.
30+
.SH "OPTIONS"
31+
\-r, \-\-replace
32+
.
33+
.IP "" 4
34+
.
35+
.nf
36+
37+
Replace all previous stamps in the last commit message that have the same identifier
38+
The identifier is case insensitive for this replacement
39+
.
40+
.fi
41+
.
42+
.IP "" 0
43+
.
44+
.SH "EXAMPLES"
45+
Commit message is
46+
.
47+
.IP "" 4
48+
.
49+
.nf
50+
51+
| Fix timezone bug
52+
.
53+
.fi
54+
.
55+
.IP "" 0
56+
.
57+
.P
58+
Reference the issues numbers from your bug tracker
59+
.
60+
.IP "" 4
61+
.
62+
.nf
63+
64+
$ git stamp Issue FOO\-123
65+
$ git stamp Issue FOO\-456 \e#close
66+
67+
| Fix timezone bug
68+
|
69+
| Issue FOO\-123
70+
|
71+
| Issue FOO\-456 #close
72+
.
73+
.fi
74+
.
75+
.IP "" 0
76+
.
77+
.P
78+
Link to its review page
79+
.
80+
.IP "" 4
81+
.
82+
.nf
83+
84+
$ git stamp Review https://reviews\.foo\.org/r/4567/
85+
86+
| Fix timezone bug
87+
|
88+
| Issue FOO\-123
89+
|
90+
| Issue FOO\-456 #close
91+
|
92+
| Review https://reviews\.foo\.org/r/4567/
93+
.
94+
.fi
95+
.
96+
.IP "" 0
97+
.
98+
.P
99+
Replace previous issues with a new one
100+
.
101+
.br
102+
(Note that the identifier is case insensitive)
103+
.
104+
.IP "" 4
105+
.
106+
.nf
107+
108+
$ git stamp \-\-replace issue BAR\-123
109+
110+
| Fix timezone bug
111+
|
112+
| Review https://reviews\.foo\.org/r/4567/
113+
|
114+
| issue BAR\-123
115+
.
116+
.fi
117+
.
118+
.IP "" 0
119+
.
120+
.SH "AUTHOR"
121+
Written by Damien Tardy\-Panis <\fIdamien@tardypad\.me\fR>
122+
.
123+
.SH "REPORTING BUGS"
124+
<\fIhttp://github\.com/tj/git\-extras/issues\fR>
125+
.
126+
.SH "SEE ALSO"
127+
<\fIhttps://github\.com/tj/git\-extras\fR>

0 commit comments

Comments
 (0)