Skip to content

Commit fa8ae45

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: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef1b5f4 commit fa8ae45

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
@@ -968,7 +968,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
968968
}
969969

970970
static int stash_patch(struct stash_info *info, struct pathspec ps,
971-
struct strbuf *out_patch)
971+
struct strbuf *out_patch, int quiet)
972972
{
973973
int ret = 0;
974974
struct strbuf out = STRBUF_INIT;
@@ -1021,7 +1021,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10211021
}
10221022

10231023
if (!out_patch->len) {
1024-
fprintf_ln(stderr, _("No changes selected"));
1024+
if (!quiet)
1025+
fprintf_ln(stderr, _("No changes selected"));
10251026
ret = 1;
10261027
}
10271028

@@ -1100,7 +1101,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
11001101

11011102
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11021103
int include_untracked, int patch_mode,
1103-
struct stash_info *info, struct strbuf *patch)
1104+
struct stash_info *info, struct strbuf *patch,
1105+
int quiet)
11041106
{
11051107
int ret = 0;
11061108
int flags = 0;
@@ -1120,7 +1122,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11201122
refresh_cache(REFRESH_QUIET);
11211123

11221124
if (get_oid("HEAD", &info->b_commit)) {
1123-
fprintf_ln(stderr, _("You do not have the initial commit yet"));
1125+
if (!quiet)
1126+
fprintf_ln(stderr, _("You do not have "
1127+
"the initial commit yet"));
11241128
ret = -1;
11251129
goto done;
11261130
} else {
@@ -1145,34 +1149,39 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11451149
if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
11461150
commit_tree(commit_tree_label.buf, commit_tree_label.len,
11471151
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1148-
fprintf_ln(stderr, _("Cannot save the current index state"));
1152+
if (!quiet)
1153+
fprintf_ln(stderr, _("Cannot save the current "
1154+
"index state"));
11491155
ret = -1;
11501156
goto done;
11511157
}
11521158

11531159
if (include_untracked && get_untracked_files(ps, include_untracked,
11541160
&untracked_files)) {
11551161
if (save_untracked_files(info, &msg, untracked_files)) {
1156-
fprintf_ln(stderr, _("Cannot save "
1157-
"the untracked files"));
1162+
if (!quiet)
1163+
fprintf_ln(stderr, _("Cannot save "
1164+
"the untracked files"));
11581165
ret = -1;
11591166
goto done;
11601167
}
11611168
untracked_commit_option = 1;
11621169
}
11631170
if (patch_mode) {
1164-
ret = stash_patch(info, ps, patch);
1171+
ret = stash_patch(info, ps, patch, quiet);
11651172
if (ret < 0) {
1166-
fprintf_ln(stderr, _("Cannot save the current "
1167-
"worktree state"));
1173+
if (!quiet)
1174+
fprintf_ln(stderr, _("Cannot save the current "
1175+
"worktree state"));
11681176
goto done;
11691177
} else if (ret > 0) {
11701178
goto done;
11711179
}
11721180
} else {
11731181
if (stash_working_tree(info, ps)) {
1174-
fprintf_ln(stderr, _("Cannot save the current "
1175-
"worktree state"));
1182+
if (!quiet)
1183+
fprintf_ln(stderr, _("Cannot save the current "
1184+
"worktree state"));
11761185
ret = -1;
11771186
goto done;
11781187
}
@@ -1198,7 +1207,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11981207

11991208
if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
12001209
parents, &info->w_commit, NULL, NULL)) {
1201-
fprintf_ln(stderr, _("Cannot record working tree state"));
1210+
if (!quiet)
1211+
fprintf_ln(stderr, _("Cannot record "
1212+
"working tree state"));
12021213
ret = -1;
12031214
goto done;
12041215
}
@@ -1233,7 +1244,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12331244
memset(&ps, 0, sizeof(ps));
12341245
strbuf_addstr(&stash_msg_buf, stash_msg);
12351246
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1236-
NULL);
1247+
NULL, 0);
12371248
if (!ret)
12381249
printf_ln("%s", oid_to_hex(&info.w_commit));
12391250

@@ -1295,26 +1306,29 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12951306

12961307
if (!reflog_exists(ref_stash) && do_clear_stash()) {
12971308
ret = -1;
1298-
fprintf_ln(stderr, _("Cannot initialize stash"));
1309+
if (!quiet)
1310+
fprintf_ln(stderr, _("Cannot initialize stash"));
12991311
goto done;
13001312
}
13011313

13021314
if (stash_msg)
13031315
strbuf_addstr(&stash_msg_buf, stash_msg);
13041316
if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1305-
&info, &patch)) {
1317+
&info, &patch, quiet)) {
13061318
ret = -1;
13071319
goto done;
13081320
}
13091321

13101322
if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
13111323
ret = -1;
1312-
fprintf_ln(stderr, _("Cannot save the current status"));
1324+
if (!quiet)
1325+
fprintf_ln(stderr, _("Cannot save the current status"));
13131326
goto done;
13141327
}
13151328

1316-
printf_ln(_("Saved working directory and index state %s"),
1317-
stash_msg_buf.buf);
1329+
if (!quiet)
1330+
printf_ln(_("Saved working directory and index state %s"),
1331+
stash_msg_buf.buf);
13181332

13191333
if (!patch_mode) {
13201334
if (include_untracked && !ps.nr) {
@@ -1416,7 +1430,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
14161430
argv_array_pushl(&cp.args, "apply", "-R", NULL);
14171431

14181432
if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1419-
fprintf_ln(stderr, _("Cannot remove worktree changes"));
1433+
if (!quiet)
1434+
fprintf_ln(stderr, _("Cannot remove "
1435+
"worktree changes"));
14201436
ret = -1;
14211437
goto done;
14221438
}

t/t3903-stash.sh

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

1075+
test_expect_success 'push: -q is quiet with changes' '
1076+
>foo &&
1077+
git add foo &&
1078+
git stash push -q >output 2>&1 &&
1079+
test_must_be_empty output
1080+
'
1081+
1082+
test_expect_success 'push: -q is quiet with no changes' '
1083+
git stash push -q >output 2>&1 &&
1084+
test_must_be_empty output
1085+
'
1086+
1087+
test_expect_success 'push: -q is quiet even if there is no initial commit' '
1088+
git init foo_dir &&
1089+
test_when_finished rm -rf foo_dir &&
1090+
(
1091+
cd foo_dir &&
1092+
>bar &&
1093+
test_must_fail git stash push -q >output 2>&1 &&
1094+
test_must_be_empty output
1095+
)
1096+
'
1097+
10751098
test_expect_success 'untracked files are left in place when -u is not given' '
10761099
>file &&
10771100
git add file &&

0 commit comments

Comments
 (0)