diff options
-rw-r--r-- | Documentation/git-read-tree.txt | 74 | ||||
-rw-r--r-- | read-tree.c | 49 | ||||
-rwxr-xr-x | t/t1001-read-tree-m-2way.sh | 281 | ||||
-rwxr-xr-x | t/t1002-read-tree-m-u-2way.sh | 307 |
4 files changed, 698 insertions, 13 deletions
diff --git a/Documentation/git-read-tree.txt b/Documentation/git-read-tree.txt index cbde13dba..6440c4b41 100644 --- a/Documentation/git-read-tree.txt +++ b/Documentation/git-read-tree.txt @@ -53,6 +53,80 @@ the stuff that really changed. This is used to avoid unnecessary false hits when "git-diff-files" is run after git-read-tree. + +Two Tree Merge +~~~~~~~~~~~~~~ + +Typically, this is invoked as "git-read-tree -m $H $M", where $H +is the head commit of the current repository, and $M is the head +of a foreign tree, which is simply ahead of $H (i.e. we are in a +fast forward situation). + +When two trees are specified, the user is telling git-read-tree +the following: + + (1) The current index and work tree is derived from $H, but + the user may have local changes in them since $H; + + (2) The user wants to fast-forward to $M. + +In this case, the "git-read-tree -m $H $M" command makes sure +that no local change is lost as the result of this "merge". +Here are the "carry forward" rules: + + I (index) H M Result + ------------------------------------------------------- + 0 nothing nothing nothing (does not happen) + 1 nothing nothing exists use M + 2 nothing exists nothing remove path from cache + 3 nothing exists exists use M + + clean I==H I==M + ------------------ + 4 yes N/A N/A nothing nothing keep index + 5 no N/A N/A nothing nothing keep index + + 6 yes N/A yes nothing exists keep index + 7 no N/A yes nothing exists keep index + 8 yes N/A no nothing exists fail + 9 no N/A no nothing exists fail + + 10 yes yes N/A exists nothing remove path from cache + 11 no yes N/A exists nothing fail + 12 yes no N/A exists nothing fail + 13 no no N/A exists nothing fail + + clean (H=M) + ------ + 14 yes exists exists keep index + 15 no exists exists keep index + + clean I==H I==M (H!=M) + ------------------ + 16 yes no no exists exists fail + 17 no no no exists exists fail + 18 yes no yes exists exists keep index + 19 no no yes exists exists keep index + 20 yes yes no exists exists use M + 21 no yes no exists exists fail + +In all "keep index" cases, the cache entry stays as in the +original index file. If the entry were not up to date, +git-read-tree keeps the copy in the work tree intact when +operating under the -u flag. + +When this form of git-read-tree returns successfully, you can +see what "local changes" you made are carried forward by running +"git-diff-cache --cached $M". Note that this does not +necessarily match "git-diff-cache --cached $H" would have +produced before such a two tree merge. This is because of cases +18 and 19 --- if you already had the changes in $M (e.g. maybe +you picked it up via e-mail in a patch form), "git-diff-cache +--cached $H" would have told you about the change before this +merge, but it would not show in "git-diff-cache --cached $M" +output after two-tree merge. + + 3-Way Merge ~~~~~~~~~~~ Each "index" entry has two bits worth of "stage" state. stage 0 is the diff --git a/read-tree.c b/read-tree.c index 2fb27e974..8eb243212 100644 --- a/read-tree.c +++ b/read-tree.c @@ -155,28 +155,51 @@ static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **ds /* * Two-way merge. * - * The rule is: - * - every current entry has to match the old tree - * - if the current entry matches the new tree, we leave it - * as-is. Otherwise we require that it be up-to-date. + * The rule is to "carry forward" what is in the index without losing + * information across a "fast forward", favoring a successful merge + * over a merge failure when it makes sense. For details of the + * "carry forward" rule, please see <Documentation/git-read-tree.txt>. + * */ static int twoway_merge(struct cache_entry **src, struct cache_entry **dst) { - struct cache_entry *old = src[0]; - struct cache_entry *a = src[1], *b = src[2]; + struct cache_entry *current = src[0]; + struct cache_entry *oldtree = src[1], *newtree = src[2]; if (src[3]) return -1; - if (old) { - if (!a || !same(old, a)) + if (current) { + if ((!oldtree && !newtree) || /* 4 and 5 */ + (!oldtree && newtree && + same(current, newtree)) || /* 6 and 7 */ + (oldtree && newtree && + same(oldtree, newtree)) || /* 14 and 15 */ + (oldtree && newtree && + !same(oldtree, newtree) && /* 18 and 19*/ + same(current, newtree))) { + *dst++ = current; + return 1; + } + else if (oldtree && !newtree && same(current, oldtree)) { + /* 10 or 11 */ + verify_uptodate(current); + return 0; + } + else if (oldtree && newtree && + same(current, oldtree) && !same(current, newtree)) { + /* 20 or 21 */ + verify_uptodate(current); + return merged_entry(newtree, NULL, dst); + } + else + /* all other failures */ return -1; } - if (b) - return merged_entry(b, old, dst); - if (old) - verify_uptodate(old); - return 0; + else if (newtree) + return merged_entry(newtree, NULL, dst); + else + return 0; } /* diff --git a/t/t1001-read-tree-m-2way.sh b/t/t1001-read-tree-m-2way.sh new file mode 100755 index 000000000..33041f3cc --- /dev/null +++ b/t/t1001-read-tree-m-2way.sh @@ -0,0 +1,281 @@ +#!/bin/sh +# +# Copyright (c) 2005 Junio C Hamano +# + +test_description='Two way merge with read-tree -m $H $M + +This test tries two-way merge (aka fast forward with carry forward). + +There is the head (called H) and another commit (called M), which is +simply ahead of H. The index and the work tree contains a state that +is derived from H, but may also have local changes. This test checks +all the combinations described in the two-tree merge "carry forward" +rules, found in <Documentation/git-rev-tree.txt>. + +In the test, these paths are used: + bozbar - in H, stays in M, modified from bozbar to gnusto + frotz - not in H added in M + nitfol - in H, stays in M unmodified + rezrov - in H, deleted in M + yomin - not in H nor M +' +. ./test-lib.sh + +_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' +_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" +compare_change () { + sed >current \ + -e '/^--- /d; /^+++ /d; /^@@ /d;' \ + -e 's/^\(.[0-7][0-7][0-7][0-7][0-7][0-7]\) '"$_x40"' /\1 X /' "$1" + diff -u expected current +} + +check_cache_at () { + clean_if_empty=`git-diff-files "$1"` + case "$clean_if_empty" in + '') echo "$1: clean" ;; + ?*) echo "$1: dirty" ;; + esac + case "$2,$clean_if_empty" in + clean,) : ;; + clean,?*) false ;; + dirty,) false ;; + dirty,?*) : ;; + esac +} + +test_expect_success \ + setup \ + 'echo frotz >frotz && + echo nitfol >nitfol && + echo bozbar >bozbar && + echo rezrov >rezrov && + echo yomin >yomin && + git-update-cache --add nitfol bozbar rezrov && + treeH=`git-write-tree` && + echo treeH $treeH && + git-ls-tree $treeH && + + echo gnusto >bozbar && + git-update-cache --add frotz bozbar --force-remove rezrov && + git-ls-files --stage >M.out && + treeM=`git-write-tree` && + echo treeM $treeM && + git-ls-tree $treeM && + git-diff-tree $treeH $treeM' + +test_expect_success \ + '1, 2, 3 - no carry forward' \ + 'rm -f .git/index && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >1-3.out && + cmp M.out 1-3.out && + check_cache_at bozbar dirty && + check_cache_at frotz dirty && + check_cache_at nitfol dirty' + +echo '+100644 X 0 yomin' >expected + +test_expect_success \ + '4 - carry forward local addition.' \ + 'rm -f .git/index && + git-update-cache --add yomin && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >4.out || exit + diff --unified=0 M.out 4.out >4diff.out + compare_change 4diff.out expected && + check_cache_at yomin clean' + +test_expect_success \ + '5 - carry forward local addition.' \ + 'rm -f .git/index && + echo yomin >yomin && + git-update-cache --add yomin && + echo yomin yomin >yomin && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >5.out || exit + diff --unified=0 M.out 5.out >5diff.out + compare_change 5diff.out expected && + check_cache_at yomin dirty' + +test_expect_success \ + '6 - local addition already has the same.' \ + 'rm -f .git/index && + git-update-cache --add frotz && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >6.out && + diff --unified=0 M.out 6.out && + check_cache_at frotz clean' + +test_expect_success \ + '7 - local addition already has the same.' \ + 'rm -f .git/index && + echo frotz >frotz && + git-update-cache --add frotz && + echo frotz frotz >frotz && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >7.out && + diff --unified=0 M.out 7.out && + check_cache_at frotz dirty' + +test_expect_success \ + '8 - conflicting addition.' \ + 'rm -f .git/index && + echo frotz frotz >frotz && + git-update-cache --add frotz && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '9 - conflicting addition.' \ + 'rm -f .git/index && + echo frotz frotz >frotz && + git-update-cache --add frotz && + echo frotz >frotz && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '10 - path removed.' \ + 'rm -f .git/index && + echo rezrov >rezrov && + git-update-cache --add rezrov && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >10.out && + cmp M.out 10.out' + +test_expect_success \ + '11 - dirty path removed.' \ + 'rm -f .git/index && + echo rezrov >rezrov && + git-update-cache --add rezrov && + echo rezrov rezrov >rezrov && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '12 - unmatching local changes being removed.' \ + 'rm -f .git/index && + echo rezrov rezrov >rezrov && + git-update-cache --add rezrov && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '13 - unmatching local changes being removed.' \ + 'rm -f .git/index && + echo rezrov rezrov >rezrov && + git-update-cache --add rezrov && + echo rezrov >rezrov && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +cat >expected <<EOF +-100644 X 0 nitfol ++100644 X 0 nitfol +EOF + +test_expect_success \ + '14 - unchanged in two heads.' \ + 'rm -f .git/index && + echo nitfol nitfol >nitfol && + git-update-cache --add nitfol && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >14.out || exit + diff --unified=0 M.out 14.out >14diff.out + compare_change 14diff.out expected && + check_cache_at nitfol clean' + +test_expect_success \ + '15 - unchanged in two heads.' \ + 'rm -f .git/index && + echo nitfol nitfol >nitfol && + git-update-cache --add nitfol && + echo nitfol nitfol nitfol >nitfol && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >15.out || exit + diff --unified=0 M.out 15.out >15diff.out + compare_change 15diff.out expected && + check_cache_at nitfol dirty' + +test_expect_success \ + '16 - conflicting local change.' \ + 'rm -f .git/index && + echo bozbar bozbar >bozbar && + git-update-cache --add bozbar && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '17 - conflicting local change.' \ + 'rm -f .git/index && + echo bozbar bozbar >bozbar && + git-update-cache --add bozbar && + echo bozbar bozbar bozbar >bozbar && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '18 - local change already having a good result.' \ + 'rm -f .git/index && + echo gnusto >bozbar && + git-update-cache --add bozbar && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >18.out && + diff --unified=0 M.out 18.out && + check_cache_at bozbar clean' + +test_expect_success \ + '19 - local change already having a good result, further modified.' \ + 'rm -f .git/index && + echo gnusto >bozbar && + git-update-cache --add bozbar && + echo gnusto gnusto >bozbar && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >19.out && + diff --unified=0 M.out 19.out && + check_cache_at bozbar dirty' + +test_expect_success \ + '20 - no local change, use new tree.' \ + 'rm -f .git/index && + echo bozbar >bozbar && + git-update-cache --add bozbar && + git-read-tree -m $treeH $treeM && + git-ls-files --stage >20.out && + diff --unified=0 M.out 20.out && + check_cache_at bozbar dirty' + +test_expect_success \ + '21 - no local change, dirty cache.' \ + 'rm -f .git/index && + echo bozbar >bozbar && + git-update-cache --add bozbar && + echo gnusto gnusto >bozbar && + if git-read-tree -m $treeH $treeM; then false; else :; fi' + +# Also make sure we did not break DF vs DF/DF case. +test_expect_success \ + 'DF vs DF/DF case setup.' \ + 'rm -f .git/index && + echo DF >DF && + git-update-cache --add DF && + treeDF=`git-write-tree` && + echo treeDF $treeDF && + git-ls-tree $treeDF && + + rm -f DF && + mkdir DF && + echo DF/DF >DF/DF && + git-update-cache --add --remove DF DF/DF && + treeDFDF=`git-write-tree` && + echo treeDFDF $treeDFDF && + git-ls-tree $treeDFDF && + git-ls-files --stage >DFDF.out' + +test_expect_success \ + 'DF vs DF/DF case test.' \ + 'rm -f .git/index && + rm -fr DF && + echo DF >DF && + git-update-cache --add DF && + git-read-tree -m $treeDF $treeDFDF && + git-ls-files --stage >DFDFcheck.out && + diff --unified=0 DFDF.out DFDFcheck.out && + check_cache_at DF/DF dirty' + +test_done diff --git a/t/t1002-read-tree-m-u-2way.sh b/t/t1002-read-tree-m-u-2way.sh new file mode 100755 index 000000000..75a4eaaf2 --- /dev/null +++ b/t/t1002-read-tree-m-u-2way.sh @@ -0,0 +1,307 @@ +#!/bin/sh +# +# Copyright (c) 2005 Junio C Hamano +# + +test_description='Two way merge with read-tree -m -u $H $M + +This is identical to t1001, but uses -u to update the work tree as well. + +' +. ./test-lib.sh + +_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' +_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" +compare_change () { + sed >current \ + -e '/^--- /d; /^+++ /d; /^@@ /d;' \ + -e 's/^\(.[0-7][0-7][0-7][0-7][0-7][0-7]\) '"$_x40"' /\1 X /' "$1" + diff -u expected current +} + +check_cache_at () { + clean_if_empty=`git-diff-files "$1"` + case "$clean_if_empty" in + '') echo "$1: clean" ;; + ?*) echo "$1: dirty" ;; + esac + case "$2,$clean_if_empty" in + clean,) : ;; + clean,?*) false ;; + dirty,) false ;; + dirty,?*) : ;; + esac +} + +test_expect_success \ + setup \ + 'echo frotz >frotz && + echo nitfol >nitfol && + echo bozbar >bozbar && + echo rezrov >rezrov && + echo yomin >yomin && + git-update-cache --add nitfol bozbar rezrov && + treeH=`git-write-tree` && + echo treeH $treeH && + git-ls-tree $treeH && + + echo gnusto >bozbar && + git-update-cache --add frotz bozbar --force-remove rezrov && + git-ls-files --stage >M.out && + treeM=`git-write-tree` && + echo treeM $treeM && + git-ls-tree $treeM && + sha1sum bozbar frotz nitfol >M.sha1 && + git-diff-tree $treeH $treeM' + +test_expect_success \ + '1, 2, 3 - no carry forward' \ + 'rm -f .git/index && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >1-3.out && + cmp M.out 1-3.out && + sha1sum -c M.sha1 && + check_cache_at bozbar clean && + check_cache_at frotz clean && + check_cache_at nitfol clean' + +echo '+100644 X 0 yomin' >expected + +test_expect_success \ + '4 - carry forward local addition.' \ + 'rm -f .git/index && + git-update-cache --add yomin && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >4.out || exit + diff --unified=0 M.out 4.out >4diff.out + compare_change 4diff.out expected && + check_cache_at yomin clean && + sha1sum -c M.sha1 && + echo yomin >yomin1 && + diff yomin yomin1 && + rm -f yomin1' + +test_expect_success \ + '5 - carry forward local addition.' \ + 'rm -f .git/index && + echo yomin >yomin && + git-update-cache --add yomin && + echo yomin yomin >yomin && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >5.out || exit + diff --unified=0 M.out 5.out >5diff.out + compare_change 5diff.out expected && + check_cache_at yomin dirty && + sha1sum -c M.sha1 && + : dirty index should have prevented -u from checking it out. + echo yomin yomin >yomin1 && + diff yomin yomin1 && + rm -f yomin1' + +test_expect_success \ + '6 - local addition already has the same.' \ + 'rm -f .git/index && + git-update-cache --add frotz && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >6.out && + diff --unified=0 M.out 6.out && + check_cache_at frotz clean && + sha1sum -c M.sha1 && + echo frotz >frotz1 && + diff frotz frotz1 && + rm -f frotz1' + +test_expect_success \ + '7 - local addition already has the same.' \ + 'rm -f .git/index && + echo frotz >frotz && + git-update-cache --add frotz && + echo frotz frotz >frotz && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >7.out && + diff --unified=0 M.out 7.out && + check_cache_at frotz dirty && + if sha1sum -c M.sha1; then false; else :; fi && + : dirty index should have prevented -u from checking it out. + echo frotz frotz >frotz1 && + diff frotz frotz1 && + rm -f frotz1' + +test_expect_success \ + '8 - conflicting addition.' \ + 'rm -f .git/index && + echo frotz frotz >frotz && + git-update-cache --add frotz && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '9 - conflicting addition.' \ + 'rm -f .git/index && + echo frotz frotz >frotz && + git-update-cache --add frotz && + echo frotz >frotz && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '10 - path removed.' \ + 'rm -f .git/index && + echo rezrov >rezrov && + git-update-cache --add rezrov && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >10.out && + cmp M.out 10.out && + sha1sum -c M.sha1' + +test_expect_success \ + '11 - dirty path removed.' \ + 'rm -f .git/index && + echo rezrov >rezrov && + git-update-cache --add rezrov && + echo rezrov rezrov >rezrov && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '12 - unmatching local changes being removed.' \ + 'rm -f .git/index && + echo rezrov rezrov >rezrov && + git-update-cache --add rezrov && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '13 - unmatching local changes being removed.' \ + 'rm -f .git/index && + echo rezrov rezrov >rezrov && + git-update-cache --add rezrov && + echo rezrov >rezrov && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +cat >expected <<EOF +-100644 X 0 nitfol ++100644 X 0 nitfol +EOF + +test_expect_success \ + '14 - unchanged in two heads.' \ + 'rm -f .git/index && + echo nitfol nitfol >nitfol && + git-update-cache --add nitfol && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >14.out || exit + diff --unified=0 M.out 14.out >14diff.out + compare_change 14diff.out expected && + check_cache_at nitfol clean && + grep -v nitfol M.sha1 | sha1sum -c && + if sha1sum -c M.sha1; then false; else :; fi && + echo nitfol nitfol >nitfol1 && + diff nitfol nitfol1 && + rm -f nitfol1' + +test_expect_success \ + '15 - unchanged in two heads.' \ + 'rm -f .git/index && + echo nitfol nitfol >nitfol && + git-update-cache --add nitfol && + echo nitfol nitfol nitfol >nitfol && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >15.out || exit + diff --unified=0 M.out 15.out >15diff.out + compare_change 15diff.out expected && + check_cache_at nitfol dirty && + grep -v nitfol M.sha1 | sha1sum -c && + if sha1sum -c M.sha1; then false; else :; fi && + echo nitfol nitfol nitfol >nitfol1 && + diff nitfol nitfol1 && + rm -f nitfol1' + +test_expect_success \ + '16 - conflicting local change.' \ + 'rm -f .git/index && + echo bozbar bozbar >bozbar && + git-update-cache --add bozbar && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '17 - conflicting local change.' \ + 'rm -f .git/index && + echo bozbar bozbar >bozbar && + git-update-cache --add bozbar && + echo bozbar bozbar bozbar >bozbar && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +test_expect_success \ + '18 - local change already having a good result.' \ + 'rm -f .git/index && + echo gnusto >bozbar && + git-update-cache --add bozbar && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >18.out && + diff --unified=0 M.out 18.out && + check_cache_at bozbar clean && + sha1sum -c M.sha1' + +test_expect_success \ + '19 - local change already having a good result, further modified.' \ + 'rm -f .git/index && + echo gnusto >bozbar && + git-update-cache --add bozbar && + echo gnusto gnusto >bozbar && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >19.out && + diff --unified=0 M.out 19.out && + check_cache_at bozbar dirty && + grep -v bozbar M.sha1 | sha1sum -c && + if sha1sum -c M.sha1; then false; else :; fi && + echo gnusto gnusto >bozbar1 && + diff bozbar bozbar1 && + rm -f bozbar1' + +test_expect_success \ + '20 - no local change, use new tree.' \ + 'rm -f .git/index && + echo bozbar >bozbar && + git-update-cache --add bozbar && + git-read-tree -m -u $treeH $treeM && + git-ls-files --stage >20.out && + diff --unified=0 M.out 20.out && + check_cache_at bozbar clean && + sha1sum -c M.sha1' + +test_expect_success \ + '21 - no local change, dirty cache.' \ + 'rm -f .git/index && + echo bozbar >bozbar && + git-update-cache --add bozbar && + echo gnusto gnusto >bozbar && + if git-read-tree -m -u $treeH $treeM; then false; else :; fi' + +# Also make sure we did not break DF vs DF/DF case. +test_expect_success \ + 'DF vs DF/DF case setup.' \ + 'rm -f .git/index && + echo DF >DF && + git-update-cache --add DF && + treeDF=`git-write-tree` && + echo treeDF $treeDF && + git-ls-tree $treeDF && + + rm -f DF && + mkdir DF && + echo DF/DF >DF/DF && + git-update-cache --add --remove DF DF/DF && + treeDFDF=`git-write-tree` && + echo treeDFDF $treeDFDF && + git-ls-tree $treeDFDF && + git-ls-files --stage >DFDF.out' + +test_expect_success \ + 'DF vs DF/DF case test.' \ + 'rm -f .git/index && + rm -fr DF && + echo DF >DF && + git-update-cache --add DF && + git-read-tree -m -u $treeDF $treeDFDF && + git-ls-files --stage >DFDFcheck.out && + diff --unified=0 DFDF.out DFDFcheck.out && + check_cache_at DF/DF clean' + +test_done |