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

[PATCH v10 17/21] stash: make push -q quiet

From
Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
Date
Oct 14, 2018, 22:11 UTC
Message-ID
<ea8814762c75a3387b9e697f62ea6122b5a1d014.1539553398.git.ungureanupaulsebastian@gmail.com>
In-Reply-To
<cover.1539553398.git.ungureanupaulsebastian@gmail.com>

There is a change in behaviour with this commit. When there was no initial commit, the shell version of stash would still display a message. This commit makes `push` to not display any message if `--quiet` or `-q` is specified. Add tests for `--quiet`.

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash--helper.c | 55 +++++++++++++++++++++++++++--------------
 t/t3903-stash.sh        | 23 +++++++++++++++++
 2 files changed, 59 insertions(+), 19 deletions(-)
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
index 23f1329637..befb944871 100644
--- a/builtin/stash--helper.c
+++ b/builtin/stash--helper.c
@@ -973,7 +973,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
 }
 
 static int stash_patch(struct stash_info *info, struct pathspec ps,
-		       struct strbuf *out_patch)
+		       struct strbuf *out_patch, int quiet)
 {
 	int ret = 0;
 	struct strbuf out = STRBUF_INIT;
@@ -1026,7 +1026,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
 	}
 
 	if (!out_patch->len) {
-		fprintf_ln(stderr, _("No changes selected"));
+		if (!quiet)
+			fprintf_ln(stderr, _("No changes selected"));
 		ret = 1;
 	}
 
@@ -1105,7 +1106,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
 
 static int do_create_stash(struct pathspec ps, char **stash_msg,
 			   int include_untracked, int patch_mode,
-			   struct stash_info *info, struct strbuf *patch)
+			   struct stash_info *info, struct strbuf *patch,
+			   int quiet)
 {
 	int ret = 0;
 	int flags = 0;
@@ -1124,7 +1126,9 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,
 	refresh_cache(REFRESH_QUIET);
 
 	if (get_oid("HEAD", &info->b_commit)) {
-		fprintf_ln(stderr, _("You do not have the initial commit yet"));
+		if (!quiet)
+			fprintf_ln(stderr, _("You do not have "
+					     "the initial commit yet"));
 		ret = -1;
 		*stash_msg = NULL;
 		goto done;
@@ -1151,7 +1155,9 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,
 	if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
 	    commit_tree(commit_tree_label.buf, commit_tree_label.len,
 			&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
-		fprintf_ln(stderr, _("Cannot save the current index state"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot save the current "
+					     "index state"));
 		ret = -1;
 		*stash_msg = NULL;
 		goto done;
@@ -1160,8 +1166,9 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,
 	if (include_untracked && get_untracked_files(ps, include_untracked,
 						     &untracked_files)) {
 		if (save_untracked_files(info, &msg, untracked_files)) {
-			fprintf_ln(stderr, _("Cannot save "
-					     "the untracked files"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot save "
+						     "the untracked files"));
 			ret = -1;
 			*stash_msg = NULL;
 			goto done;
@@ -1169,19 +1176,21 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,
 		untracked_commit_option = 1;
 	}
 	if (patch_mode) {
-		ret = stash_patch(info, ps, patch);
+		ret = stash_patch(info, ps, patch, quiet);
 		*stash_msg = NULL;
 		if (ret < 0) {
-			fprintf_ln(stderr, _("Cannot save the current "
-					     "worktree state"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot save the current "
+						     "worktree state"));
 			goto done;
 		} else if (ret > 0) {
 			goto done;
 		}
 	} else {
 		if (stash_working_tree(info, ps)) {
-			fprintf_ln(stderr, _("Cannot save the current "
-					     "worktree state"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot save the current "
+						     "worktree state"));
 			ret = -1;
 			*stash_msg = NULL;
 			goto done;
@@ -1210,7 +1219,9 @@ static int do_create_stash(struct pathspec ps, char **stash_msg,
 
 	if (commit_tree(*stash_msg, strlen(*stash_msg), &info->w_tree,
 			parents, &info->w_commit, NULL, NULL)) {
-		fprintf_ln(stderr, _("Cannot record working tree state"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot record "
+					     "working tree state"));
 		ret = -1;
 		goto done;
 	}
@@ -1244,7 +1255,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
 
 	memset(&ps, 0, sizeof(ps));
 	ret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,
-			      NULL);
+			      NULL, 0);
 
 	if (!ret)
 		printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1306,23 +1317,27 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,
 
 	if (!reflog_exists(ref_stash) && do_clear_stash()) {
 		ret = -1;
-		fprintf_ln(stderr, _("Cannot initialize stash"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot initialize stash"));
 		goto done;
 	}
 
 	if (do_create_stash(ps, &stash_msg, include_untracked, patch_mode,
-			    &info, &patch)) {
+			    &info, &patch, quiet)) {
 		ret = -1;
 		goto done;
 	}
 
 	if (do_store_stash(&info.w_commit, stash_msg, 1)) {
 		ret = -1;
-		fprintf_ln(stderr, _("Cannot save the current status"));
+		if (!quiet)
+			fprintf_ln(stderr, _("Cannot save the current status"));
 		goto done;
 	}
 
-	printf_ln(_("Saved working directory and index state %s"), stash_msg);
+	if (!quiet)
+		printf_ln(_("Saved working directory and index state %s"),
+			  stash_msg);
 
 	if (!patch_mode) {
 		if (include_untracked && !ps.nr) {
@@ -1423,7 +1438,9 @@ static int do_push_stash(struct pathspec ps, char *stash_msg, int quiet,
 		argv_array_pushl(&cp.args, "apply", "-R", NULL);
 
 		if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
-			fprintf_ln(stderr, _("Cannot remove worktree changes"));
+			if (!quiet)
+				fprintf_ln(stderr, _("Cannot remove "
+						     "worktree changes"));
 			ret = -1;
 			goto done;
 		}
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 3114c7bc4c..ed4611d3d8 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1064,6 +1064,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
 	test_path_is_file untracked
 '
 
+test_expect_success 'push: -q is quiet with changes' '
+	>foo &&
+	git add foo &&
+	git stash push -q >output 2>&1 &&
+	test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet with no changes' '
+	git stash push -q >output 2>&1 &&
+	test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet even if there is no initial commit' '
+	git init foo_dir &&
+	test_when_finished rm -rf foo_dir &&
+	(
+		cd foo_dir &&
+		>bar &&
+		test_must_fail git stash push -q >output 2>&1 &&
+		test_must_be_empty output
+	)
+'
+
 test_expect_success 'untracked files are left in place when -u is not given' '
 	>file &&
 	git add file &&
-- 
2.19.0.rc0.23.g10a62394e7
Previous: Paul-Sebastian UngureanuNext: Paul-Sebastian Ungureanu
Message 19 of 30 in “Convert "git stash" to C builtin”
  1. 00/21 Convert "git stash" to C builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  2. 01/21 sha1-name.c: add `get_oidf()` which acts like `get_oid()`Paul-Sebastian Ungureanu, Oct 14, 2018
  3. 02/21 strbuf.c: add `strbuf_join_argv()`Paul-Sebastian Ungureanu, Oct 14, 2018
  4. 03/21 stash: improve option parsing test coveragePaul-Sebastian Ungureanu, Oct 14, 2018
  5. 04/21 t3903: modernize stylePaul-Sebastian Ungureanu, Oct 14, 2018
  6. 05/21 stash: rename test cases to be more descriptivePaul-Sebastian Ungureanu, Oct 14, 2018
  7. 06/21 stash: add tests for `git stash show` configPaul-Sebastian Ungureanu, Oct 14, 2018
  8. 09/21 stash: convert drop and clear to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  9. 07/21 stash: mention options in `show` synopsisPaul-Sebastian Ungureanu, Oct 14, 2018
  10. 10/21 stash: convert branch to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  11. 11/21 stash: convert pop to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  12. 12/21 stash: convert list to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  13. 13/21 stash: convert show to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  14. 14/21 stash: convert store to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  15. 15/21 stash: convert create to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  16. 16/21 stash: convert push to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  17. 20/21 stash: optimize `get_untracked_files()` and `check_changes()`Paul-Sebastian Ungureanu, Oct 14, 2018
  18. 21/21 stash: replace all `write-tree` child processes with API callsPaul-Sebastian Ungureanu, Oct 14, 2018
  19. 17/21 stash: make push -q quietPaul-Sebastian Ungureanu, Oct 14, 2018
  20. 19/21 stash: convert `stash--helper.c` into `stash.c`Paul-Sebastian Ungureanu, Oct 14, 2018
  21. Thomas GummererOct 15, 2018
  22. 18/21 stash: convert save to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  23. Thomas GummererOct 15, 2018
  24. 08/21 stash: convert apply to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  25. Johannes SchindelinOct 15, 2018
  26. Thomas GummererOct 15, 2018
  27. Thomas GummererOct 15, 2018
  28. Junio C HamanoOct 16, 2018
  29. Johannes SchindelinOct 16, 2018
  30. Thomas GummererOct 16, 2018

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.