refactor apply_patch to support renaming files add two helper functions (schedule_add, schedule_del) and move the guts of apply_patch into a new function `patch_file'. This simplifies apply_patch and makes easier to figure out what happens. Then, drop GOT_ERR_PATCH_PATHS_DIFFER since we allow the to rename files.
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
diff --git a/include/got_error.h b/include/got_error.h
index 64f2cb9..11708cc 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -165,8 +165,7 @@
#define GOT_ERR_PATCH_MALFORMED 147
#define GOT_ERR_PATCH_TRUNCATED 148
#define GOT_ERR_PATCH_DONT_APPLY 149
-#define GOT_ERR_PATCH_PATHS_DIFFER 150
-#define GOT_ERR_NO_PATCH 151
+#define GOT_ERR_NO_PATCH 150
static const struct got_error {
int code;
@@ -346,8 +345,6 @@ static const struct got_error {
{ GOT_ERR_PATCH_MALFORMED, "malformed patch" },
{ GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
{ GOT_ERR_PATCH_DONT_APPLY, "patch doesn't apply" },
- { GOT_ERR_PATCH_PATHS_DIFFER, "the paths mentioned in the patch "
- "are different." },
{ GOT_ERR_NO_PATCH, "no patch found" },
};
diff --git a/lib/patch.c b/lib/patch.c
index 957d639..8dc632e 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -381,69 +381,65 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
}
static const struct got_error *
-apply_patch(struct got_worktree *worktree, struct got_repository *repo,
- struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
- got_worktree_checkout_cb add_cb, void *add_arg)
+schedule_add(const char *path, struct got_worktree *worktree,
+ struct got_repository *repo, got_worktree_checkout_cb add_cb,
+ void *add_arg)
{
- const struct got_error *err = NULL;
+ static const struct got_error *err = NULL;
struct got_pathlist_head paths;
struct got_pathlist_entry *pe;
- char *path = NULL, *tmppath = NULL, *template = NULL;
- FILE *orig = NULL, *tmp = NULL;
- struct got_patch_hunk *h;
- size_t i;
- long lineno = 0;
- off_t copypos, pos;
- char *line = NULL;
- size_t linesize = 0;
- ssize_t linelen;
TAILQ_INIT(&paths);
- err = got_worktree_resolve_path(&path, worktree,
- p->new != NULL ? p->new : p->old);
- if (err)
- return err;
err = got_pathlist_insert(&pe, &paths, path, NULL);
- if (err)
- goto done;
+ if (err == NULL)
+ err = got_worktree_schedule_add(worktree, &paths,
+ add_cb, add_arg, repo, 1);
+ got_pathlist_free(&paths);
+ return err;
+}
- if (p->old != NULL && p->new == NULL) {
- /*
- * special case: delete a file. don't try to match
- * the lines but just schedule the removal.
- */
+static const struct got_error *
+schedule_del(const char *path, struct got_worktree *worktree,
+ struct got_repository *repo, got_worktree_delete_cb delete_cb,
+ void *delete_arg)
+{
+ static const struct got_error *err = NULL;
+ struct got_pathlist_head paths;
+ struct got_pathlist_entry *pe;
+
+ TAILQ_INIT(&paths);
+
+ err = got_pathlist_insert(&pe, &paths, path, NULL);
+ if (err == NULL)
err = got_worktree_schedule_delete(worktree, &paths,
0, NULL, delete_cb, delete_arg, repo, 0, 0);
- goto done;
- } else if (p->old != NULL && strcmp(p->old, p->new)) {
- err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
- goto done;
- }
-
- if (asprintf(&template, "%s/got-patch",
- got_worktree_get_root_path(worktree)) == -1) {
- err = got_error_from_errno(template);
- goto done;
- }
+ got_pathlist_free(&paths);
+ return err;
+}
- err = got_opentemp_named(&tmppath, &tmp, template);
- if (err)
- goto done;
+static const struct got_error *
+patch_file(struct got_patch *p, const char *path, FILE *tmp)
+{
+ const struct got_error *err = NULL;
+ struct got_patch_hunk *h;
+ size_t i;
+ long lineno = 0;
+ FILE *orig;
+ off_t copypos, pos;
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
if (p->old == NULL) { /* create */
h = STAILQ_FIRST(&p->head);
- if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
- err = got_error(GOT_ERR_PATCH_MALFORMED);
- goto done;
- }
+ if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
+ return got_error(GOT_ERR_PATCH_MALFORMED);
for (i = 0; i < h->len; ++i) {
- if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
- err = got_error_from_errno("fprintf");
- goto done;
- }
+ if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
+ return got_error_from_errno("fprintf");
}
- goto rename;
+ return err;
}
if ((orig = fopen(path, "r")) == NULL) {
@@ -453,6 +449,9 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
copypos = 0;
STAILQ_FOREACH(h, &p->head, entries) {
+ if (h->lines == NULL)
+ break;
+
tryagain:
err = locate_hunk(orig, h, &pos, &lineno);
if (err != NULL)
@@ -494,40 +493,86 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
}
}
- if (!feof(orig)) {
+ if (!feof(orig))
err = copy(tmp, orig, copypos, -1);
- if (err)
- goto done;
+
+done:
+ if (orig != NULL)
+ fclose(orig);
+ return err;
+}
+
+static const struct got_error *
+apply_patch(struct got_worktree *worktree, struct got_repository *repo,
+ struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
+ got_worktree_checkout_cb add_cb, void *add_arg)
+{
+ const struct got_error *err = NULL;
+ int file_renamed = 0;
+ char *oldpath = NULL, *newpath = NULL;
+ char *tmppath = NULL, *template = NULL;
+ FILE *tmp = NULL;
+
+ err = got_worktree_resolve_path(&oldpath, worktree,
+ p->old != NULL ? p->old : p->new);
+ if (err)
+ goto done;
+
+ err = got_worktree_resolve_path(&newpath, worktree,
+ p->new != NULL ? p->new : p->old);
+ if (err)
+ goto done;
+
+ if (p->old != NULL && p->new == NULL) {
+ /*
+ * special case: delete a file. don't try to match
+ * the lines but just schedule the removal.
+ */
+ err = schedule_del(p->old, worktree, repo, delete_cb,
+ delete_arg);
+ goto done;
}
-rename:
- if (rename(tmppath, path) == -1) {
- err = got_error_from_errno3("rename", tmppath, path);
+ if (asprintf(&template, "%s/got-patch",
+ got_worktree_get_root_path(worktree)) == -1) {
+ err = got_error_from_errno(template);
goto done;
}
- if (p->old == NULL)
- err = got_worktree_schedule_add(worktree, &paths,
- add_cb, add_arg, repo, 1);
+ err = got_opentemp_named(&tmppath, &tmp, template);
+ if (err)
+ goto done;
+ err = patch_file(p, oldpath, tmp);
+ if (err)
+ goto done;
+
+ if (rename(tmppath, newpath) == -1) {
+ err = got_error_from_errno3("rename", tmppath, newpath);
+ goto done;
+ }
+
+ file_renamed = p->old != NULL && strcmp(p->old, p->new);
+ if (file_renamed) {
+ err = schedule_del(oldpath, worktree, repo, delete_cb,
+ delete_arg);
+ if (err == NULL)
+ err = schedule_add(newpath, worktree, repo,
+ add_cb, add_arg);
+ } else if (p->old == NULL)
+ err = schedule_add(newpath, worktree, repo, add_cb,
+ add_arg);
else
- printf("M %s\n", path); /* XXX */
+ printf("M %s\n", oldpath); /* XXX */
+
done:
+ if (err != NULL && (file_renamed || p->old == NULL))
+ unlink(newpath);
free(template);
- if (err != NULL && p->old == NULL && path != NULL)
- unlink(path);
- if (tmp != NULL)
- fclose(tmp);
if (tmppath != NULL)
unlink(tmppath);
free(tmppath);
- if (orig != NULL) {
- if (p->old == NULL && err != NULL)
- unlink(path);
- fclose(orig);
- }
- free(path);
- free(line);
- got_pathlist_free(&paths);
+ free(oldpath);
+ free(newpath);
return err;
}
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 7a5ad36..5916921 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -624,6 +624,112 @@ EOF
test_done $testroot $ret
}
+test_patch_rename() {
+ local testroot=`test_init patch_rename`
+
+ 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
++++ eta
+@@ -0,0 +0,0 @@
+EOF
+
+ echo 'D alpha' > $testroot/stdout.expected
+ echo 'A eta' >> $testroot/stdout.expected
+
+ (cd $testroot/wt && got patch patch) > $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ 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
+
+ if [ -f $testroot/wt/alpha ]; then
+ echo "alpha was not removed" >&2
+ test_done $testroot 1
+ return 1
+ fi
+ if [ ! -f $testroot/wt/eta ]; then
+ echo "eta was not created" >&2
+ test_done $testroot 1
+ return 1
+ fi
+
+ echo alpha > $testroot/wt/eta.expected
+ cmp -s $testroot/wt/eta.expected $testroot/wt/eta
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/wt/eta.expected $testroot/wt/eta
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # revert the changes and try again with a rename + edit
+ (cd $testroot/wt && got revert alpha eta) > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ cat <<EOF > $testroot/wt/patch
+--- alpha
++++ eta
+@@ -1 +1,2 @@
+ alpha
++but now is eta
+EOF
+
+ (cd $testroot/wt && got patch patch) > $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ 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
+
+ if [ -f $testroot/wt/alpha ]; then
+ echo "alpha was not removed" >&2
+ test_done $testroot 1
+ return 1
+ fi
+ if [ ! -f $testroot/wt/eta ]; then
+ echo "eta was not created" >&2
+ test_done $testroot 1
+ return 1
+ fi
+
+ echo alpha > $testroot/wt/eta.expected
+ echo 'but now is eta' >> $testroot/wt/eta.expected
+ cmp -s $testroot/wt/eta.expected $testroot/wt/eta
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/wt/eta.expected $testroot/wt/eta
+ fi
+ test_done $testroot $ret
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -636,3 +742,4 @@ run_test test_patch_dont_apply
run_test test_patch_malformed
run_test test_patch_no_patch
run_test test_patch_equals_for_context
+run_test test_patch_rename