git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] short status: improve reporting for submodule changes

From
Stefan Beller <sbeller@google.com>
Date
Mar 23, 2017, 00:43 UTC
Message-ID
<20170323004329.15892-4-sbeller@google.com>
In-Reply-To
<20170323004329.15892-1-sbeller@google.com>

If I add an untracked file to a submodule or modify a tracked file, currently "git status --short" treats the change in the same way as changes to the current HEAD of the submodule:

        $ git clone --quiet --recurse-submodules https://gerrit.googlesource.com/gerrit
        $ echo hello >gerrit/plugins/replication/stray-file
        $ sed -i -e 's/.*//' gerrit/plugins/replication/.mailmap
        $ git -C gerrit status --short
         M plugins/replication

This is by analogy with ordinary files, where "M" represents a change that has not been added yet to the index. But this change cannot be added to the index without entering the submodule, "git add"-ing it, and running "git commit", so the analogy is counterproductive.

Introduce new status letters " ?" and " m" for this. These are similar to the existing "??" and " M" but mean that the submodule (not the parent project) has new untracked files and modified files, respectively. The user can use "git add" and "git commit" from within the submodule to add them.

Changes to the submodule's HEAD commit can be recorded in the index with a plain "git add -u" and are shown with " M", like today.

To avoid excessive clutter, show at most one of " ?", " m", and " M" for the submodule. They represent increasing levels of change --- the last one that applies is shown (e.g., " m" if there are both modified files and untracked files in the submodule, or " M" if the submodule's HEAD has been modified and it has untracked files).

While making these changes, we need to make sure to not break porcelain level 1, which shares code with "status --short". We only change "git status --short".

Non-short "git status" and "git status --porcelain=2" already handle these cases by showing more detail:

        $ git -C gerrit status --porcelain=2
        1 .M S.MU 160000 160000 160000 305c864db28eb0c77c8499bc04c87de3f849cf3c 305c864db28eb0c77c8499bc04c87de3f849cf3c plugins/replication
        $ git -C gerrit status
[...]
        modified:   plugins/replication (modified content, untracked content)
Scripts caring about these distinctions should use --porcelain=2.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
---
 Documentation/git-status.txt |  9 +++++++++
 t/t3600-rm.sh                | 18 +++++++++++++-----
 t/t7506-status-submodule.sh  | 24 ++++++++++++++++++++++++
 wt-status.c                  | 13 +++++++++++--
 4 files changed, 57 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-status.txt b/Documentation/git-status.txt
index ba873657cf..01b457c322 100644
--- a/Documentation/git-status.txt
+++ b/Documentation/git-status.txt
@@ -181,6 +181,13 @@ in which case `XY` are `!!`.
     !           !    ignored
     -------------------------------------------------
 
+Submodules have more state and instead report
+		M    the submodule has a different HEAD than
+		     recorded in the index
+		m    the submodule has modified content
+		?    the submodule has untracked files
+
+
 If -b is used the short-format status is preceded by a line
 
     ## branchname tracking info
@@ -210,6 +217,8 @@ field from the first filename).  Third, filenames containing special
 characters are not specially formatted; no quoting or
 backslash-escaping is performed.
 
+Any submodule changes are reported as modified `M` instead of `m` or single `?`.
+
 Porcelain Format Version 2
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/t/t3600-rm.sh b/t/t3600-rm.sh
index 5aa6db584c..b58793448b 100755
--- a/t/t3600-rm.sh
+++ b/t/t3600-rm.sh
@@ -268,6 +268,14 @@ cat >expect.modified <<EOF
  M submod
 EOF
 
+cat >expect.modified_inside <<EOF
+ m submod
+EOF
+
+cat >expect.modified_untracked <<EOF
+ ? submod
+EOF
+
 cat >expect.cached <<EOF
 D  submod
 EOF
@@ -421,7 +429,7 @@ test_expect_success 'rm of a populated submodule with modifications fails unless
 	test -d submod &&
 	test -f submod/.git &&
 	git status -s -uno --ignore-submodules=none >actual &&
-	test_cmp expect.modified actual &&
+	test_cmp expect.modified_inside actual &&
 	git rm -f submod &&
 	test ! -d submod &&
 	git status -s -uno --ignore-submodules=none >actual &&
@@ -436,7 +444,7 @@ test_expect_success 'rm of a populated submodule with untracked files fails unle
 	test -d submod &&
 	test -f submod/.git &&
 	git status -s -uno --ignore-submodules=none >actual &&
-	test_cmp expect.modified actual &&
+	test_cmp expect.modified_untracked actual &&
 	git rm -f submod &&
 	test ! -d submod &&
 	git status -s -uno --ignore-submodules=none >actual &&
@@ -621,7 +629,7 @@ test_expect_success 'rm of a populated nested submodule with different nested HE
 	test -d submod &&
 	test -f submod/.git &&
 	git status -s -uno --ignore-submodules=none >actual &&
-	test_cmp expect.modified actual &&
+	test_cmp expect.modified_inside actual &&
 	git rm -f submod &&
 	test ! -d submod &&
 	git status -s -uno --ignore-submodules=none >actual &&
@@ -636,7 +644,7 @@ test_expect_success 'rm of a populated nested submodule with nested modification
 	test -d submod &&
 	test -f submod/.git &&
 	git status -s -uno --ignore-submodules=none >actual &&
-	test_cmp expect.modified actual &&
+	test_cmp expect.modified_inside actual &&
 	git rm -f submod &&
 	test ! -d submod &&
 	git status -s -uno --ignore-submodules=none >actual &&
@@ -651,7 +659,7 @@ test_expect_success 'rm of a populated nested submodule with nested untracked fi
 	test -d submod &&
 	test -f submod/.git &&
 	git status -s -uno --ignore-submodules=none >actual &&
-	test_cmp expect.modified actual &&
+	test_cmp expect.modified_untracked actual &&
 	git rm -f submod &&
 	test ! -d submod &&
 	git status -s -uno --ignore-submodules=none >actual &&
diff --git a/t/t7506-status-submodule.sh b/t/t7506-status-submodule.sh
index d31b34da83..98dff01947 100755
--- a/t/t7506-status-submodule.sh
+++ b/t/t7506-status-submodule.sh
@@ -50,6 +50,15 @@ test_expect_success 'status with modified file in submodule (porcelain)' '
 	EOF
 '
 
+test_expect_success 'status with modified file in submodule (short)' '
+	(cd sub && git reset --hard) &&
+	echo "changed" >sub/foo &&
+	git status --short >output &&
+	diff output - <<-\EOF
+	 m sub
+	EOF
+'
+
 test_expect_success 'status with added file in submodule' '
 	(cd sub && git reset --hard && echo >foo && git add foo) &&
 	git status >output &&
@@ -64,6 +73,14 @@ test_expect_success 'status with added file in submodule (porcelain)' '
 	EOF
 '
 
+test_expect_success 'status with added file in submodule (short)' '
+	(cd sub && git reset --hard && echo >foo && git add foo) &&
+	git status --short >output &&
+	diff output - <<-\EOF
+	 m sub
+	EOF
+'
+
 test_expect_success 'status with untracked file in submodule' '
 	(cd sub && git reset --hard) &&
 	echo "content" >sub/new-file &&
@@ -83,6 +100,13 @@ test_expect_success 'status with untracked file in submodule (porcelain)' '
 	EOF
 '
 
+test_expect_success 'status with untracked file in submodule (short)' '
+	git status --short >output &&
+	diff output - <<-\EOF
+	 ? sub
+	EOF
+'
+
 test_expect_success 'status with added and untracked file in submodule' '
 	(cd sub && git reset --hard && echo >foo && git add foo) &&
 	echo "content" >sub/new-file &&
diff --git a/wt-status.c b/wt-status.c
index 308cf3779e..9909fd0e57 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -431,10 +431,19 @@ static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
 		}
 		if (!d->worktree_status)
 			d->worktree_status = p->status;
-		d->dirty_submodule = p->two->dirty_submodule;
-		if (S_ISGITLINK(p->two->mode))
+		if (S_ISGITLINK(p->two->mode)) {
+			d->dirty_submodule = p->two->dirty_submodule;
 			d->new_submodule_commits = !!oidcmp(&p->one->oid,
 							    &p->two->oid);
+			if (s->status_format == STATUS_FORMAT_SHORT) {
+				if (d->new_submodule_commits)
+					d->worktree_status = 'M';
+				else if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
+					d->worktree_status = 'm';
+				else if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
+					d->worktree_status = '?';
+			}
+		}
 
 		switch (p->status) {
 		case DIFF_STATUS_ADDED:
-- 
2.12.1.432.gfe308fe33c.dirty
Previous: Jonathan NiederNext: Jonathan Nieder
Message 8 of 73 in “[PATCHv3 0/3] short status: improve reporting for submodule changes”
  1. Stefan BellerMar 23, 2017
  2. 1/3 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  3. Jonathan NiederMar 23, 2017
  4. Junio C HamanoMar 23, 2017
  5. Stefan BellerMar 23, 2017
  6. 2/3 submodule.c, is_submodule_modified: stricter checking for submodulesStefan Beller, Mar 23, 2017
  7. Jonathan NiederMar 23, 2017
  8. 3/3 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  9. Jonathan NiederMar 23, 2017
  10. 0/8 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  11. 3/8 submodule.c: convert is_submodule_modified to use strbuf_getwholeline_fdStefan Beller, Mar 23, 2017
  12. 1/8 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  13. 5/8 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 23, 2017
  14. 2/8 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 23, 2017
  15. 4/8 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  16. 8/8 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017
  17. 6/8 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017
  18. 7/8 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  19. Stefan BellerMar 23, 2017
  20. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  21. 1/7 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 23, 2017
  22. Jonathan NiederMar 23, 2017
  23. 4/7 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 23, 2017
  24. 5/7 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017
  25. 2/7 submodule.c: convert is_submodule_modified to use strbuf_getwholeline_fdStefan Beller, Mar 23, 2017
  26. Jonathan NiederMar 23, 2017
  27. Stefan BellerMar 23, 2017
  28. Stefan BellerMar 23, 2017
  29. 6/7 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  30. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 24, 2017
  31. 1/7 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 24, 2017
  32. Jonathan NiederMar 24, 2017
  33. 2/7 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 24, 2017
  34. Jonathan NiederMar 24, 2017
  35. 3/7 submodule.c: convert is_submodule_modified to use strbuf_getwholelineStefan Beller, Mar 24, 2017
  36. Jonathan NiederMar 24, 2017
  37. Stefan BellerMar 25, 2017
  38. 5/7 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 24, 2017
  39. Jonathan NiederMar 24, 2017
  40. 6/7 short status: improve reporting for submodule changesStefan Beller, Mar 24, 2017
  41. Jonathan NiederMar 24, 2017
  42. 7/7 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 24, 2017
  43. Jonathan NiederMar 24, 2017
  44. Stefan BellerMar 25, 2017
  45. 4/7 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 24, 2017
  46. Jonathan NiederMar 24, 2017
  47. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 25, 2017
  48. 1/7 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 25, 2017
  49. 2/7 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 25, 2017
  50. 3/7 submodule.c: convert is_submodule_modified to use strbuf_getwholelineStefan Beller, Mar 25, 2017
  51. 4/7 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 25, 2017
  52. 7/7 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 25, 2017
  53. Junio C HamanoMar 27, 2017
  54. Jonathan NiederMar 28, 2017
  55. Junio C HamanoMar 30, 2017
  56. Stefan BellerMar 28, 2017
  57. 6/7 short status: improve reporting for submodule changesStefan Beller, Mar 25, 2017
  58. 5/7 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 25, 2017
  59. Jonathan NiederMar 25, 2017
  60. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 28, 2017
  61. 2/2 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 28, 2017
  62. Jonathan NiederMar 28, 2017
  63. Stefan BellerMar 29, 2017
  64. 1/2 short status: improve reporting for submodule changesStefan Beller, Mar 28, 2017
  65. Jonathan NiederMar 28, 2017
  66. [PATCHv9 (6,7)/7] short status: improve reporting for submodule changesStefan Beller, Mar 29, 2017
  67. 1/2 short status: improve reporting for submodule changesStefan Beller, Mar 29, 2017
  68. Jonathan NiederMar 29, 2017
  69. 2/2 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 29, 2017
  70. Jonathan NiederMar 29, 2017
  71. Junio C HamanoMar 30, 2017
  72. 3/7 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  73. 7/7 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.