t / t3310-notes-merge-manual-resolve.shon commit doc: promote "git restore" (80f537f)
   1#!/bin/sh
   2#
   3# Copyright (c) 2010 Johan Herland
   4#
   5
   6test_description='Test notes merging with manual conflict resolution'
   7
   8. ./test-lib.sh
   9
  10# Set up a notes merge scenario with different kinds of conflicts
  11test_expect_success 'setup commits' '
  12        test_commit 1st &&
  13        test_commit 2nd &&
  14        test_commit 3rd &&
  15        test_commit 4th &&
  16        test_commit 5th
  17'
  18
  19commit_sha1=$(git rev-parse 1st^{commit})
  20commit_sha2=$(git rev-parse 2nd^{commit})
  21commit_sha3=$(git rev-parse 3rd^{commit})
  22commit_sha4=$(git rev-parse 4th^{commit})
  23commit_sha5=$(git rev-parse 5th^{commit})
  24
  25verify_notes () {
  26        notes_ref="$1"
  27        git -c core.notesRef="refs/notes/$notes_ref" notes |
  28                sort >"output_notes_$notes_ref" &&
  29        test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" &&
  30        git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
  31                >"output_log_$notes_ref" &&
  32        test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
  33}
  34
  35cat <<EOF | sort >expect_notes_x
  366e8e3febca3c2bb896704335cc4d0c34cb2f8715 $commit_sha4
  37e5388c10860456ee60673025345fe2e153eb8cf8 $commit_sha3
  38ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2
  39EOF
  40
  41cat >expect_log_x <<EOF
  42$commit_sha5 5th
  43
  44$commit_sha4 4th
  45x notes on 4th commit
  46
  47$commit_sha3 3rd
  48x notes on 3rd commit
  49
  50$commit_sha2 2nd
  51x notes on 2nd commit
  52
  53$commit_sha1 1st
  54
  55EOF
  56
  57test_expect_success 'setup merge base (x)' '
  58        git config core.notesRef refs/notes/x &&
  59        git notes add -m "x notes on 2nd commit" 2nd &&
  60        git notes add -m "x notes on 3rd commit" 3rd &&
  61        git notes add -m "x notes on 4th commit" 4th &&
  62        verify_notes x
  63'
  64
  65cat <<EOF | sort >expect_notes_y
  66e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4
  675772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3
  68b0a6021ec006d07e80e9b20ec9b444cbd9d560d3 $commit_sha1
  69EOF
  70
  71cat >expect_log_y <<EOF
  72$commit_sha5 5th
  73
  74$commit_sha4 4th
  75y notes on 4th commit
  76
  77$commit_sha3 3rd
  78y notes on 3rd commit
  79
  80$commit_sha2 2nd
  81
  82$commit_sha1 1st
  83y notes on 1st commit
  84
  85EOF
  86
  87test_expect_success 'setup local branch (y)' '
  88        git update-ref refs/notes/y refs/notes/x &&
  89        git config core.notesRef refs/notes/y &&
  90        git notes add -f -m "y notes on 1st commit" 1st &&
  91        git notes remove 2nd &&
  92        git notes add -f -m "y notes on 3rd commit" 3rd &&
  93        git notes add -f -m "y notes on 4th commit" 4th &&
  94        verify_notes y
  95'
  96
  97cat <<EOF | sort >expect_notes_z
  98cff59c793c20bb49a4e01bc06fb06bad642e0d54 $commit_sha4
  99283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2
 1000a81da8956346e19bcb27a906f04af327e03e31b $commit_sha1
 101EOF
 102
 103cat >expect_log_z <<EOF
 104$commit_sha5 5th
 105
 106$commit_sha4 4th
 107z notes on 4th commit
 108
 109$commit_sha3 3rd
 110
 111$commit_sha2 2nd
 112z notes on 2nd commit
 113
 114$commit_sha1 1st
 115z notes on 1st commit
 116
 117EOF
 118
 119test_expect_success 'setup remote branch (z)' '
 120        git update-ref refs/notes/z refs/notes/x &&
 121        git config core.notesRef refs/notes/z &&
 122        git notes add -f -m "z notes on 1st commit" 1st &&
 123        git notes add -f -m "z notes on 2nd commit" 2nd &&
 124        git notes remove 3rd &&
 125        git notes add -f -m "z notes on 4th commit" 4th &&
 126        verify_notes z
 127'
 128
 129# At this point, before merging z into y, we have the following status:
 130#
 131# commit | base/x  | local/y | remote/z | diff from x to y/z
 132# -------|---------|---------|----------|---------------------------
 133# 1st    | [none]  | b0a6021 | 0a81da8  | added     / added (diff)
 134# 2nd    | ceefa67 | [none]  | 283b482  | removed   / changed
 135# 3rd    | e5388c1 | 5772f42 | [none]   | changed   / removed
 136# 4th    | 6e8e3fe | e2bfd06 | cff59c7  | changed   / changed (diff)
 137# 5th    | [none]  | [none]  | [none]   | [none]
 138
 139cat <<EOF | sort >expect_conflicts
 140$commit_sha1
 141$commit_sha2
 142$commit_sha3
 143$commit_sha4
 144EOF
 145
 146cat >expect_conflict_$commit_sha1 <<EOF
 147<<<<<<< refs/notes/m
 148y notes on 1st commit
 149=======
 150z notes on 1st commit
 151>>>>>>> refs/notes/z
 152EOF
 153
 154cat >expect_conflict_$commit_sha2 <<EOF
 155z notes on 2nd commit
 156EOF
 157
 158cat >expect_conflict_$commit_sha3 <<EOF
 159y notes on 3rd commit
 160EOF
 161
 162cat >expect_conflict_$commit_sha4 <<EOF
 163<<<<<<< refs/notes/m
 164y notes on 4th commit
 165=======
 166z notes on 4th commit
 167>>>>>>> refs/notes/z
 168EOF
 169
 170cp expect_notes_y expect_notes_m
 171cp expect_log_y expect_log_m
 172
 173git rev-parse refs/notes/y > pre_merge_y
 174git rev-parse refs/notes/z > pre_merge_z
 175
 176test_expect_success 'merge z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
 177        git update-ref refs/notes/m refs/notes/y &&
 178        git config core.notesRef refs/notes/m &&
 179        test_must_fail git notes merge z >output 2>&1 &&
 180        # Output should point to where to resolve conflicts
 181        test_i18ngrep "\\.git/NOTES_MERGE_WORKTREE" output &&
 182        # Inspect merge conflicts
 183        ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
 184        test_cmp expect_conflicts output_conflicts &&
 185        ( for f in $(cat expect_conflicts); do
 186                test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
 187                exit 1
 188        done ) &&
 189        # Verify that current notes tree (pre-merge) has not changed (m == y)
 190        verify_notes y &&
 191        verify_notes m &&
 192        test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
 193'
 194
 195cat <<EOF | sort >expect_notes_z
 19600494adecf2d9635a02fa431308d67993f853968 $commit_sha4
 197283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2
 1980a81da8956346e19bcb27a906f04af327e03e31b $commit_sha1
 199EOF
 200
 201cat >expect_log_z <<EOF
 202$commit_sha5 5th
 203
 204$commit_sha4 4th
 205z notes on 4th commit
 206
 207More z notes on 4th commit
 208
 209$commit_sha3 3rd
 210
 211$commit_sha2 2nd
 212z notes on 2nd commit
 213
 214$commit_sha1 1st
 215z notes on 1st commit
 216
 217EOF
 218
 219test_expect_success 'change notes in z' '
 220        git notes --ref z append -m "More z notes on 4th commit" 4th &&
 221        verify_notes z
 222'
 223
 224test_expect_success 'cannot do merge w/conflicts when previous merge is unfinished' '
 225        test -d .git/NOTES_MERGE_WORKTREE &&
 226        test_must_fail git notes merge z >output 2>&1 &&
 227        # Output should indicate what is wrong
 228        test_i18ngrep -q "\\.git/NOTES_MERGE_\\* exists" output
 229'
 230
 231# Setup non-conflicting merge between x and new notes ref w
 232
 233cat <<EOF | sort >expect_notes_w
 234ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2
 235f75d1df88cbfe4258d49852f26cfc83f2ad4494b $commit_sha1
 236EOF
 237
 238cat >expect_log_w <<EOF
 239$commit_sha5 5th
 240
 241$commit_sha4 4th
 242
 243$commit_sha3 3rd
 244
 245$commit_sha2 2nd
 246x notes on 2nd commit
 247
 248$commit_sha1 1st
 249w notes on 1st commit
 250
 251EOF
 252
 253test_expect_success 'setup unrelated notes ref (w)' '
 254        git config core.notesRef refs/notes/w &&
 255        git notes add -m "w notes on 1st commit" 1st &&
 256        git notes add -m "x notes on 2nd commit" 2nd &&
 257        verify_notes w
 258'
 259
 260cat <<EOF | sort >expect_notes_w
 2616e8e3febca3c2bb896704335cc4d0c34cb2f8715 $commit_sha4
 262e5388c10860456ee60673025345fe2e153eb8cf8 $commit_sha3
 263ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2
 264f75d1df88cbfe4258d49852f26cfc83f2ad4494b $commit_sha1
 265EOF
 266
 267cat >expect_log_w <<EOF
 268$commit_sha5 5th
 269
 270$commit_sha4 4th
 271x notes on 4th commit
 272
 273$commit_sha3 3rd
 274x notes on 3rd commit
 275
 276$commit_sha2 2nd
 277x notes on 2nd commit
 278
 279$commit_sha1 1st
 280w notes on 1st commit
 281
 282EOF
 283
 284test_expect_success 'can do merge without conflicts even if previous merge is unfinished (x => w)' '
 285        test -d .git/NOTES_MERGE_WORKTREE &&
 286        git notes merge x &&
 287        verify_notes w &&
 288        # Verify that other notes refs has not changed (x and y)
 289        verify_notes x &&
 290        verify_notes y
 291'
 292
 293cat <<EOF | sort >expect_notes_m
 294021faa20e931fb48986ffc6282b4bb05553ac946 $commit_sha4
 2955772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3
 296283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2
 2970a59e787e6d688aa6309e56e8c1b89431a0fc1c1 $commit_sha1
 298EOF
 299
 300cat >expect_log_m <<EOF
 301$commit_sha5 5th
 302
 303$commit_sha4 4th
 304y and z notes on 4th commit
 305
 306$commit_sha3 3rd
 307y notes on 3rd commit
 308
 309$commit_sha2 2nd
 310z notes on 2nd commit
 311
 312$commit_sha1 1st
 313y and z notes on 1st commit
 314
 315EOF
 316
 317test_expect_success 'do not allow mixing --commit and --abort' '
 318        test_must_fail git notes merge --commit --abort
 319'
 320
 321test_expect_success 'do not allow mixing --commit and --strategy' '
 322        test_must_fail git notes merge --commit --strategy theirs
 323'
 324
 325test_expect_success 'do not allow mixing --abort and --strategy' '
 326        test_must_fail git notes merge --abort --strategy theirs
 327'
 328
 329test_expect_success 'finalize conflicting merge (z => m)' '
 330        # Resolve conflicts and finalize merge
 331        cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
 332y and z notes on 1st commit
 333EOF
 334        cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
 335y and z notes on 4th commit
 336EOF
 337        git notes merge --commit &&
 338        # No .git/NOTES_MERGE_* files left
 339        test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
 340        test_must_be_empty output &&
 341        # Merge commit has pre-merge y and pre-merge z as parents
 342        test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
 343        test "$(git rev-parse refs/notes/m^2)" = "$(cat pre_merge_z)" &&
 344        # Merge commit mentions the notes refs merged
 345        git log -1 --format=%B refs/notes/m > merge_commit_msg &&
 346        grep -q refs/notes/m merge_commit_msg &&
 347        grep -q refs/notes/z merge_commit_msg &&
 348        # Merge commit mentions conflicting notes
 349        grep -q "Conflicts" merge_commit_msg &&
 350        ( for sha1 in $(cat expect_conflicts); do
 351                grep -q "$sha1" merge_commit_msg ||
 352                exit 1
 353        done ) &&
 354        # Verify contents of merge result
 355        verify_notes m &&
 356        # Verify that other notes refs has not changed (w, x, y and z)
 357        verify_notes w &&
 358        verify_notes x &&
 359        verify_notes y &&
 360        verify_notes z
 361'
 362
 363cat >expect_conflict_$commit_sha4 <<EOF
 364<<<<<<< refs/notes/m
 365y notes on 4th commit
 366=======
 367z notes on 4th commit
 368
 369More z notes on 4th commit
 370>>>>>>> refs/notes/z
 371EOF
 372
 373cp expect_notes_y expect_notes_m
 374cp expect_log_y expect_log_m
 375
 376git rev-parse refs/notes/y > pre_merge_y
 377git rev-parse refs/notes/z > pre_merge_z
 378
 379test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
 380        git update-ref refs/notes/m refs/notes/y &&
 381        git config core.notesRef refs/notes/m &&
 382        test_must_fail git notes merge z >output 2>&1 &&
 383        # Output should point to where to resolve conflicts
 384        test_i18ngrep "\\.git/NOTES_MERGE_WORKTREE" output &&
 385        # Inspect merge conflicts
 386        ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
 387        test_cmp expect_conflicts output_conflicts &&
 388        ( for f in $(cat expect_conflicts); do
 389                test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
 390                exit 1
 391        done ) &&
 392        # Verify that current notes tree (pre-merge) has not changed (m == y)
 393        verify_notes y &&
 394        verify_notes m &&
 395        test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
 396'
 397
 398test_expect_success 'abort notes merge' '
 399        git notes merge --abort &&
 400        # No .git/NOTES_MERGE_* files left
 401        test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
 402        test_must_be_empty output &&
 403        # m has not moved (still == y)
 404        test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)" &&
 405        # Verify that other notes refs has not changed (w, x, y and z)
 406        verify_notes w &&
 407        verify_notes x &&
 408        verify_notes y &&
 409        verify_notes z
 410'
 411
 412git rev-parse refs/notes/y > pre_merge_y
 413git rev-parse refs/notes/z > pre_merge_z
 414
 415test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
 416        test_must_fail git notes merge z >output 2>&1 &&
 417        # Output should point to where to resolve conflicts
 418        test_i18ngrep "\\.git/NOTES_MERGE_WORKTREE" output &&
 419        # Inspect merge conflicts
 420        ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
 421        test_cmp expect_conflicts output_conflicts &&
 422        ( for f in $(cat expect_conflicts); do
 423                test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
 424                exit 1
 425        done ) &&
 426        # Verify that current notes tree (pre-merge) has not changed (m == y)
 427        verify_notes y &&
 428        verify_notes m &&
 429        test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
 430'
 431
 432cat <<EOF | sort >expect_notes_m
 433304dfb4325cf243025b9957486eb605a9b51c199 $commit_sha5
 434283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2
 4350a59e787e6d688aa6309e56e8c1b89431a0fc1c1 $commit_sha1
 436EOF
 437
 438cat >expect_log_m <<EOF
 439$commit_sha5 5th
 440new note on 5th commit
 441
 442$commit_sha4 4th
 443
 444$commit_sha3 3rd
 445
 446$commit_sha2 2nd
 447z notes on 2nd commit
 448
 449$commit_sha1 1st
 450y and z notes on 1st commit
 451
 452EOF
 453
 454test_expect_success 'add + remove notes in finalized merge (z => m)' '
 455        # Resolve one conflict
 456        cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
 457y and z notes on 1st commit
 458EOF
 459        # Remove another conflict
 460        rm .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
 461        # Remove a D/F conflict
 462        rm .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
 463        # Add a new note
 464        echo "new note on 5th commit" > .git/NOTES_MERGE_WORKTREE/$commit_sha5 &&
 465        # Finalize merge
 466        git notes merge --commit &&
 467        # No .git/NOTES_MERGE_* files left
 468        test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
 469        test_must_be_empty output &&
 470        # Merge commit has pre-merge y and pre-merge z as parents
 471        test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
 472        test "$(git rev-parse refs/notes/m^2)" = "$(cat pre_merge_z)" &&
 473        # Merge commit mentions the notes refs merged
 474        git log -1 --format=%B refs/notes/m > merge_commit_msg &&
 475        grep -q refs/notes/m merge_commit_msg &&
 476        grep -q refs/notes/z merge_commit_msg &&
 477        # Merge commit mentions conflicting notes
 478        grep -q "Conflicts" merge_commit_msg &&
 479        ( for sha1 in $(cat expect_conflicts); do
 480                grep -q "$sha1" merge_commit_msg ||
 481                exit 1
 482        done ) &&
 483        # Verify contents of merge result
 484        verify_notes m &&
 485        # Verify that other notes refs has not changed (w, x, y and z)
 486        verify_notes w &&
 487        verify_notes x &&
 488        verify_notes y &&
 489        verify_notes z
 490'
 491
 492cp expect_notes_y expect_notes_m
 493cp expect_log_y expect_log_m
 494
 495test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
 496        git update-ref refs/notes/m refs/notes/y &&
 497        test_must_fail git notes merge z >output 2>&1 &&
 498        # Output should point to where to resolve conflicts
 499        test_i18ngrep "\\.git/NOTES_MERGE_WORKTREE" output &&
 500        # Inspect merge conflicts
 501        ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
 502        test_cmp expect_conflicts output_conflicts &&
 503        ( for f in $(cat expect_conflicts); do
 504                test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
 505                exit 1
 506        done ) &&
 507        # Verify that current notes tree (pre-merge) has not changed (m == y)
 508        verify_notes y &&
 509        verify_notes m &&
 510        test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
 511'
 512
 513cp expect_notes_w expect_notes_m
 514cp expect_log_w expect_log_m
 515
 516test_expect_success 'reset notes ref m to somewhere else (w)' '
 517        git update-ref refs/notes/m refs/notes/w &&
 518        verify_notes m &&
 519        test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
 520'
 521
 522test_expect_success 'fail to finalize conflicting merge if underlying ref has moved in the meantime (m != NOTES_MERGE_PARTIAL^1)' '
 523        # Resolve conflicts
 524        cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
 525y and z notes on 1st commit
 526EOF
 527        cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
 528y and z notes on 4th commit
 529EOF
 530        # Fail to finalize merge
 531        test_must_fail git notes merge --commit >output 2>&1 &&
 532        # .git/NOTES_MERGE_* must remain
 533        test -f .git/NOTES_MERGE_PARTIAL &&
 534        test -f .git/NOTES_MERGE_REF &&
 535        test -f .git/NOTES_MERGE_WORKTREE/$commit_sha1 &&
 536        test -f .git/NOTES_MERGE_WORKTREE/$commit_sha2 &&
 537        test -f .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
 538        test -f .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
 539        # Refs are unchanged
 540        test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&
 541        test "$(git rev-parse refs/notes/y)" = "$(git rev-parse NOTES_MERGE_PARTIAL^1)" &&
 542        test "$(git rev-parse refs/notes/m)" != "$(git rev-parse NOTES_MERGE_PARTIAL^1)" &&
 543        # Mention refs/notes/m, and its current and expected value in output
 544        test_i18ngrep -q "refs/notes/m" output &&
 545        test_i18ngrep -q "$(git rev-parse refs/notes/m)" output &&
 546        test_i18ngrep -q "$(git rev-parse NOTES_MERGE_PARTIAL^1)" output &&
 547        # Verify that other notes refs has not changed (w, x, y and z)
 548        verify_notes w &&
 549        verify_notes x &&
 550        verify_notes y &&
 551        verify_notes z
 552'
 553
 554test_expect_success 'resolve situation by aborting the notes merge' '
 555        git notes merge --abort &&
 556        # No .git/NOTES_MERGE_* files left
 557        test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
 558        test_must_be_empty output &&
 559        # m has not moved (still == w)
 560        test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&
 561        # Verify that other notes refs has not changed (w, x, y and z)
 562        verify_notes w &&
 563        verify_notes x &&
 564        verify_notes y &&
 565        verify_notes z
 566'
 567
 568cat >expect_notes <<EOF
 569foo
 570bar
 571EOF
 572
 573test_expect_success 'switch cwd before committing notes merge' '
 574        git notes add -m foo HEAD &&
 575        git notes --ref=other add -m bar HEAD &&
 576        test_must_fail git notes merge refs/notes/other &&
 577        (
 578                cd .git/NOTES_MERGE_WORKTREE &&
 579                echo "foo" > $(git rev-parse HEAD) &&
 580                echo "bar" >> $(git rev-parse HEAD) &&
 581                git notes merge --commit
 582        ) &&
 583        git notes show HEAD > actual_notes &&
 584        test_cmp expect_notes actual_notes
 585'
 586
 587test_done