Debian Codegraph @

guilt-repair

raw
1 #!/bin/sh
2 #
3 # Copyright (c) Josef "Jeff" Sipek, 2008-2013
4 #
5
6 DO_NOT_CHECK_STATUS_FILE_FORMAT=1
7
8 USAGE="--full | --status"
9 if [ -z "$GUILT_VERSION" ]; then
10 echo "Invoking `basename "$0"` directly is no longer supported." >&2
11 exit 1
12 fi
13
14 safety_abort()
15 {
16 die "Please read the man page first. (you need to specify repair mode to proceed)."
17 }
18
19 #
20 # Check whether status file needs fixing/upgrading. If not, just return,
21 # otherwise proceed to rewrite the status file and set up proper refs
22 #
23 repair_status()
24 {
25 _disp "Checking status file format..."
26 if ! grep "^[0-9a-f]\{40\}:" "$applied" > /dev/null ; then
27 disp "ok; no upgrade necessary."
28 return 0
29 fi
30 disp "old; about to upgrade."
31
32 # we got an old format status file
33
34 printf "" > "$applied.new"
35
36 cat "$applied" | while read line ; do
37 hash=`echo "$line" | cut -d: -f1`
38 pname=`echo "$line" | cut -d: -f2-`
39
40 npname=`echo "$pname" | sed -e 's/ /-/g'`
41 [ "$pname" != "$npname" -a -e "$npname" ] && die "Patch name collision"
42
43 git update-ref "refs/patches/$branch/$npname" "$hash"
44 echo "$npname" >> "$applied.new"
45
46 if [ "$pname" != "$npname" ]; then
47 series_rename_patch "$pname" "$npname"
48
49 mv "$GUILT_DIR/$branch/$pname" "$GUILT_DIR/$branch/$npname"
50 fi
51 done
52
53 # replace the status file
54 mv "$applied" "$applied~"
55 mv "$applied.new" "$applied"
56
57 disp "Upgrade complete."
58
59 return 0
60 }
61
62 #
63 # Pop all patches - forcefully.
64 #
65 repair_pushed()
66 {
67 if [ -s "$applied" ]; then
68 # there were some patches applied
69 newrev=`git rev-parse refs/patches/$branch/$(head_n 1 < "$applied")^`
70 else
71 # no patches were applied, but let's do all the work anyway
72 newrev="$oldrev"
73 fi
74
75 disp "Current HEAD commit $oldrev"
76 disp "New HEAD commit $newrev"
77
78 disp "About to forcefully pop all patches..."
79 _disp "Are you sure you want to proceed? [y/N] "
80 read n
81 if [ "$n" != "y" ] && [ "$n" != "Y" ]; then
82 die "Aborting..."
83 fi
84
85 # blow away any commits
86 git reset --hard "$newrev" > /dev/null
87 if [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
88 then
89 git symbolic-ref HEAD refs/heads/$branch
90 git update-ref -d refs/heads/$GUILT_PREFIX$branch
91 fi
92
93 # blow away the applied stack
94 remove_patch_refs < "$applied"
95 printf "" > "$applied"
96
97 disp "Patches should be popped."
98 return 0
99 }
100
101 _main() {
102
103 [ $# -ne 1 ] && safety_abort
104
105 case "$1" in
106 --full)
107 repair="full"
108 ;;
109 --status)
110 repair="status"
111 ;;
112 --autotag)
113 echo "Autotagging is no longer supported" >&2
114 ;;
115 *)
116 usage
117 ;;
118 esac
119
120 oldrev=`git show-ref -s "refs/heads/\`git_branch\`"`
121
122 case "$repair" in
123 full)
124 repair_status
125 repair_pushed
126 ;;
127 status)
128 repair_status
129 ;;
130 *)
131 die "Internal error"
132 ;;
133 esac
134
135 disp "Repair complete."
136 exit 0
137
138 }