chiark / gitweb /
changelog: start 9.14
[dgit.git] / absurd / git
1 #!/bin/sh
2 set -e
3
4 case "$DGIT_ABSURD_DEBUG" in
5 ''|0)   exec 3>/dev/null ;;
6 1)      exec 3>>../../gbp-pq-output ;;
7 *)      exec 3>>../../gbp-pq-output 2>&3 ;;
8 esac
9
10 exec 4>>../../absurd-apply-warnings
11
12 log () {
13         echo >&3 "DGIT ABSURD GIT APPLY (DEBUG)  $*"
14         echo >&2 "DGIT ABSURD GIT APPLY (STDERR) $*"
15 }
16
17 fail () {
18         log "FAILED: $*"
19         exit 127
20 }
21
22 self=${0%/*}
23 npath=${PATH#$self:}
24 if test "x$PATH" = "x$npath"; then
25         fail "PATH FILTER FAIL ($0 $self $PATH)"
26 fi
27
28 filter_path () {
29         PATH=$npath
30 }
31
32 squash_author () {
33         author_vars='NAME EMAIL DATE'
34         for var in in $author_vars; do
35                 eval "GIT_AUTHOR_$var=\$GIT_COMMITTER_$var"
36         done
37 }
38
39 try_commit () { git commit-tree "$try_commit_obj" </dev/null >/dev/null; }
40
41 maybe_squash_author () {
42         if   (set -e; filter_path;                try_commit); then return; fi
43         if ! (set -e; filter_path; squash_author; try_commit); then return; fi
44         read last <../../absurd-apply-applied
45         echo >&4 \
46  "dgit: warning: suppressed corrupted metadata! ($last)"
47         squash_author
48 }
49
50 bypass=true
51 for arg in "$@"; do
52         case "$arg" in
53         apply)  bypass=false; break     ;;
54         commit-tree)
55                 # Work around #950446 in gbp
56                 echo >&3 "DGIT ABSURD GIT APPLY - COMMIT-TREE: $*"
57                 try_commit_obj="$2"
58                 maybe_squash_author
59                 bypass=true; break
60                 ;;
61         -*)                             ;;
62         *)      bypass=true; break      ;;
63         esac
64 done
65
66 if $bypass; then
67         filter_path
68         echo >&3 "DGIT ABSURD GIT APPLY - BYPASS: $*"
69         exec git "$@"
70 fi
71
72 log "NO BYPASS: $*"
73
74 case "$DGIT_ABSURD_DEBUG" in
75 ''|0|1) ;;
76 *)      set -x ;;
77 esac
78
79 #exec >/dev/tty 2>&1
80
81 index=0
82 noo=0
83
84 for arg in "$@"; do
85         case "$noo.$arg" in
86         1.--index)
87                 index=1
88                 continue
89                 ;;
90         1.--whitespace=fix)
91                 continue
92                 ;;
93         ?.-*)
94                 fail "UNKNOWN OPTION $arg ($*)"
95                 ;;
96         0.apply)
97                 ;;
98         1.*)    patch="$arg"
99                 ;;
100         *)
101                 fail "BAD USAGE $arg ($noo $*)"
102         esac
103         noo=$(( $noo + 1 ))
104 done
105
106 if [ $noo != 2 ]; then
107         fail "NO PATCH ($*)"
108 fi
109
110 pwd=`pwd`
111 patch=${patch#$pwd/debian/patches/}
112 rm -f debian/patches/series
113
114 # Work around #848611.
115 # We need a stunt filename which the source package must not
116 # contain.  A trick is to use the commit hash of HEAD, whose
117 # hash value cannot appear in any file in its own tree.
118 omgwtf="dgit-omg-wtf-$(git rev-parse HEAD)"
119 cat <<END >debian/patches/$omgwtf
120 ---
121 --- a/$omgwtf   2016-10-31 23:28:47.314155919 +0000
122 +++ b/$omgwtf   2016-12-18 22:40:01.870058270 +0000
123 @@ -0,0 +1 @@
124 +:
125 END
126 printf "%s\n" "$omgwtf" >debian/patches/series
127 printf "%s\n" "$patch" >>debian/patches/series
128
129 # Just in case some joker tries to patch .git/something
130 mv .git ../.git
131 set +e
132 dpkg-source --before-build .
133 rc=$?
134 set -e
135 rm -rf .git
136 mv ../.git .
137 test $rc = 0
138
139 rm -f $omgwtf debian/patches/$omgwtf
140
141 rm -rf .pc
142 git checkout debian/patches/series
143 git add -Af .
144
145 log "APPLIED $patch"
146 printf >../../absurd-apply-applied '%s\n' "$patch"
147 #printf 'APPLIED '; date --iso-8601=ns