Skip to content

Commit 75405e7

Browse files
committed
Merge branch 'dt/submodule-diff-fixes'
"git diff --submodule=diff" showed failure from run_command() when trying to run diff inside a submodule, when the user manually removes the submodule directory. * dt/submodule-diff-fixes: diff --submodule=diff: don't print failure message twice diff --submodule=diff: do not fail on ever-initialied deleted submodules t4060: remove unused variable
2 parents c2509c5 + 67f61ef commit 75405e7

File tree

2 files changed

+164
-9
lines changed

2 files changed

+164
-9
lines changed

submodule.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,20 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
720720
strvec_push(&cp.args, oid_to_hex(new_oid));
721721

722722
prepare_submodule_repo_env(&cp.env_array);
723-
if (start_command(&cp))
723+
724+
if (!is_directory(path)) {
725+
/* fall back to absorbed git dir, if any */
726+
if (!sub)
727+
goto done;
728+
cp.dir = sub->gitdir;
729+
strvec_push(&cp.env_array, GIT_DIR_ENVIRONMENT "=.");
730+
strvec_push(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT "=.");
731+
}
732+
733+
if (start_command(&cp)) {
724734
diff_emit_submodule_error(o, "(diff failed)\n");
735+
goto done;
736+
}
725737

726738
while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
727739
diff_emit_submodule_pipethrough(o, sb.buf, sb.len);

t/t4060-diff-submodule-option-diff-format.sh

Lines changed: 151 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ test_expect_success 'typechanged submodule(submodule->blob)' '
361361
rm -f sm1 &&
362362
test_create_repo sm1 &&
363363
head6=$(add_file sm1 foo6 foo7)
364-
fullhead6=$(cd sm1; git rev-parse --verify HEAD)
365364
test_expect_success 'nonexistent commit' '
366365
git diff-index -p --submodule=diff HEAD >actual &&
367366
cat >expected <<-EOF &&
@@ -704,10 +703,26 @@ test_expect_success 'path filter' '
704703
diff_cmp expected actual
705704
'
706705

707-
commit_file sm2
706+
cat >.gitmodules <<-EOF
707+
[submodule "sm2"]
708+
path = sm2
709+
url = bogus_url
710+
EOF
711+
git add .gitmodules
712+
commit_file sm2 .gitmodules
713+
708714
test_expect_success 'given commit' '
709715
git diff-index -p --submodule=diff HEAD^ >actual &&
710716
cat >expected <<-EOF &&
717+
diff --git a/.gitmodules b/.gitmodules
718+
new file mode 100644
719+
index 1234567..89abcde
720+
--- /dev/null
721+
+++ b/.gitmodules
722+
@@ -0,0 +1,3 @@
723+
+[submodule "sm2"]
724+
+path = sm2
725+
+url = bogus_url
711726
Submodule sm1 $head7...0000000 (submodule deleted)
712727
Submodule sm2 0000000...$head9 (new submodule)
713728
diff --git a/sm2/foo8 b/sm2/foo8
@@ -729,15 +744,21 @@ test_expect_success 'given commit' '
729744
'
730745

731746
test_expect_success 'setup .git file for sm2' '
732-
(cd sm2 &&
733-
REAL="$(pwd)/../.real" &&
734-
mv .git "$REAL" &&
735-
echo "gitdir: $REAL" >.git)
747+
git submodule absorbgitdirs sm2
736748
'
737749

738750
test_expect_success 'diff --submodule=diff with .git file' '
739751
git diff --submodule=diff HEAD^ >actual &&
740752
cat >expected <<-EOF &&
753+
diff --git a/.gitmodules b/.gitmodules
754+
new file mode 100644
755+
index 1234567..89abcde
756+
--- /dev/null
757+
+++ b/.gitmodules
758+
@@ -0,0 +1,3 @@
759+
+[submodule "sm2"]
760+
+path = sm2
761+
+url = bogus_url
741762
Submodule sm1 $head7...0000000 (submodule deleted)
742763
Submodule sm2 0000000...$head9 (new submodule)
743764
diff --git a/sm2/foo8 b/sm2/foo8
@@ -758,9 +779,67 @@ test_expect_success 'diff --submodule=diff with .git file' '
758779
diff_cmp expected actual
759780
'
760781

782+
mv sm2 sm2-bak
783+
784+
test_expect_success 'deleted submodule with .git file' '
785+
git diff-index -p --submodule=diff HEAD >actual &&
786+
cat >expected <<-EOF &&
787+
Submodule sm1 $head7...0000000 (submodule deleted)
788+
Submodule sm2 $head9...0000000 (submodule deleted)
789+
diff --git a/sm2/foo8 b/sm2/foo8
790+
deleted file mode 100644
791+
index 1234567..89abcde
792+
--- a/sm2/foo8
793+
+++ /dev/null
794+
@@ -1 +0,0 @@
795+
-foo8
796+
diff --git a/sm2/foo9 b/sm2/foo9
797+
deleted file mode 100644
798+
index 1234567..89abcde
799+
--- a/sm2/foo9
800+
+++ /dev/null
801+
@@ -1 +0,0 @@
802+
-foo9
803+
EOF
804+
diff_cmp expected actual
805+
'
806+
807+
echo submodule-to-blob>sm2
808+
809+
test_expect_success 'typechanged(submodule->blob) submodule with .git file' '
810+
git diff-index -p --submodule=diff HEAD >actual &&
811+
cat >expected <<-EOF &&
812+
Submodule sm1 $head7...0000000 (submodule deleted)
813+
Submodule sm2 $head9...0000000 (submodule deleted)
814+
diff --git a/sm2/foo8 b/sm2/foo8
815+
deleted file mode 100644
816+
index 1234567..89abcde
817+
--- a/sm2/foo8
818+
+++ /dev/null
819+
@@ -1 +0,0 @@
820+
-foo8
821+
diff --git a/sm2/foo9 b/sm2/foo9
822+
deleted file mode 100644
823+
index 1234567..89abcde
824+
--- a/sm2/foo9
825+
+++ /dev/null
826+
@@ -1 +0,0 @@
827+
-foo9
828+
diff --git a/sm2 b/sm2
829+
new file mode 100644
830+
index 1234567..89abcde
831+
--- /dev/null
832+
+++ b/sm2
833+
@@ -0,0 +1 @@
834+
+submodule-to-blob
835+
EOF
836+
diff_cmp expected actual
837+
'
838+
839+
rm sm2
840+
mv sm2-bak sm2
841+
761842
test_expect_success 'setup nested submodule' '
762-
git submodule add -f ./sm2 &&
763-
git commit -a -m "add sm2" &&
764843
git -C sm2 submodule add ../sm2 nested &&
765844
git -C sm2 commit -a -m "nested sub" &&
766845
head10=$(git -C sm2 rev-parse --short --verify HEAD)
@@ -791,6 +870,7 @@ test_expect_success 'diff --submodule=diff with moved nested submodule HEAD' '
791870

792871
test_expect_success 'diff --submodule=diff recurses into nested submodules' '
793872
cat >expected <<-EOF &&
873+
Submodule sm1 $head7...0000000 (submodule deleted)
794874
Submodule sm2 contains modified content
795875
Submodule sm2 $head9..$head10:
796876
diff --git a/sm2/.gitmodules b/sm2/.gitmodules
@@ -830,4 +910,67 @@ test_expect_success 'diff --submodule=diff recurses into nested submodules' '
830910
diff_cmp expected actual
831911
'
832912

913+
(cd sm2; commit_file nested)
914+
commit_file sm2
915+
head12=$(cd sm2; git rev-parse --short --verify HEAD)
916+
917+
mv sm2 sm2-bak
918+
919+
test_expect_success 'diff --submodule=diff recurses into deleted nested submodules' '
920+
cat >expected <<-EOF &&
921+
Submodule sm1 $head7...0000000 (submodule deleted)
922+
Submodule sm2 $head12...0000000 (submodule deleted)
923+
diff --git a/sm2/.gitmodules b/sm2/.gitmodules
924+
deleted file mode 100644
925+
index 3a816b8..0000000
926+
--- a/sm2/.gitmodules
927+
+++ /dev/null
928+
@@ -1,3 +0,0 @@
929+
-[submodule "nested"]
930+
- path = nested
931+
- url = ../sm2
932+
diff --git a/sm2/foo8 b/sm2/foo8
933+
deleted file mode 100644
934+
index db9916b..0000000
935+
--- a/sm2/foo8
936+
+++ /dev/null
937+
@@ -1 +0,0 @@
938+
-foo8
939+
diff --git a/sm2/foo9 b/sm2/foo9
940+
deleted file mode 100644
941+
index 9c3b4f6..0000000
942+
--- a/sm2/foo9
943+
+++ /dev/null
944+
@@ -1 +0,0 @@
945+
-foo9
946+
Submodule nested $head11...0000000 (submodule deleted)
947+
diff --git a/sm2/nested/file b/sm2/nested/file
948+
deleted file mode 100644
949+
index ca281f5..0000000
950+
--- a/sm2/nested/file
951+
+++ /dev/null
952+
@@ -1 +0,0 @@
953+
-nested content
954+
diff --git a/sm2/nested/foo8 b/sm2/nested/foo8
955+
deleted file mode 100644
956+
index db9916b..0000000
957+
--- a/sm2/nested/foo8
958+
+++ /dev/null
959+
@@ -1 +0,0 @@
960+
-foo8
961+
diff --git a/sm2/nested/foo9 b/sm2/nested/foo9
962+
deleted file mode 100644
963+
index 9c3b4f6..0000000
964+
--- a/sm2/nested/foo9
965+
+++ /dev/null
966+
@@ -1 +0,0 @@
967+
-foo9
968+
EOF
969+
git diff --submodule=diff >actual 2>err &&
970+
test_must_be_empty err &&
971+
diff_cmp expected actual
972+
'
973+
974+
mv sm2-bak sm2
975+
833976
test_done

0 commit comments

Comments
 (0)