add an xfail test for a cherrypick bug where unrelated changes get merged The test case I am using here is a bit large but I could not yet find a way to make it smaller. If someone knows a smaller test case, please let me know. naddy caught a small bug in the new test code I had written, thanks!
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
diff --git a/regress/cmdline/cherrypick.sh b/regress/cmdline/cherrypick.sh
index a11bcd4..516a404 100755
--- a/regress/cmdline/cherrypick.sh
+++ b/regress/cmdline/cherrypick.sh
@@ -861,6 +861,341 @@ test_cherrypick_conflict_no_eol2() {
test_done "$testroot" "$ret"
}
+test_cherrypick_unrelated_changes() {
+ local testroot=`test_init cherrypick_unrelated_changes`
+
+ # Sorry about the large HERE document but I have not found
+ # a smaller reproduction recipe yet...
+ cat > $testroot/repo/reference.c <<EOF
+const struct got_error *
+got_ref_alloc(struct got_reference **ref, const char *name,
+ struct got_object_id *id)
+{
+ if (!is_valid_ref_name(name))
+ return got_error_path(name, GOT_ERR_BAD_REF_NAME);
+
+ return alloc_ref(ref, name, id, 0);
+}
+
+static const struct got_error *
+parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
+ const char *line)
+{
+ struct got_object_id id;
+ const char *name;
+
+ *ref = NULL;
+
+ if (line[0] == '#' || line[0] == '^')
+ return NULL;
+
+ if (!got_parse_sha1_digest(id.sha1, line))
+ return got_error(GOT_ERR_BAD_REF_DATA);
+
+ if (abs_refname) {
+ if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
+ return NULL;
+ name = abs_refname;
+ } else
+ name = line + SHA1_DIGEST_STRING_LENGTH;
+
+ return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
+}
+
+static const struct got_error *
+open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
+ int nsubdirs, const char *refname)
+{
+ const struct got_error *err = NULL;
+ char *abs_refname;
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
+ int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
+
+ *ref = NULL;
+
+ if (ref_is_absolute)
+ abs_refname = (char *)refname;
+ do {
+ linelen = getline(&line, &linesize, f);
+ if (linelen == -1) {
+ if (feof(f))
+ break;
+ err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
+ break;
+ }
+ if (linelen > 0 && line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
+ for (i = 0; i < nsubdirs; i++) {
+ if (!ref_is_absolute &&
+ asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
+ refname) == -1)
+ return got_error_from_errno("asprintf");
+ err = parse_packed_ref_line(ref, abs_refname, line);
+ if (!ref_is_absolute)
+ free(abs_refname);
+ if (err || *ref != NULL)
+ break;
+ }
+ if (err)
+ break;
+ } while (*ref == NULL);
+ free(line);
+
+ return err;
+}
+
+static const struct got_error *
+open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
+ const char *name, int lock)
+{
+ const struct got_error *err = NULL;
+ char *path = NULL;
+ char *absname = NULL;
+ int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
+ int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
+
+ *ref = NULL;
+
+ if (ref_is_absolute || ref_is_well_known) {
+ if (asprintf(&path, "%s/%s", path_refs, name) == -1)
+ return got_error_from_errno("asprintf");
+ absname = (char *)name;
+ } else {
+ if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
+ subdir[0] ? "/" : "", name) == -1)
+ return got_error_from_errno("asprintf");
+
+ if (asprintf(&absname, "refs/%s%s%s",
+ subdir, subdir[0] ? "/" : "", name) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ }
+
+ err = parse_ref_file(ref, name, absname, path, lock);
+done:
+ if (!ref_is_absolute && !ref_is_well_known)
+ free(absname);
+ free(path);
+ return err;
+}
+
+const struct got_error *
+got_ref_open(struct got_reference **ref, struct got_repository *repo,
+ const char *refname, int lock)
+{
+ const struct got_error *err = NULL;
+ char *path_refs = NULL;
+ const char *subdirs[] = {
+ GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
+ };
+ size_t i;
+ int well_known = is_well_known_ref(refname);
+ struct got_lockfile *lf = NULL;
+
+ *ref = NULL;
+
+ path_refs = get_refs_dir_path(repo, refname);
+ if (path_refs == NULL) {
+ err = got_error_from_errno2("get_refs_dir_path", refname);
+ goto done;
+ }
+
+ if (well_known) {
+ err = open_ref(ref, path_refs, "", refname, lock);
+ } else {
+ char *packed_refs_path;
+ FILE *f;
+
+ /* Search on-disk refs before packed refs! */
+ for (i = 0; i < nitems(subdirs); i++) {
+ err = open_ref(ref, path_refs, subdirs[i], refname,
+ lock);
+ if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
+ goto done;
+ }
+
+ packed_refs_path = got_repo_get_path_packed_refs(repo);
+ if (packed_refs_path == NULL) {
+ err = got_error_from_errno(
+ "got_repo_get_path_packed_refs");
+ goto done;
+ }
+
+ if (lock) {
+ err = got_lockfile_lock(&lf, packed_refs_path);
+ if (err)
+ goto done;
+ }
+ f = fopen(packed_refs_path, "rb");
+ free(packed_refs_path);
+ if (f != NULL) {
+ err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
+ refname);
+ if (!err) {
+ if (fclose(f) == EOF) {
+ err = got_error_from_errno("fclose");
+ got_ref_close(*ref);
+ *ref = NULL;
+ } else if (*ref)
+ (*ref)->lf = lf;
+ }
+ }
+ }
+done:
+ if (!err && *ref == NULL)
+ err = got_error_not_ref(refname);
+ if (err && lf)
+ got_lockfile_unlock(lf);
+ free(path_refs);
+ return err;
+}
+
+struct got_reference *
+got_ref_dup(struct got_reference *ref)
+{
+ struct got_reference *ret;
+
+ ret = calloc(1, sizeof(*ret));
+ if (ret == NULL)
+ return NULL;
+
+ ret->flags = ref->flags;
+ if (ref->flags & GOT_REF_IS_SYMBOLIC) {
+ ret->ref.symref.name = strdup(ref->ref.symref.name);
+ if (ret->ref.symref.name == NULL) {
+ free(ret);
+ return NULL;
+ }
+ ret->ref.symref.ref = strdup(ref->ref.symref.ref);
+ if (ret->ref.symref.ref == NULL) {
+ free(ret->ref.symref.name);
+ free(ret);
+ return NULL;
+ }
+ } else {
+ ret->ref.ref.name = strdup(ref->ref.ref.name);
+ if (ret->ref.ref.name == NULL) {
+ free(ret);
+ return NULL;
+ }
+ memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
+ sizeof(ret->ref.ref.sha1));
+ }
+
+ return ret;
+}
+
+const struct got_error *
+got_reflist_entry_dup(struct got_reflist_entry **newp,
+ struct got_reflist_entry *re)
+{
+ const struct got_error *err = NULL;
+ struct got_reflist_entry *new;
+
+ *newp = NULL;
+
+ new = malloc(sizeof(*new));
+ if (new == NULL)
+ return got_error_from_errno("malloc");
+
+ new->ref = got_ref_dup(re->ref);
+ if (new->ref == NULL) {
+ err = got_error_from_errno("got_ref_dup");
+ free(new);
+ return err;
+ }
+
+ *newp = new;
+ return NULL;
+}
+
+void
+got_ref_list_free(struct got_reflist_head *refs)
+{
+ struct got_reflist_entry *re;
+
+ while ((re = TAILQ_FIRST(refs))) {
+ TAILQ_REMOVE(refs, re, entry);
+ free(re);
+ }
+
+}
+EOF
+ (cd $testroot/repo && git add reference.c)
+ git_commit $testroot/repo -m "added reference.c file"
+ local base_commit=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ ed -s $testroot/repo/reference.c <<EOF
+91a
+ if (!is_valid_ref_name(name))
+ return got_error_path(name, GOT_ERR_BAD_REF_NAME);
+
+.
+w
+q
+EOF
+ git_commit $testroot/repo -m "added lines on newbranch"
+ local branch_rev1=`git_show_head $testroot/repo`
+
+ ed -s $testroot/repo/reference.c <<EOF
+255a
+ got_ref_close(re->ref);
+.
+w
+q
+EOF
+ git_commit $testroot/repo -m "more lines on newbranch"
+
+ local branch_rev2=`git_show_head $testroot/repo`
+
+ (cd $testroot/wt && got cherrypick $branch_rev2 > $testroot/stdout)
+
+ echo "G reference.c" > $testroot/stdout.expected
+ echo "Merged commit $branch_rev2" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ cat > $testroot/diff.expected <<EOF
+--- reference.c
++++ reference.c
+@@ -250,6 +250,7 @@ got_ref_list_free(struct got_reflist_head *refs)
+
+ while ((re = TAILQ_FIRST(refs))) {
+ TAILQ_REMOVE(refs, re, entry);
++ got_ref_close(re->ref);
+ free(re);
+ }
+
+EOF
+ (cd $testroot/wt && got diff |
+ egrep -v '^(diff|blob|file)' > $testroot/diff)
+ cmp -s $testroot/diff.expected $testroot/diff
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ #diff -u $testroot/diff.expected $testroot/diff
+ ret="xfail cherrypick results in unexpected diff"
+ fi
+
+ test_done "$testroot" "$ret"
+}
+
test_parseargs "$@"
run_test test_cherrypick_basic
run_test test_cherrypick_root_commit
@@ -873,3 +1208,4 @@ run_test test_cherrypick_symlink_conflicts
run_test test_cherrypick_with_path_prefix_and_empty_tree
run_test test_cherrypick_conflict_no_eol
run_test test_cherrypick_conflict_no_eol2
+run_test test_cherrypick_unrelated_changes