commit 19dd85cbe25c418975d46e00cc13b48e26488715 from: Omar Polo via: Thomas Adam date: Fri Jul 01 21:13:24 2022 UTC got patch: handle git-style diffs for the 3-way merge too tweak and ok stsp@ commit - 8afec5d5388b965430c12e3405765ceb552faefa commit + 19dd85cbe25c418975d46e00cc13b48e26488715 blob - c7465535acc5666625d7b591529f651371547808 blob + 21ea570c756aff230fd0f3e62994dd72a5ccd74f --- lib/patch.c +++ lib/patch.c @@ -584,15 +584,25 @@ open_blob(char **path, FILE **fp, const char *blobid, { const struct got_error *err = NULL; struct got_blob_object *blob = NULL; - struct got_object_id id; + struct got_object_id id, *idptr, *matched_id = NULL; *fp = NULL; *path = NULL; - if (!got_parse_sha1_digest(id.sha1, blobid)) - return got_error(GOT_ERR_BAD_OBJ_ID_STR); + if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) { + err = got_repo_match_object_id(&matched_id, NULL, blobid, + GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */, + repo); + if (err) + return err; + idptr = matched_id; + } else { + if (!got_parse_sha1_digest(id.sha1, blobid)) + return got_error(GOT_ERR_BAD_OBJ_ID_STR); + idptr = &id; + } - err = got_object_open_as_blob(&blob, repo, &id, 8192); + err = got_object_open_as_blob(&blob, repo, idptr, 8192); if (err) goto done; @@ -607,6 +617,8 @@ open_blob(char **path, FILE **fp, const char *blobid, done: if (blob) got_object_blob_close(blob); + if (matched_id != NULL) + free(matched_id); if (err) { if (*fp != NULL) fclose(*fp); @@ -645,7 +657,8 @@ apply_patch(int *overlapcnt, struct got_worktree *work * ignore failures to open this blob, we might have * parsed gibberish. */ - if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) + if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) && + err->code != GOT_ERR_NO_OBJ) return err; else if (err == NULL) do_merge = 1; blob - 6ef560b1f7733ae1a05b7a9de9c80f90453766a4 blob + 3e5d28e8bf764b595e938091addf64eb8753d5ed --- libexec/got-read-patch/got-read-patch.c +++ libexec/got-read-patch/got-read-patch.c @@ -129,7 +129,7 @@ filename(const char *at, char **name) } static const struct got_error * -blobid(const char *line, char **blob) +blobid(const char *line, char **blob, int git) { uint8_t digest[SHA1_DIGEST_LENGTH]; size_t len; @@ -140,7 +140,7 @@ blobid(const char *line, char **blob) if ((*blob = strndup(line, len)) == NULL) return got_error_from_errno("strndup"); - if (!got_parse_sha1_digest(digest, *blob)) { + if (!git && !got_parse_sha1_digest(digest, *blob)) { /* silently ignore invalid blob ids */ free(*blob); *blob = NULL; @@ -176,13 +176,16 @@ find_patch(int *done, FILE *fp) err = filename(line+4, &new); } else if (!git && !strncmp(line, "blob - ", 7)) { free(blob); - err = blobid(line + 7, &blob); + err = blobid(line + 7, &blob, git); } 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 (git && !strncmp(line, "index ", 6)) { + free(blob); + err = blobid(line + 6, &blob, git); + } else if (!strncmp(line, "diff --git a/", 13)) { git = 1; free(commitid); commitid = NULL; @@ -190,10 +193,10 @@ find_patch(int *done, FILE *fp) blob = NULL; } else if (!git && !strncmp(line, "diff ", 5)) { free(commitid); - err = blobid(line + 5, &commitid); + err = blobid(line + 5, &commitid, git); } else if (!git && !strncmp(line, "commit - ", 9)) { free(commitid); - err = blobid(line + 9, &commitid); + err = blobid(line + 9, &commitid, git); } if (err) blob - 9fb1dd39304f1cd716283dd5684a1162ee8f745c blob + 51903218ccbe2f14d00ba02be9bbc8ead7c2f1e4 --- regress/cmdline/patch.sh +++ regress/cmdline/patch.sh @@ -1503,7 +1503,63 @@ test_patch_merge_simple() { ret=$? if [ $ret -ne 0 ]; then diff -u $testroot/wt/numbers $testroot/wt/numbers.expected + fi + test_done $testroot $ret +} + +test_patch_merge_gitdiff() { + local testroot=`test_init patch_merge_gitdiff` + + jot 10 > $testroot/repo/numbers + (cd $testroot/repo && git add numbers && \ + git_commit $testroot/repo -m "nums") + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + jot 10 | sed 's/4/four/g' > $testroot/repo/numbers + (cd $testroot/repo && git diff > $testroot/old.diff) + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + # restore numbers + jot 10 > $testroot/repo/numbers + + jot 10 | sed 's/6/six/g' > $testroot/repo/numbers + (cd $testroot/repo && git add numbers && \ + git_commit $testroot/repo -m "edit") + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + # now work with got: + got checkout $testroot/repo $testroot/wt > /dev/null + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + (cd $testroot/wt && got patch $testroot/old.diff) > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 fi + + echo 'G numbers' > $testroot/stdout.expected + cmp -s $testroot/stdout $testroot/stdout.expected + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout $testroot/stdout.expected + fi test_done $testroot $ret } @@ -1665,7 +1721,36 @@ EOF ret=$? if [ $ret -ne 0 ]; then diff -u $testroot/stdout.expected $testroot/stdout + test_done $testroot $ret + return 1 fi + + # try again with a git-style diff + + cat < $testroot/wt/patch +diff --git a/alpha b/alpha +index 0123456789ab..abcdef012345 100644 +--- a/alpha ++++ b/alpha +@@ -1 +1 @@ +-alpha ++ALPHA +EOF + + (cd $testroot/wt && got revert alpha > /dev/null && 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 } @@ -1695,5 +1780,6 @@ 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_gitdiff run_test test_patch_merge_conflict run_test test_patch_merge_unknown_blob