got patch: allow to reverse a patch add a flag to got_patch to reverse a patch before applying and the -R flag for `got patch'. ok stsp@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
diff --git a/got/got.1 b/got/got.1
index bd6e06c..b91cfb2 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1285,7 +1285,7 @@ option)
.El
.El
.Tg pa
-.It Cm patch Oo Fl n Oc Oo Fl p Ar strip-count Oc Op Ar patchfile
+.It Cm patch Oo Fl n Oc Oo Fl p Ar strip-count Oc Oo Fl R Oc Op Ar patchfile
.Dl Pq alias: Cm pa
Apply changes from
.Ar patchfile
@@ -1367,6 +1367,8 @@ and
path prefixes generated by
.Xr git-diff 1
will be recognized and stripped automatically.
+.It Fl R
+Reverse the patch before applying it.
.El
.Tg rv
.It Cm revert Oo Fl p Oc Oo Fl F Ar response-script Oc Oo Fl R Oc Ar path ...
diff --git a/got/got.c b/got/got.c
index f5b90a5..80e5d2f 100644
--- a/got/got.c
+++ b/got/got.c
@@ -7153,8 +7153,8 @@ done:
__dead static void
usage_patch(void)
{
- fprintf(stderr, "usage: %s patch [-n] [-p strip-count] [patchfile]\n",
- getprogname());
+ fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
+ "[-R] [patchfile]\n", getprogname());
exit(1);
}
@@ -7243,10 +7243,10 @@ cmd_patch(int argc, char *argv[])
struct got_repository *repo = NULL;
const char *errstr;
char *cwd = NULL;
- int ch, nop = 0, strip = -1;
+ int ch, nop = 0, strip = -1, reverse = 0;
int patchfd;
- while ((ch = getopt(argc, argv, "np:")) != -1) {
+ while ((ch = getopt(argc, argv, "np:R")) != -1) {
switch (ch) {
case 'n':
nop = 1;
@@ -7257,6 +7257,9 @@ cmd_patch(int argc, char *argv[])
errx(1, "pathname strip count is %s: %s",
errstr, optarg);
break;
+ case 'R':
+ reverse = 1;
+ break;
default:
usage_patch();
/* NOTREACHED */
@@ -7304,7 +7307,7 @@ cmd_patch(int argc, char *argv[])
err(1, "pledge");
#endif
- error = got_patch(patchfd, worktree, repo, nop, strip,
+ error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
&patch_progress, NULL, check_cancelled, NULL);
done:
diff --git a/include/got_patch.h b/include/got_patch.h
index 62c76b0..bc2e95b 100644
--- a/include/got_patch.h
+++ b/include/got_patch.h
@@ -32,4 +32,4 @@ typedef const struct got_error *(*got_patch_progress_cb)(void *,
*/
const struct got_error *
got_patch(int, struct got_worktree *, struct got_repository *, int, int,
- got_patch_progress_cb, void *, got_cancel_cb, void *);
+ int, got_patch_progress_cb, void *, got_cancel_cb, void *);
diff --git a/lib/patch.c b/lib/patch.c
index f34e561..9a66825 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -688,10 +688,39 @@ done:
return err;
}
+static void
+reverse_patch(struct got_patch *p)
+{
+ struct got_patch_hunk *h;
+ size_t i;
+ long tmp;
+
+ STAILQ_FOREACH(h, &p->head, entries) {
+ tmp = h->old_from;
+ h->old_from = h->new_from;
+ h->new_from = tmp;
+
+ tmp = h->old_lines;
+ h->old_lines = h->new_lines;
+ h->new_lines = tmp;
+
+ tmp = h->old_nonl;
+ h->old_nonl = h->new_nonl;
+ h->new_nonl = tmp;
+
+ for (i = 0; i < h->len; ++i) {
+ if (*h->lines[i] == '+')
+ *h->lines[i] = '-';
+ else if (*h->lines[i] == '-')
+ *h->lines[i] = '+';
+ }
+ }
+}
+
const struct got_error *
got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
- int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg,
- got_cancel_cb cancel_cb, void *cancel_arg)
+ int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
+ void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_fileindex *fileindex = NULL;
@@ -750,6 +779,9 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
if (err || done)
break;
+ if (reverse)
+ reverse_patch(&p);
+
err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
&newpath, worktree, repo, fileindex);
if (err == NULL)
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 5b848e8..4398159 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -1399,6 +1399,50 @@ EOF
test_done $testroot $ret
}
+test_patch_reverse() {
+ local testroot=`test_init patch_reverse`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ cat <<EOF > $testroot/wt/patch
+--- alpha
++++ alpha
+@@ -1 +1 @@
+-ALPHA
+\ No newline at end of file
++alpha
+EOF
+
+ (cd $testroot/wt && got patch -R patch) > $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo "M alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo -n ALPHA > $testroot/wt/alpha.expected
+ cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
+ fi
+ test_done $testroot $ret
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -1423,3 +1467,4 @@ run_test test_patch_strip
run_test test_patch_relative_paths
run_test test_patch_with_path_prefix
run_test test_patch_relpath_with_path_prefix
+run_test test_patch_reverse