got patch: use diff3 to merge the changes Parse the "blob -" metadata in diffs produced by 'got diff' and use the original file for patching. Then, use the diff3 with the current version of the file to merge the differences. This solves many failures automagically or at least turns them into a conflict. ok/improvements 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 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index af9557d..8b9f4c0 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -613,6 +613,7 @@ struct got_imsg_patch {
int git;
char old[PATH_MAX];
char new[PATH_MAX];
+ char blob[41];
};
/*
diff --git a/lib/patch.c b/lib/patch.c
index 0a98afc..9fbe217 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -42,12 +42,15 @@
#include "got_reference.h"
#include "got_cancel.h"
#include "got_worktree.h"
+#include "got_repository.h"
#include "got_opentemp.h"
#include "got_patch.h"
#include "got_lib_delta.h"
+#include "got_lib_diff.h"
#include "got_lib_object.h"
#include "got_lib_privsep.h"
+#include "got_lib_sha1.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -70,6 +73,7 @@ STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
struct got_patch {
char *old;
char *new;
+ char blob[41];
struct got_patch_hunk_head head;
};
@@ -180,11 +184,14 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
memcpy(&patch, imsg.data, sizeof(patch));
if (patch.old[sizeof(patch.old)-1] != '\0' ||
- patch.new[sizeof(patch.new)-1] != '\0') {
+ patch.new[sizeof(patch.new)-1] != '\0' ||
+ patch.blob[sizeof(patch.blob)-1] != '\0') {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
+ strlcpy(p->blob, patch.blob, sizeof(p->blob));
+
/* automatically set strip=1 for git-style diffs */
if (strip == -1 && patch.git &&
(*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
@@ -569,18 +576,75 @@ patch_add(void *arg, unsigned char status, const char *path)
}
static const struct got_error *
-apply_patch(struct got_worktree *worktree, struct got_repository *repo,
- struct got_fileindex *fileindex, const char *old, const char *new,
- struct got_patch *p, int nop, struct patch_args *pa,
- got_cancel_cb cancel_cb, void *cancel_arg)
+open_blob(char **path, FILE **fp, const char *blobid,
+ struct got_repository *repo)
{
const struct got_error *err = NULL;
- int file_renamed = 0;
+ struct got_blob_object *blob = NULL;
+ struct got_object_id id;
+
+ *fp = NULL;
+ *path = NULL;
+
+ if (!got_parse_sha1_digest(id.sha1, blobid))
+ return got_error(GOT_ERR_BAD_OBJ_ID_STR);
+
+ err = got_object_open_as_blob(&blob, repo, &id, 8192);
+ if (err)
+ goto done;
+
+ err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
+ if (err)
+ goto done;
+
+ err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
+ if (err)
+ goto done;
+
+done:
+ if (blob)
+ got_object_blob_close(blob);
+ if (err) {
+ if (*fp != NULL)
+ fclose(*fp);
+ if (*path != NULL)
+ unlink(*path);
+ free(*path);
+ *fp = NULL;
+ *path = NULL;
+ }
+ return err;
+}
+
+static const struct got_error *
+apply_patch(int *overlapcnt, struct got_worktree *worktree,
+ struct got_repository *repo, struct got_fileindex *fileindex,
+ const char *old, const char *new, struct got_patch *p, int nop,
+ struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err = NULL;
+ int do_merge = 0, file_renamed = 0;
+ char *oldlabel = NULL, *newlabel = NULL;
char *oldpath = NULL, *newpath = NULL;
char *tmppath = NULL, *template = NULL, *parent = NULL;
- FILE *oldfile = NULL, *tmp = NULL;
+ char *apath = NULL, *mergepath = NULL;
+ FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
+ int outfd;
+ const char *outpath;
mode_t mode = GOT_DEFAULT_FILE_MODE;
+ *overlapcnt = 0;
+
+ /* don't run the diff3 merge on creations/deletions */
+ if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
+ err = open_blob(&apath, &afile, p->blob, repo);
+ if (err && err->code != GOT_ERR_NOT_REF)
+ return err;
+ else if (err == NULL)
+ do_merge = 1;
+ err = NULL;
+ }
+
if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
old) == -1) {
err = got_error_from_errno("asprintf");
@@ -606,13 +670,48 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
goto done;
}
- err = got_opentemp_named(&tmppath, &tmp, template);
+ err = got_opentemp_named(&tmppath, &tmpfile, template);
if (err)
goto done;
- err = patch_file(p, oldfile, tmp, &mode);
+ outpath = tmppath;
+ outfd = fileno(tmpfile);
+ err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
if (err)
goto done;
+ if (do_merge) {
+ if (fseeko(afile, 0, SEEK_SET) == -1 ||
+ fseeko(oldfile, 0, SEEK_SET) == -1 ||
+ fseeko(tmpfile, 0, SEEK_SET) == -1) {
+ err = got_error_from_errno("fseeko");
+ goto done;
+ }
+
+ if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
+ err = got_error_from_errno("asprintf");
+ oldlabel = NULL;
+ goto done;
+ }
+
+ if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
+ err = got_error_from_errno("asprintf");
+ newlabel = NULL;
+ goto done;
+ }
+
+ err = got_opentemp_named(&mergepath, &mergefile, template);
+ if (err)
+ goto done;
+ outpath = mergepath;
+ outfd = fileno(mergefile);
+
+ err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
+ oldfile, oldlabel, p->blob, newlabel,
+ GOT_DIFF_ALGORITHM_PATIENCE);
+ if (err)
+ goto done;
+ }
+
if (nop)
goto done;
@@ -622,14 +721,14 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
goto done;
}
- if (fchmod(fileno(tmp), mode) == -1) {
+ if (fchmod(outfd, mode) == -1) {
err = got_error_from_errno2("chmod", tmppath);
goto done;
}
- if (rename(tmppath, newpath) == -1) {
+ if (rename(outpath, newpath) == -1) {
if (errno != ENOENT) {
- err = got_error_from_errno3("rename", tmppath,
+ err = got_error_from_errno3("rename", outpath,
newpath);
goto done;
}
@@ -640,8 +739,8 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
err = got_path_mkdir(parent);
if (err != NULL)
goto done;
- if (rename(tmppath, newpath) == -1) {
- err = got_error_from_errno3("rename", tmppath,
+ if (rename(outpath, newpath) == -1) {
+ err = got_error_from_errno3("rename", outpath,
newpath);
goto done;
}
@@ -661,21 +760,40 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
fileindex, patch_add, pa);
if (err)
unlink(newpath);
- } else
+ } else if (*overlapcnt != 0)
+ err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
+ else
err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
done:
free(parent);
free(template);
+
if (tmppath != NULL)
unlink(tmppath);
- if (tmp != NULL && fclose(tmp) == EOF && err == NULL)
+ if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
err = got_error_from_errno("fclose");
free(tmppath);
+
free(oldpath);
if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
err = got_error_from_errno("fclose");
+
+ if (apath != NULL)
+ unlink(apath);
+ if (afile != NULL && fclose(afile) == EOF && err == NULL)
+ err = got_error_from_errno("fclose");
+ free(apath);
+
+ if (mergepath != NULL)
+ unlink(mergepath);
+ if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
+ err = got_error_from_errno("fclose");
+ free(mergepath);
+
free(newpath);
+ free(oldlabel);
+ free(newlabel);
return err;
}
@@ -719,7 +837,7 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
char *oldpath, *newpath;
struct imsgbuf *ibuf;
int imsg_fds[2] = {-1, -1};
- int done = 0, failed = 0;
+ int overlapcnt, done = 0, failed = 0;
pid_t pid;
ibuf = calloc(1, sizeof(*ibuf));
@@ -778,8 +896,9 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
&newpath, worktree, repo, fileindex);
if (err == NULL)
- err = apply_patch(worktree, repo, fileindex, oldpath,
- newpath, &p, nop, &pa, cancel_cb, cancel_arg);
+ err = apply_patch(&overlapcnt, worktree, repo,
+ fileindex, oldpath, newpath, &p, nop, &pa,
+ cancel_cb, cancel_arg);
if (err != NULL) {
failed = 1;
/* recoverable errors */
@@ -791,6 +910,8 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
err = report_progress(&pa, p.old, p.new,
GOT_STATUS_CANNOT_UPDATE, NULL);
}
+ if (overlapcnt != 0)
+ failed = 1;
free(oldpath);
free(newpath);
diff --git a/libexec/got-read-patch/got-read-patch.c b/libexec/got-read-patch/got-read-patch.c
index 23212ab..ba9f841 100644
--- a/libexec/got-read-patch/got-read-patch.c
+++ b/libexec/got-read-patch/got-read-patch.c
@@ -56,11 +56,12 @@
#include "got_lib_delta.h"
#include "got_lib_object.h"
#include "got_lib_privsep.h"
+#include "got_lib_sha1.h"
struct imsgbuf ibuf;
static const struct got_error *
-send_patch(const char *oldname, const char *newname, int git)
+send_patch(const char *oldname, const char *newname, const char *blob, int git)
{
struct got_imsg_patch p;
@@ -72,9 +73,11 @@ send_patch(const char *oldname, const char *newname, int git)
if (newname != NULL)
strlcpy(p.new, newname, sizeof(p.new));
+ if (blob != NULL)
+ strlcpy(p.blob, blob, sizeof(p.blob));
+
p.git = git;
- if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
- &p, sizeof(p)) == -1)
+ if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
return NULL;
}
@@ -123,10 +126,31 @@ filename(const char *at, char **name)
}
static const struct got_error *
+blobid(const char *line, char **blob)
+{
+ uint8_t digest[SHA1_DIGEST_LENGTH];
+ size_t len;
+
+ *blob = NULL;
+
+ len = strspn(line, "0123456789abcdefABCDEF");
+ if ((*blob = strndup(line, len)) == NULL)
+ return got_error_from_errno("strndup");
+
+ if (!got_parse_sha1_digest(digest, *blob)) {
+ /* silently ignore invalid blob ids */
+ free(*blob);
+ *blob = NULL;
+ }
+ return NULL;
+}
+
+static const struct got_error *
find_patch(int *done, FILE *fp)
{
const struct got_error *err = NULL;
char *old = NULL, *new = NULL;
+ char *blob = NULL;
char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
@@ -147,13 +171,19 @@ find_patch(int *done, FILE *fp)
} else if (!strncmp(line, "+++ ", 4)) {
free(new);
err = filename(line+4, &new);
+ } else if (!git && !strncmp(line, "blob - ", 7)) {
+ free(blob);
+ err = blobid(line + 7, &blob);
} else if (rename && !strncmp(line, "rename to ", 10)) {
free(new);
err = filename(line + 10, &new);
} else if (git && !strncmp(line, "similarity index 100%", 21))
rename = 1;
- else if (!strncmp(line, "diff --git a/", 13))
+ else if (!strncmp(line, "diff --git a/", 13)) {
git = 1;
+ free(blob);
+ blob = NULL;
+ }
if (err)
break;
@@ -165,7 +195,7 @@ find_patch(int *done, FILE *fp)
*/
if (rename && old != NULL && new != NULL) {
*done = 1;
- err = send_patch(old, new, git);
+ err = send_patch(old, new, blob, git);
break;
}
@@ -175,7 +205,7 @@ find_patch(int *done, FILE *fp)
(!create && old == NULL))
err = got_error(GOT_ERR_PATCH_MALFORMED);
else
- err = send_patch(old, new, git);
+ err = send_patch(old, new, blob, git);
if (err)
break;
@@ -189,6 +219,7 @@ find_patch(int *done, FILE *fp)
free(old);
free(new);
+ free(blob);
free(line);
if (ferror(fp) && err == NULL)
err = got_error_from_errno("getline");
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index c1e703a..6c9ee03 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -1443,6 +1443,177 @@ EOF
test_done $testroot $ret
}
+test_patch_merge_simple() {
+ local testroot=`test_init patch_merge_simple`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 > $testroot/wt/numbers
+ (cd $testroot/wt && got add numbers && got commit -m +numbers) \
+ > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 | sed 's/4/four/g' > $testroot/wt/numbers
+
+ (cd $testroot/wt && got diff > $testroot/old.diff \
+ && got revert numbers) >/dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 | sed 's/6/six/g' > $testroot/wt/numbers
+ (cd $testroot/wt && got commit -m 'edit numbers') \
+ > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ (cd $testroot/wt && got patch $testroot/old.diff) \
+ 2>&1 > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
+ cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
+ fi
+ test_done $testroot $ret
+}
+
+test_patch_merge_conflict() {
+ local testroot=`test_init patch_merge_conflict`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 > $testroot/wt/numbers
+ (cd $testroot/wt && got add numbers && got commit -m +numbers) \
+ > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 | sed 's/6/six/g' > $testroot/wt/numbers
+
+ (cd $testroot/wt && got diff > $testroot/old.diff \
+ && got revert numbers) >/dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ jot 10 | sed 's/6/3+3/g' > $testroot/wt/numbers
+ (cd $testroot/wt && got commit -m 'edit numbers') \
+ > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ (cd $testroot/wt && got patch $testroot/old.diff) \
+ >/dev/null 2>&1
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ echo "got patch merged a diff that should conflict" >&2
+ test_done $testroot 0
+ return 1
+ fi
+
+ # XXX: prefixing every line with a tab otherwise got thinks
+ # the file has conflicts in it.
+ cat <<-EOF > $testroot/wt/numbers.expected
+ 1
+ 2
+ 3
+ 4
+ 5
+ <<<<<<< --- numbers
+ six
+ ||||||| f00c965d8307308469e537302baa73048488f162
+ 6
+ =======
+ 3+3
+ >>>>>>> +++ numbers
+ 7
+ 8
+ 9
+ 10
+EOF
+
+ cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
+ fi
+ test_done $testroot $ret
+}
+
+test_patch_merge_unknown_blob() {
+ local testroot=`test_init patch_merge_unknown_blob`
+
+ 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
+I've got a
+blob - aaaabbbbccccddddeeeeffff0000111122223333
+and also a
+blob + 0000111122223333444455556666888899990000
+for this dummy diff
+--- alpha
++++ alpha
+@@ -1 +1 @@
+-alpha
++ALPHA
+will it work?
+EOF
+
+ (cd $testroot/wt/ && got patch 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
+ fi
+ test_done $testroot $ret
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -1468,3 +1639,6 @@ 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
+run_test test_patch_merge_simple
+run_test test_patch_merge_conflict
+run_test test_patch_merge_unknown_blob