add a dry-run/nop mode for got patch with lots of help from stsp for the manpage bits!
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
diff --git a/got/got.1 b/got/got.1
index 6ecd586..1109e3c 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1285,7 +1285,7 @@ option)
.El
.El
.Tg pa
-.It Cm patch Op Ar patchfile
+.It Cm patch Oo Fl n Oc Op Ar patchfile
.Dl Pq alias: Cm pa
Apply changes from
.Ar patchfile
@@ -1338,6 +1338,19 @@ Such changes can be viewed with
and can be reverted with
.Cm got revert
if needed.
+.Pp
+The options for
+.Cm got patch
+are as follows:
+.Bl -tag -width Ds
+.It Fl n
+Do not make any modifications to the work tree.
+This can be used to check whether a patch would apply without issues.
+If the
+.Ar patchfile
+contains diffs that affect the same file multiple times the results
+displayed may be incorrect.
+.El
.Tg rv
.It Cm revert Oo Fl p Oc Oo Fl F Ar response-script Oc Oo Fl R Oc Ar path ...
.Dl Pq alias: Cm rv
diff --git a/got/got.c b/got/got.c
index 4bcea52..d595de1 100644
--- a/got/got.c
+++ b/got/got.c
@@ -7132,7 +7132,7 @@ done:
__dead static void
usage_patch(void)
{
- fprintf(stderr, "usage: %s patch [patchfile]\n",
+ fprintf(stderr, "usage: %s patch [-n] [patchfile]\n",
getprogname());
exit(1);
}
@@ -7192,11 +7192,14 @@ cmd_patch(int argc, char *argv[])
struct got_worktree *worktree = NULL;
struct got_repository *repo = NULL;
char *cwd = NULL;
- int ch;
+ int ch, nop = 0;
int patchfd;
- while ((ch = getopt(argc, argv, "")) != -1) {
+ while ((ch = getopt(argc, argv, "n")) != -1) {
switch (ch) {
+ case 'n':
+ nop = 1;
+ break;
default:
usage_patch();
/* NOTREACHED */
@@ -7244,7 +7247,7 @@ cmd_patch(int argc, char *argv[])
err(1, "pledge");
#endif
- error = got_patch(patchfd, worktree, repo, &print_remove_status,
+ error = got_patch(patchfd, worktree, repo, nop, &print_remove_status,
NULL, &add_progress, NULL, check_cancelled, NULL);
done:
diff --git a/include/got_patch.h b/include/got_patch.h
index 448bb17..5f28ffc 100644
--- a/include/got_patch.h
+++ b/include/got_patch.h
@@ -21,6 +21,6 @@
* The patch file descriptor *must* be seekable.
*/
const struct got_error *
-got_patch(int, struct got_worktree *, struct got_repository *,
+got_patch(int, struct got_worktree *, struct got_repository *, int,
got_worktree_delete_cb, void *, got_worktree_checkout_cb, void *,
got_cancel_cb, void *);
diff --git a/lib/patch.c b/lib/patch.c
index 71695e6..6e5ec0f 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -66,6 +66,7 @@ struct got_patch_hunk {
};
struct got_patch {
+ int nop;
char *old;
char *new;
STAILQ_HEAD(, got_patch_hunk) head;
@@ -399,6 +400,8 @@ patch_file(struct got_patch *p, const char *path, FILE *tmp)
h = STAILQ_FIRST(&p->head);
if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
return got_error(GOT_ERR_PATCH_MALFORMED);
+ if (p->nop)
+ return NULL;
for (i = 0; i < h->len; ++i) {
if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
return got_error_from_errno("fprintf");
@@ -420,7 +423,8 @@ patch_file(struct got_patch *p, const char *path, FILE *tmp)
err = locate_hunk(orig, h, &pos, &lineno);
if (err != NULL)
goto done;
- err = copy(tmp, orig, copypos, pos);
+ if (!p->nop)
+ err = copy(tmp, orig, copypos, pos);
if (err != NULL)
goto done;
copypos = pos;
@@ -446,7 +450,8 @@ patch_file(struct got_patch *p, const char *path, FILE *tmp)
if (err != NULL)
goto done;
- err = apply_hunk(tmp, h, &lineno);
+ if (!p->nop)
+ err = apply_hunk(tmp, h, &lineno);
if (err != NULL)
goto done;
@@ -465,7 +470,7 @@ patch_file(struct got_patch *p, const char *path, FILE *tmp)
err = got_error_from_errno("fstat");
else if (sb.st_size != copypos)
err = got_error(GOT_ERR_PATCH_DONT_APPLY);
- } else if (!feof(orig))
+ } else if (!p->nop && !feof(orig))
err = copy(tmp, orig, copypos, -1);
done:
@@ -599,13 +604,17 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
goto done;
}
- err = got_opentemp_named(&tmppath, &tmp, template);
+ if (!p->nop)
+ err = got_opentemp_named(&tmppath, &tmp, template);
if (err)
goto done;
err = patch_file(p, oldpath, tmp);
if (err)
goto done;
+ if (p->nop)
+ goto done;
+
if (p->old != NULL && p->new == NULL) {
err = got_worktree_schedule_delete(worktree, &oldpaths,
0, NULL, delete_cb, delete_arg, repo, 0, 0);
@@ -645,7 +654,7 @@ done:
const struct got_error *
got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
- got_worktree_delete_cb delete_cb, void *delete_arg,
+ int nop, got_worktree_delete_cb delete_cb, void *delete_arg,
got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
void *cancel_arg)
{
@@ -695,6 +704,7 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
if (err || done)
break;
+ p.nop = nop;
err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
add_cb, add_arg, cancel_cb, cancel_arg);
patch_free(&p);
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 9a300a8..41f290c 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -914,6 +914,59 @@ EOF
test_done $testroot $ret
}
+test_patch_nop() {
+ local testroot=`test_init patch_nop`
+
+ 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
++cafe alpha
+--- beta
++++ /dev/null
+@@ -1 +0,0 @@
+-beta
+--- gamma/delta
++++ gamma/delta.new
+@@ -1 +1 @@
+-delta
++delta updated and renamed!
+EOF
+
+ (cd $testroot/wt && got patch -n patch)
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # remove the patch to avoid the ? entry
+ rm $testroot/wt/patch
+
+ (cd $testroot/wt && got status) > $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo -n > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done $testroot $ret
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -928,3 +981,4 @@ run_test test_patch_no_patch
run_test test_patch_equals_for_context
run_test test_patch_rename
run_test test_patch_illegal_status
+run_test test_patch_nop