Skip to content

Commit ccc0582

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 5914750 commit ccc0582

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
@@ -971,7 +971,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
971971
}
972972

973973
static int stash_patch(struct stash_info *info, struct pathspec ps,
974-
struct strbuf *out_patch)
974+
struct strbuf *out_patch, int quiet)
975975
{
976976
int ret = 0;
977977
struct strbuf out = STRBUF_INIT;
@@ -1024,7 +1024,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10241024
}
10251025

10261026
if (!out_patch->len) {
1027-
fprintf_ln(stderr, _("No changes selected"));
1027+
if (!quiet)
1028+
fprintf_ln(stderr, _("No changes selected"));
10281029
ret = 1;
10291030
}
10301031

@@ -1102,7 +1103,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
11021103

11031104
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11041105
int include_untracked, int patch_mode,
1105-
struct stash_info *info, struct strbuf *patch)
1106+
struct stash_info *info, struct strbuf *patch,
1107+
int quiet)
11061108
{
11071109
int ret = 0;
11081110
int flags = 0;
@@ -1122,7 +1124,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11221124
refresh_cache(REFRESH_QUIET);
11231125

11241126
if (get_oid("HEAD", &info->b_commit)) {
1125-
fprintf_ln(stderr, _("You do not have the initial commit yet"));
1127+
if (!quiet)
1128+
fprintf_ln(stderr, _("You do not have "
1129+
"the initial commit yet"));
11261130
ret = -1;
11271131
goto done;
11281132
} else {
@@ -1147,34 +1151,39 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11471151
if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
11481152
commit_tree(commit_tree_label.buf, commit_tree_label.len,
11491153
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1150-
fprintf_ln(stderr, _("Cannot save the current index state"));
1154+
if (!quiet)
1155+
fprintf_ln(stderr, _("Cannot save the current "
1156+
"index state"));
11511157
ret = -1;
11521158
goto done;
11531159
}
11541160

11551161
if (include_untracked && get_untracked_files(ps, include_untracked,
11561162
&untracked_files)) {
11571163
if (save_untracked_files(info, &msg, untracked_files)) {
1158-
fprintf_ln(stderr, _("Cannot save "
1159-
"the untracked files"));
1164+
if (!quiet)
1165+
fprintf_ln(stderr, _("Cannot save "
1166+
"the untracked files"));
11601167
ret = -1;
11611168
goto done;
11621169
}
11631170
untracked_commit_option = 1;
11641171
}
11651172
if (patch_mode) {
1166-
ret = stash_patch(info, ps, patch);
1173+
ret = stash_patch(info, ps, patch, quiet);
11671174
if (ret < 0) {
1168-
fprintf_ln(stderr, _("Cannot save the current "
1169-
"worktree state"));
1175+
if (!quiet)
1176+
fprintf_ln(stderr, _("Cannot save the current "
1177+
"worktree state"));
11701178
goto done;
11711179
} else if (ret > 0) {
11721180
goto done;
11731181
}
11741182
} else {
11751183
if (stash_working_tree(info, ps)) {
1176-
fprintf_ln(stderr, _("Cannot save the current "
1177-
"worktree state"));
1184+
if (!quiet)
1185+
fprintf_ln(stderr, _("Cannot save the current "
1186+
"worktree state"));
11781187
ret = -1;
11791188
goto done;
11801189
}
@@ -1200,7 +1209,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
12001209

12011210
if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
12021211
parents, &info->w_commit, NULL, NULL)) {
1203-
fprintf_ln(stderr, _("Cannot record working tree state"));
1212+
if (!quiet)
1213+
fprintf_ln(stderr, _("Cannot record "
1214+
"working tree state"));
12041215
ret = -1;
12051216
goto done;
12061217
}
@@ -1235,7 +1246,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12351246
memset(&ps, 0, sizeof(ps));
12361247
strbuf_addstr(&stash_msg_buf, stash_msg);
12371248
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1238-
NULL);
1249+
NULL, 0);
12391250

12401251
if (!ret)
12411252
printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1298,26 +1309,29 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12981309

12991310
if (!reflog_exists(ref_stash) && do_clear_stash()) {
13001311
ret = -1;
1301-
fprintf_ln(stderr, _("Cannot initialize stash"));
1312+
if (!quiet)
1313+
fprintf_ln(stderr, _("Cannot initialize stash"));
13021314
goto done;
13031315
}
13041316

13051317
if (stash_msg)
13061318
strbuf_addstr(&stash_msg_buf, stash_msg);
13071319
if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1308-
&info, &patch)) {
1320+
&info, &patch, quiet)) {
13091321
ret = -1;
13101322
goto done;
13111323
}
13121324

13131325
if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
13141326
ret = -1;
1315-
fprintf_ln(stderr, _("Cannot save the current status"));
1327+
if (!quiet)
1328+
fprintf_ln(stderr, _("Cannot save the current status"));
13161329
goto done;
13171330
}
13181331

1319-
printf_ln(_("Saved working directory and index state %s"),
1320-
stash_msg_buf.buf);
1332+
if (!quiet)
1333+
printf_ln(_("Saved working directory and index state %s"),
1334+
stash_msg_buf.buf);
13211335

13221336
if (!patch_mode) {
13231337
if (include_untracked && !ps.nr) {
@@ -1418,7 +1432,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
14181432
argv_array_pushl(&cp.args, "apply", "-R", NULL);
14191433

14201434
if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1421-
fprintf_ln(stderr, _("Cannot remove worktree changes"));
1435+
if (!quiet)
1436+
fprintf_ln(stderr, _("Cannot remove "
1437+
"worktree changes"));
14221438
ret = -1;
14231439
goto done;
14241440
}

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)