Skip to content

Commit dd8f5b8

Browse files
ungpsdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f9a4ad commit dd8f5b8

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

builtin/stash--helper.c

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
973973
}
974974

975975
static int stash_patch(struct stash_info *info, struct pathspec ps,
976-
struct strbuf *out_patch)
976+
struct strbuf *out_patch, int quiet)
977977
{
978978
int ret = 0;
979979
struct strbuf out = STRBUF_INIT;
@@ -1026,7 +1026,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10261026
}
10271027

10281028
if (!out_patch->len) {
1029-
fprintf_ln(stderr, _("No changes selected"));
1029+
if (!quiet)
1030+
fprintf_ln(stderr, _("No changes selected"));
10301031
ret = 1;
10311032
}
10321033

@@ -1104,7 +1105,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
11041105

11051106
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11061107
int include_untracked, int patch_mode,
1107-
struct stash_info *info, struct strbuf *patch)
1108+
struct stash_info *info, struct strbuf *patch,
1109+
int quiet)
11081110
{
11091111
int ret = 0;
11101112
int flags = 0;
@@ -1124,7 +1126,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11241126
refresh_cache(REFRESH_QUIET);
11251127

11261128
if (get_oid("HEAD", &info->b_commit)) {
1127-
fprintf_ln(stderr, _("You do not have the initial commit yet"));
1129+
if (!quiet)
1130+
fprintf_ln(stderr, _("You do not have "
1131+
"the initial commit yet"));
11281132
ret = -1;
11291133
goto done;
11301134
} else {
@@ -1149,34 +1153,39 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11491153
if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
11501154
commit_tree(commit_tree_label.buf, commit_tree_label.len,
11511155
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1152-
fprintf_ln(stderr, _("Cannot save the current index state"));
1156+
if (!quiet)
1157+
fprintf_ln(stderr, _("Cannot save the current "
1158+
"index state"));
11531159
ret = -1;
11541160
goto done;
11551161
}
11561162

11571163
if (include_untracked && get_untracked_files(ps, include_untracked,
11581164
&untracked_files)) {
11591165
if (save_untracked_files(info, &msg, untracked_files)) {
1160-
fprintf_ln(stderr, _("Cannot save "
1161-
"the untracked files"));
1166+
if (!quiet)
1167+
fprintf_ln(stderr, _("Cannot save "
1168+
"the untracked files"));
11621169
ret = -1;
11631170
goto done;
11641171
}
11651172
untracked_commit_option = 1;
11661173
}
11671174
if (patch_mode) {
1168-
ret = stash_patch(info, ps, patch);
1175+
ret = stash_patch(info, ps, patch, quiet);
11691176
if (ret < 0) {
1170-
fprintf_ln(stderr, _("Cannot save the current "
1171-
"worktree state"));
1177+
if (!quiet)
1178+
fprintf_ln(stderr, _("Cannot save the current "
1179+
"worktree state"));
11721180
goto done;
11731181
} else if (ret > 0) {
11741182
goto done;
11751183
}
11761184
} else {
11771185
if (stash_working_tree(info, ps)) {
1178-
fprintf_ln(stderr, _("Cannot save the current "
1179-
"worktree state"));
1186+
if (!quiet)
1187+
fprintf_ln(stderr, _("Cannot save the current "
1188+
"worktree state"));
11801189
ret = -1;
11811190
goto done;
11821191
}
@@ -1202,7 +1211,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
12021211

12031212
if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
12041213
parents, &info->w_commit, NULL, NULL)) {
1205-
fprintf_ln(stderr, _("Cannot record working tree state"));
1214+
if (!quiet)
1215+
fprintf_ln(stderr, _("Cannot record "
1216+
"working tree state"));
12061217
ret = -1;
12071218
goto done;
12081219
}
@@ -1237,7 +1248,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12371248
memset(&ps, 0, sizeof(ps));
12381249
strbuf_addstr(&stash_msg_buf, stash_msg);
12391250
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1240-
NULL);
1251+
NULL, 0);
12411252

12421253
if (!ret)
12431254
printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1300,26 +1311,29 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13001311

13011312
if (!reflog_exists(ref_stash) && do_clear_stash()) {
13021313
ret = -1;
1303-
fprintf_ln(stderr, _("Cannot initialize stash"));
1314+
if (!quiet)
1315+
fprintf_ln(stderr, _("Cannot initialize stash"));
13041316
goto done;
13051317
}
13061318

13071319
if (stash_msg)
13081320
strbuf_addstr(&stash_msg_buf, stash_msg);
13091321
if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1310-
&info, &patch)) {
1322+
&info, &patch, quiet)) {
13111323
ret = -1;
13121324
goto done;
13131325
}
13141326

13151327
if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
13161328
ret = -1;
1317-
fprintf_ln(stderr, _("Cannot save the current status"));
1329+
if (!quiet)
1330+
fprintf_ln(stderr, _("Cannot save the current status"));
13181331
goto done;
13191332
}
13201333

1321-
printf_ln(_("Saved working directory and index state %s"),
1322-
stash_msg_buf.buf);
1334+
if (!quiet)
1335+
printf_ln(_("Saved working directory and index state %s"),
1336+
stash_msg_buf.buf);
13231337

13241338
if (!patch_mode) {
13251339
if (include_untracked && !ps.nr) {
@@ -1420,7 +1434,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
14201434
argv_array_pushl(&cp.args, "apply", "-R", NULL);
14211435

14221436
if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1423-
fprintf_ln(stderr, _("Cannot remove worktree changes"));
1437+
if (!quiet)
1438+
fprintf_ln(stderr, _("Cannot remove "
1439+
"worktree changes"));
14241440
ret = -1;
14251441
goto done;
14261442
}

t/t3903-stash.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
10641064
test_path_is_file untracked
10651065
'
10661066

1067+
test_expect_success 'push: -q is quiet with changes' '
1068+
>foo &&
1069+
git add foo &&
1070+
git stash push -q >output 2>&1 &&
1071+
test_must_be_empty output
1072+
'
1073+
1074+
test_expect_success 'push: -q is quiet with no changes' '
1075+
git stash push -q >output 2>&1 &&
1076+
test_must_be_empty output
1077+
'
1078+
1079+
test_expect_success 'push: -q is quiet even if there is no initial commit' '
1080+
git init foo_dir &&
1081+
test_when_finished rm -rf foo_dir &&
1082+
(
1083+
cd foo_dir &&
1084+
>bar &&
1085+
test_must_fail git stash push -q >output 2>&1 &&
1086+
test_must_be_empty output
1087+
)
1088+
'
1089+
10671090
test_expect_success 'untracked files are left in place when -u is not given' '
10681091
>file &&
10691092
git add file &&

0 commit comments

Comments
 (0)