ignore unknown color configuration
[git/kirr.git] / contrib / git-resurrect.sh
blobc364dda696912037cfafdb5e182b6e58f99e0a7e
1 #!/bin/sh
3 USAGE="[-a] [-r] [-m] [-t] [-n] [-b <newname>] <name>"
4 LONG_USAGE="git-resurrect attempts to find traces of a branch tip
5 called <name>, and tries to resurrect it. Currently, the reflog is
6 searched for checkout messages, and with -r also merge messages. With
7 -m and -t, the history of all refs is scanned for Merge <name> into
8 other/Merge <other> into <name> (respectively) commit subjects, which
9 is rather slow but allows you to resurrect other people's topic
10 branches."
12 OPTIONS_SPEC="\
13 git resurrect $USAGE
15 b,branch= save branch as <newname> instead of <name>
16 a,all same as -l -r -m -t
17 k,keep-going full rev-list scan (instead of first match)
18 l,reflog scan reflog for checkouts (enabled by default)
19 r,reflog-merges scan for merges recorded in reflog
20 m,merges scan for merges into other branches (slow)
21 t,merge-targets scan for merges of other branches into <name>
22 n,dry-run don't recreate the branch"
24 . git-sh-setup
26 search_reflog () {
27 sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \
28 < "$GIT_DIR"/logs/HEAD
31 search_reflog_merges () {
32 git rev-parse $(
33 sed -ne 's~^[^ ]* \([^ ]*\) .*\tmerge '"$1"':.*~\1^2~p' \
34 < "$GIT_DIR"/logs/HEAD
38 _x40="[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
39 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
41 search_merges () {
42 git rev-list --all --grep="Merge branch '$1'" \
43 --pretty=tformat:"%P %s" |
44 sed -ne "/^$_x40 \($_x40\) Merge .*/ {s//\1/p;$early_exit}"
47 search_merge_targets () {
48 git rev-list --all --grep="Merge branch '[^']*' into $branch\$" \
49 --pretty=tformat:"%H %s" --all |
50 sed -ne "/^\($_x40\) Merge .*/ {s//\1/p;$early_exit} "
53 dry_run=
54 early_exit=q
55 scan_reflog=t
56 scan_reflog_merges=
57 scan_merges=
58 scan_merge_targets=
59 new_name=
61 while test "$#" != 0; do
62 case "$1" in
63 -b|--branch)
64 shift
65 new_name="$1"
67 -n|--dry-run)
68 dry_run=t
70 --no-dry-run)
71 dry_run=
73 -k|--keep-going)
74 early_exit=
76 --no-keep-going)
77 early_exit=q
79 -m|--merges)
80 scan_merges=t
82 --no-merges)
83 scan_merges=
85 -l|--reflog)
86 scan_reflog=t
88 --no-reflog)
89 scan_reflog=
91 -r|--reflog_merges)
92 scan_reflog_merges=t
94 --no-reflog_merges)
95 scan_reflog_merges=
97 -t|--merge-targets)
98 scan_merge_targets=t
100 --no-merge-targets)
101 scan_merge_targets=
103 -a|--all)
104 scan_reflog=t
105 scan_reflog_merges=t
106 scan_merges=t
107 scan_merge_targets=t
110 shift
111 break
114 usage
116 esac
117 shift
118 done
120 test "$#" = 1 || usage
122 all_strategies="$scan_reflog$scan_reflog_merges$scan_merges$scan_merge_targets"
123 if test -z "$all_strategies"; then
124 die "must enable at least one of -lrmt"
127 branch="$1"
128 test -z "$new_name" && new_name="$branch"
130 if test ! -z "$scan_reflog"; then
131 if test -r "$GIT_DIR"/logs/HEAD; then
132 candidates="$(search_reflog $branch)"
133 else
134 die 'reflog scanning requested, but' \
135 '$GIT_DIR/logs/HEAD not readable'
138 if test ! -z "$scan_reflog_merges"; then
139 if test -r "$GIT_DIR"/logs/HEAD; then
140 candidates="$candidates $(search_reflog_merges $branch)"
141 else
142 die 'reflog scanning requested, but' \
143 '$GIT_DIR/logs/HEAD not readable'
146 if test ! -z "$scan_merges"; then
147 candidates="$candidates $(search_merges $branch)"
149 if test ! -z "$scan_merge_targets"; then
150 candidates="$candidates $(search_merge_targets $branch)"
153 candidates="$(git rev-parse $candidates | sort -u)"
155 if test -z "$candidates"; then
156 hint=
157 test "z$all_strategies" != "ztttt" \
158 && hint=" (maybe try again with -a)"
159 die "no candidates for $branch found$hint"
162 echo "** Candidates for $branch **"
163 for cmt in $candidates; do
164 git --no-pager log --pretty=tformat:"%ct:%h [%cr] %s" --abbrev-commit -1 $cmt
165 done \
166 | sort -n | cut -d: -f2-
168 newest="$(git rev-list -1 $candidates)"
169 if test ! -z "$dry_run"; then
170 printf "** Most recent: "
171 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
172 elif ! git rev-parse --verify --quiet $new_name >/dev/null; then
173 printf "** Restoring $new_name to "
174 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
175 git branch $new_name $newest
176 else
177 printf "Most recent: "
178 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
179 echo "** $new_name already exists, doing nothing"