got patch: handle git-style diffs for the 3-way merge too tweak and 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
diff --git a/lib/patch.c b/lib/patch.c
index 9c2a6ee..1bc4c53 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -587,15 +587,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;
@@ -610,6 +620,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);
@@ -648,7 +660,8 @@ apply_patch(int *overlapcnt, struct got_worktree *worktree,
* 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;
diff --git a/libexec/got-read-patch/got-read-patch.c b/libexec/got-read-patch/got-read-patch.c
index 1419169..d6eea51 100644
--- a/libexec/got-read-patch/got-read-patch.c
+++ b/libexec/got-read-patch/got-read-patch.c
@@ -130,7 +130,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;
@@ -141,7 +141,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;
@@ -177,13 +177,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;
@@ -191,10 +194,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)
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 9fb1dd3..5190321 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -1507,6 +1507,62 @@ test_patch_merge_simple() {
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
+}
+
test_patch_merge_conflict() {
local testroot=`test_init patch_merge_conflict`
@@ -1665,6 +1721,35 @@ 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 <<EOF > $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