autotagging: fix a tiny quirk, and document autotagging properly
[guilt.git] / guilt-repair
blob42f6625578149d3e70bd7ddc883d99eab0ad6979
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2008
6 DO_NOT_CHECK_STATUS_FILE_FORMAT=1
8 USAGE="--full | --status | --autotag"
9 . `dirname $0`/guilt
11 safety_abort()
13 die "Please read the man page first. (you need to specify repair mode to proceed)."
16 [ $# -ne 1 ] && safety_abort
18 case "$1" in
19 --full)
20 repair="full"
22 --status)
23 repair="status"
25 --autotag)
26 repair="autotag"
29 usage
31 esac
33 oldrev=`git show-ref -s "refs/heads/$branch"`
36 # Check whether status file needs fixing/upgrading. If not, just return,
37 # otherwise proceed to rewrite the status file and set up proper refs
39 repair_status()
41 _disp "Checking status file format..."
42 if ! grep "^[0-9a-f]\{40\}:" "$applied" > /dev/null ; then
43 disp "ok; no upgrade necessary."
44 return 0
46 disp "old; about to upgrade."
48 # we got an old format status file
50 printf "" > "$applied.new"
52 cat "$applied" | while read line ; do
53 hash=`echo "$line" | cut -d: -f1`
54 pname=`echo "$line" | cut -d: -f2-`
56 npname=`echo "$pname" | sed -e 's/ /-/g'`
57 [ "$pname" != "$npname" -a -e "$npname" ] && die "Patch name collision"
59 git update-ref "refs/patches/$branch/$npname" "$hash"
60 echo "$npname" >> "$applied.new"
62 if [ "$pname" != "$npname" ]; then
63 series_rename_patch "$pname" "$npname"
65 mv "$GUILT_DIR/$branch/$pname" "$GUILT_DIR/$branch/$npname"
67 done
69 # replace the status file
70 mv "$applied" "$applied~"
71 mv "$applied.new" "$applied"
73 disp "Upgrade complete."
75 return 0
79 # Pop all patches - forcefully.
81 repair_pushed()
83 if [ -s "$applied" ]; then
84 # there were some patches applied
85 newrev=`git rev-parse refs/patches/$branch/$(head -1 < "$applied")^`
86 else
87 # no patches were applied, but let's do all the work anyway
88 newrev="$oldrev"
91 disp "Current HEAD commit $oldrev"
92 disp "New HEAD commit $newrev"
94 disp "About to forcefully pop all patches..."
95 _disp "Are you sure you want to proceed? [y/N] "
96 read n
97 if [ "$n" != "y" ] && [ "$n" != "Y" ]; then
98 die "Aborting..."
101 # blow away any commits
102 git reset --hard "$newrev" > /dev/null
104 # blow away the applied stack
105 remove_patch_refs < "$applied"
106 printf "" > "$applied"
108 # remove the ${branch}_{top,bottom,base} tags
109 update_stack_tags "force"
111 disp "Patches should be popped."
112 return 0
115 # update the top/bottom/base tags
116 repair_autotag()
118 update_stack_tags
121 case "$repair" in
122 full)
123 repair_status
124 repair_pushed
126 status)
127 repair_status
129 autotag)
130 repair_autotag
133 die "Internal error"
135 esac
137 disp "Repair complete."
138 exit 0