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

Re: Please default to 'commit -a' when no changes were added

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 22, 2010, 15:58 UTC
Message-ID
<20100422155806.GC4801@progeny.tock>
In-Reply-To
<20100422151037.2310.2429.reportbug@frosties.localdomain>

[topic: making ‘git commit’ more helpful when there are no changes registered in the index]

Hi Goswin,
Goswin von Brederlow wrote:
> in most (all but git?) RCS a plain 'commit' without any arguments
> commits all changes (to registered files).
Yes, but they are wrong. :)
> no changes added to commit (use "git add" and/or "git commit -a")
[...]
> Imho in most cases where no changes
> were added people do want to commit all modified files. And if not
> then exiting the editor to abort is easy enough.

I absent-mindedly type ‘git commit’ having forgotten to update the index with my changes fairly often. Then I add the appropriate changes, which is almost never all of them. I don’t think this is so unusual.

Starting out, I can see how it would be comforting to people if ‘git commit’ would default to -a behavior if they ignore the index. That is logically a different operation, though, so it would also send a wrong message and make it harder in the long run to get used to the interface.

Instead, I think it would be better to focus on making the error message more helpful. Right now there is a screen full of status before the advice, which might make it easy to get scared before reading it.

Here’s a very rough patch to suppress that screenful. What do you think?

diff --git a/builtin/commit.c b/builtin/commit.c
index c5ab683..9cb5489 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -396,7 +396,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
 }
 
 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
-		      struct wt_status *s)
+		      struct wt_status *s, int simple)
 {
 	unsigned char sha1[20];
 
@@ -415,6 +415,13 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
 
 	wt_status_collect(s);
 
+	if (simple) {
+		if (s->commitable)
+			die("internal error: are there changes or not?");
+		wt_status_print_nochanges(s);
+		return 0;
+	}
+
 	switch (status_format) {
 	case STATUS_FORMAT_SHORT:
 		wt_shortstatus_print(s, null_termination);
@@ -670,7 +677,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 
 		saved_color_setting = s->use_color;
 		s->use_color = 0;
-		commitable = run_status(fp, index_file, prefix, 1, s);
+		commitable = run_status(fp, index_file, prefix, 1, s, 0);
 		s->use_color = saved_color_setting;
 	} else {
 		unsigned char sha1[20];
@@ -692,7 +699,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 
 	if (!commitable && !in_merge && !allow_empty &&
 	    !(amend && is_a_merge(head_sha1))) {
-		run_status(stdout, index_file, prefix, 0, s);
+		run_status(stdout, index_file, prefix, 0, s, 1);
 		return 0;
 	}
 
@@ -946,7 +953,7 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
 	const char *index_file;
 
 	index_file = prepare_index(argc, argv, prefix, 1);
-	commitable = run_status(stdout, index_file, prefix, 0, s);
+	commitable = run_status(stdout, index_file, prefix, 0, s, 0);
 	rollback_index_files();
 
 	return commitable ? 0 : 1;
diff --git a/wt-status.c b/wt-status.c
index 8ca59a2..b50bf71 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -589,6 +589,24 @@ static void wt_status_print_tracking(struct wt_status *s)
 	color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
 }
 
+void wt_status_print_nochanges(struct wt_status *s)
+{
+	if (s->amend)
+		fprintf(s->fp, "# No changes\n");
+	else if (s->nowarn)
+		; /* nothing */
+	else if (s->workdir_dirty)
+		printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
+	else if (s->untracked.nr)
+		printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
+	else if (s->is_initial)
+		printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
+	else if (!s->show_untracked_files)
+		printf("nothing to commit (use -u to show untracked files)\n");
+	else
+		printf("nothing to commit (working directory clean)\n");
+}
+
 void wt_status_print(struct wt_status *s)
 {
 	const char *branch_color = color(WT_STATUS_HEADER, s);
@@ -629,22 +647,8 @@ void wt_status_print(struct wt_status *s)
 
 	if (s->verbose)
 		wt_status_print_verbose(s);
-	if (!s->commitable) {
-		if (s->amend)
-			fprintf(s->fp, "# No changes\n");
-		else if (s->nowarn)
-			; /* nothing */
-		else if (s->workdir_dirty)
-			printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
-		else if (s->untracked.nr)
-			printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
-		else if (s->is_initial)
-			printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
-		else if (!s->show_untracked_files)
-			printf("nothing to commit (use -u to show untracked files)\n");
-		else
-			printf("nothing to commit (working directory clean)\n");
-	}
+	if (!s->commitable)
+		wt_status_print_nochanges(s);
 }
 
 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
diff --git a/wt-status.h b/wt-status.h
index 9120673..f249955 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -59,6 +59,8 @@ void wt_status_prepare(struct wt_status *s);
 void wt_status_print(struct wt_status *s);
 void wt_status_collect(struct wt_status *s);
 
+void wt_status_print_nochanges(struct wt_status *s);
+
 void wt_shortstatus_print(struct wt_status *s, int null_termination);
 void wt_porcelain_print(struct wt_status *s, int null_termination);
 
Next: Goswin von Brederlow
Message 1 of 76 in “Re: Please default to 'commit -a' when no changes were added”
  1. Jonathan NiederApr 22, 2010
  2. Goswin von BrederlowApr 22, 2010
  3. Nicolas PitreApr 22, 2010
  4. Sverre RabbelierApr 22, 2010
  5. Goswin von BrederlowApr 22, 2010
  6. Nicolas PitreApr 22, 2010
  7. Goswin von BrederlowApr 23, 2010
  8. Miles BaderApr 23, 2010
  9. Wincent ColaiutaApr 23, 2010
  10. Goswin von BrederlowApr 23, 2010
  11. Michael WittenApr 23, 2010
  12. Daniel GraceApr 23, 2010
  13. Nicolas PitreApr 23, 2010
  14. Goswin von BrederlowApr 24, 2010
  15. Jonathan NiederApr 24, 2010
  16. Goswin von BrederlowApr 24, 2010
  17. Jonathan NiederApr 24, 2010
  18. Miles BaderApr 25, 2010
  19. Jonathan NiederApr 25, 2010
  20. Matthias AndreeApr 23, 2010
  21. Junio C HamanoApr 24, 2010
  22. Junio C HamanoApr 22, 2010
  23. Matthieu MoyApr 22, 2010
  24. Michael WittenApr 22, 2010
  25. Goswin von BrederlowApr 23, 2010
  26. Tomas CarneckyApr 23, 2010
  27. Michael WittenApr 23, 2010
  28. Matthieu MoyApr 23, 2010
  29. Tor ArntsenApr 23, 2010
  30. Adam BrewsterApr 22, 2010
  31. Jonathan NiederApr 22, 2010
  32. Goswin von BrederlowApr 23, 2010
  33. The index (Re: Please default to 'commit -a' when no changes were added)Jonathan Nieder, Apr 23, 2010
  34. Jon SeymourApr 22, 2010
  35. Adam BrewsterApr 23, 2010
  36. Goswin von BrederlowApr 23, 2010
  37. Goswin von BrederlowApr 23, 2010
  38. Björn SteinbrinkApr 23, 2010
  39. Sergei OrganovApr 23, 2010
  40. Sverre RabbelierApr 23, 2010
  41. Sergei OrganovApr 23, 2010
  42. Goswin von BrederlowApr 23, 2010
  43. Matthias AndreeApr 23, 2010
  44. Michael WittenApr 23, 2010
  45. Matthias AndreeApr 23, 2010
  46. Eric RaymondApr 23, 2010
  47. Michael WittenApr 23, 2010
  48. Eric RaymondApr 24, 2010
  49. Michael WittenApr 24, 2010
  50. Eric RaymondApr 24, 2010
  51. Michael WittenApr 23, 2010
  52. Tor ArntsenApr 24, 2010
  53. 'commit -a' safety (was: Re: Please default to 'commit -a' when no changes were added)Jakub Narebski, Apr 24, 2010
  54. Miles BaderApr 24, 2010
  55. Andreas SchwabApr 24, 2010
  56. Jakub NarebskiApr 24, 2010
  57. Miles BaderApr 24, 2010
  58. Nicolas PitreApr 24, 2010
  59. Jakub NarebskiApr 25, 2010
  60. Miles BaderApr 25, 2010
  61. Wincent ColaiutaApr 24, 2010
  62. Jakub NarebskiApr 24, 2010
  63. Joey HessApr 24, 2010
  64. Mike HommeyApr 24, 2010
  65. Petr BaudisApr 24, 2010
  66. Bug#578764: 'commit -a' safety (was: Re: Please default to 'commit -a' when no changes were added)Wincent Colaiuta, Apr 24, 2010
  67. Petr BaudisApr 24, 2010
  68. Nicolas PitreApr 24, 2010
  69. Petr BaudisApr 24, 2010
  70. Nicolas PitreApr 24, 2010
  71. Jacob HelwigApr 24, 2010
  72. Nicolas PitreApr 24, 2010
  73. Jacob HelwigApr 24, 2010
  74. Jakub NarebskiApr 24, 2010
  75. Junio C HamanoApr 25, 2010
  76. Jakub NarebskiApr 25, 2010

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.