@matheusbsbs / matheus-bagliano / commits / 313ee0d69f
Fork 0

submodule: process conflicting submodules only once

During a merge module_list returns conflicting submodules several times (stage 1,2,3) which caused the submodules to be used multiple times in git submodule init, sync, update and status command. There are 5 callers of module_list; they all read (mode, sha1, stage, path) tuple, and most of them care only about path. As a first level approximation, it should be Ok (in the sense that it does not make things worse than it currently is) to filter the duplicate paths from module_list output, but some callers should change their behaviour when the merge in the superproject still has conflicts. Notice the higher-stage entries, and emit only one record from module_list, but while doing so, mark the entry with "U" (not [0-3]) in the $stage field and null out the SHA-1 part, as the object name for the lowest stage does not give any useful information to the caller, and this way any caller that uses the object name would hopefully barf. Then update the codepaths for each subcommands this way: - "update" should not touch the submodule repository, because we do not know what commit should be checked out yet. - "status" reports the conflicting submodules as 'U000...000' and does not recurse into them (we might later want to make it recurse). - The command called by "foreach" may want to do whatever it wants to do by noticing the merged status in the superproject itself, so feed the path to it from module_list as before, but only once per submodule. - "init" and "sync" are unlikely things to do while the superproject is still not merged, but as long as a submodule is there in $path, there is no point skipping it. It might however want to take the merged status of .gitmodules into account, but that is outside of the scope of this topic. Acked-by: Jens Lehmann <Jens.Lehmann@web.de> Thanks-to: Junio C Hamano <gitster@pobox.com> Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Morey-Chaisemartin committed Mar 30, 2011 at 07:20 UTC 313ee0d69f58c2bf2f4e0a62b812c9d08a39b240
3 files changed +83 -9
Documentation/git-submodule.txt
+3 -2
@@ -101,9 +101,10 @@ status::
101 currently checked out commit for each submodule, along with the
102 submodule path and the output of 'git describe' for the
103 SHA-1. Each SHA-1 will be prefixed with `-` if the submodule is not
104 - initialized and `+` if the currently checked out submodule commit
104 + initialized, `+` if the currently checked out submodule commit
105 does not match the SHA-1 found in the index of the containing
106 - repository. This command is the default command for 'git submodule'.
106 + repository and `U` if the submodule has merge conflicts.
107 + This command is the default command for 'git submodule'.
108 +
109 If '--recursive' is specified, this command will recurse into nested
110 submodules, and show their status as well.
git-submodule.sh
+28 -1
@@ -72,7 +72,24 @@ resolve_relative_url ()
72 #
73 module_list()
74 {
75 - git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
75 + git ls-files --error-unmatch --stage -- "$@" |
76 + perl -e '
77 + my %unmerged = ();
78 + my ($null_sha1) = ("0" x 40);
79 + while (<STDIN>) {
80 + chomp;
81 + my ($mode, $sha1, $stage, $path) =
82 + /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
83 + next unless $mode eq "160000";
84 + if ($stage ne "0") {
85 + if (!$unmerged{$path}++) {
86 + print "$mode $null_sha1 U\t$path\n";
87 + }
88 + next;
89 + }
90 + print "$_\n";
91 + }
92 + '
93 }
94
95 #
@@ -427,6 +444,11 @@ cmd_update()
444 module_list "$@" |
445 while read mode sha1 stage path
446 do
447 + if test "$stage" = U
448 + then
449 + echo >&2 "Skipping unmerged submodule $path"
450 + continue
451 + fi
452 name=$(module_name "$path") || exit
453 url=$(git config submodule."$name".url)
454 update_module=$(git config submodule."$name".update)
@@ -770,6 +792,11 @@ cmd_status()
792 name=$(module_name "$path") || exit
793 url=$(git config submodule."$name".url)
794 displaypath="$prefix$path"
795 + if test "$stage" = U
796 + then
797 + say "U$sha1 $displaypath"
798 + continue
799 + fi
800 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
801 then
802 say "-$sha1 $displaypath"
t/t7405-submodule-merge.sh
+52 -6
@@ -56,11 +56,11 @@ test_expect_success setup '
56
57 # History setup
58 #
59 -# b
60 -# / \
61 -# a d
62 -# \ /
63 -# c
59 +# b
60 +# / \
61 +# init -- a d
62 +# \ \ /
63 +# g c
64 #
65 # a in the main repository records to sub-a in the submodule and
66 # analogous b and c. d should be automatically found by merging c into
@@ -76,6 +76,8 @@ test_expect_success 'setup for merge search' '
76 git add file-a &&
77 git commit -m "sub-a" &&
78 git branch sub-a) &&
79 + git commit --allow-empty -m init &&
80 + git branch init &&
81 git add sub &&
82 git commit -m "a" &&
83 git branch a &&
@@ -101,7 +103,13 @@ test_expect_success 'setup for merge search' '
103 git checkout -b sub-d sub-b &&
104 git merge sub-c) &&
105 git commit -a -m "d" &&
104 - git branch test b)
106 + git branch test b &&
107 +
108 + git checkout -b g init &&
109 + (cd sub &&
110 + git checkout -b sub-g sub-c) &&
111 + git add sub &&
112 + git commit -a -m "g")
113 '
114
115 test_expect_success 'merge with one side as a fast-forward of the other' '
@@ -176,6 +184,44 @@ test_expect_success 'merging should fail for changes that are backwards' '
184 test_must_fail git merge f)
185 '
186
187 +
188 +# Check that the conflicting submodule is detected when it is
189 +# in the common ancestor. status should be 'U00...00"
190 +test_expect_success 'git submodule status should display the merge conflict properly with merge base' '
191 + (cd merge-search &&
192 + cat >.gitmodules <<EOF &&
193 +[submodule "sub"]
194 + path = sub
195 + url = $TRASH_DIRECTORY/sub
196 +EOF
197 + cat >expect <<EOF &&
198 +U0000000000000000000000000000000000000000 sub
199 +EOF
200 + git submodule status > actual &&
201 + test_cmp expect actual &&
202 + git reset --hard)
203 +'
204 +
205 +# Check that the conflicting submodule is detected when it is
206 +# not in the common ancestor. status should be 'U00...00"
207 +test_expect_success 'git submodule status should display the merge conflict properly without merge-base' '
208 + (cd merge-search &&
209 + git checkout -b test-no-merge-base g &&
210 + test_must_fail git merge b &&
211 + cat >.gitmodules <<EOF &&
212 +[submodule "sub"]
213 + path = sub
214 + url = $TRASH_DIRECTORY/sub
215 +EOF
216 + cat >expect <<EOF &&
217 +U0000000000000000000000000000000000000000 sub
218 +EOF
219 + git submodule status > actual &&
220 + test_cmp expect actual &&
221 + git reset --hard)
222 +'
223 +
224 +
225 test_expect_success 'merging with a modify/modify conflict between merge bases' '
226 git reset --hard HEAD &&
227 git checkout -b test2 c &&