stash: make push -q quiet

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>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Paul-Sebastian Ungureanu 2019-02-25 23:16:24 +00:00 committed by Junio C Hamano
parent d553f538b8
commit 1ac528c0b0
2 changed files with 59 additions and 20 deletions

View File

@ -966,7 +966,7 @@ done:
} }
static int stash_patch(struct stash_info *info, struct pathspec ps, static int stash_patch(struct stash_info *info, struct pathspec ps,
struct strbuf *out_patch) struct strbuf *out_patch, int quiet)
{ {
int ret = 0; int ret = 0;
struct strbuf out = STRBUF_INIT; struct strbuf out = STRBUF_INIT;
@ -1019,7 +1019,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
} }
if (!out_patch->len) { if (!out_patch->len) {
fprintf_ln(stderr, _("No changes selected")); if (!quiet)
fprintf_ln(stderr, _("No changes selected"));
ret = 1; ret = 1;
} }
@ -1098,7 +1099,8 @@ done:
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
int include_untracked, int patch_mode, 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 ret = 0;
int flags = 0; int flags = 0;
@ -1118,7 +1120,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (get_oid("HEAD", &info->b_commit)) { 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; ret = -1;
goto done; goto done;
} else { } else {
@ -1143,7 +1147,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
if (write_cache_as_tree(&info->i_tree, 0, NULL) || if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
commit_tree(commit_tree_label.buf, commit_tree_label.len, commit_tree(commit_tree_label.buf, commit_tree_label.len,
&info->i_tree, parents, &info->i_commit, NULL, NULL)) { &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; ret = -1;
goto done; goto done;
} }
@ -1151,26 +1157,29 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
if (include_untracked && get_untracked_files(ps, include_untracked, if (include_untracked && get_untracked_files(ps, include_untracked,
&untracked_files)) { &untracked_files)) {
if (save_untracked_files(info, &msg, untracked_files)) { if (save_untracked_files(info, &msg, untracked_files)) {
fprintf_ln(stderr, _("Cannot save " if (!quiet)
"the untracked files")); fprintf_ln(stderr, _("Cannot save "
"the untracked files"));
ret = -1; ret = -1;
goto done; goto done;
} }
untracked_commit_option = 1; untracked_commit_option = 1;
} }
if (patch_mode) { if (patch_mode) {
ret = stash_patch(info, ps, patch); ret = stash_patch(info, ps, patch, quiet);
if (ret < 0) { if (ret < 0) {
fprintf_ln(stderr, _("Cannot save the current " if (!quiet)
"worktree state")); fprintf_ln(stderr, _("Cannot save the current "
"worktree state"));
goto done; goto done;
} else if (ret > 0) { } else if (ret > 0) {
goto done; goto done;
} }
} else { } else {
if (stash_working_tree(info, ps)) { if (stash_working_tree(info, ps)) {
fprintf_ln(stderr, _("Cannot save the current " if (!quiet)
"worktree state")); fprintf_ln(stderr, _("Cannot save the current "
"worktree state"));
ret = -1; ret = -1;
goto done; goto done;
} }
@ -1196,7 +1205,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree, if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
parents, &info->w_commit, NULL, NULL)) { 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; ret = -1;
goto done; goto done;
} }
@ -1231,7 +1242,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
memset(&ps, 0, sizeof(ps)); memset(&ps, 0, sizeof(ps));
strbuf_addstr(&stash_msg_buf, stash_msg); strbuf_addstr(&stash_msg_buf, stash_msg);
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info, ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
NULL); NULL, 0);
if (!ret) if (!ret)
printf_ln("%s", oid_to_hex(&info.w_commit)); printf_ln("%s", oid_to_hex(&info.w_commit));
@ -1293,26 +1304,29 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
if (!reflog_exists(ref_stash) && do_clear_stash()) { if (!reflog_exists(ref_stash) && do_clear_stash()) {
ret = -1; ret = -1;
fprintf_ln(stderr, _("Cannot initialize stash")); if (!quiet)
fprintf_ln(stderr, _("Cannot initialize stash"));
goto done; goto done;
} }
if (stash_msg) if (stash_msg)
strbuf_addstr(&stash_msg_buf, stash_msg); strbuf_addstr(&stash_msg_buf, stash_msg);
if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
&info, &patch)) { &info, &patch, quiet)) {
ret = -1; ret = -1;
goto done; goto done;
} }
if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) { if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
ret = -1; ret = -1;
fprintf_ln(stderr, _("Cannot save the current status")); if (!quiet)
fprintf_ln(stderr, _("Cannot save the current status"));
goto done; goto done;
} }
printf_ln(_("Saved working directory and index state %s"), if (!quiet)
stash_msg_buf.buf); printf_ln(_("Saved working directory and index state %s"),
stash_msg_buf.buf);
if (!patch_mode) { if (!patch_mode) {
if (include_untracked && !ps.nr) { if (include_untracked && !ps.nr) {
@ -1414,7 +1428,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
argv_array_pushl(&cp.args, "apply", "-R", NULL); argv_array_pushl(&cp.args, "apply", "-R", NULL);
if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) { 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; ret = -1;
goto done; goto done;
} }

View File

@ -1072,6 +1072,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
test_path_is_file untracked 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' ' test_expect_success 'untracked files are left in place when -u is not given' '
>file && >file &&
git add file && git add file &&