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>

Paul-Sebastian Ungureanu committed Feb 25, 2019 at 23:16 UTC 1ac528c0b0c3f3e8a5faa75ea25f422fcbc2ba39
2 files changed +59 -20
builtin/stash--helper.c
+36 -20
@@ -966,7 +966,7 @@ done:
966 }
967
968 static int stash_patch(struct stash_info *info, struct pathspec ps,
969 - struct strbuf *out_patch)
969 + struct strbuf *out_patch, int quiet)
970 {
971 int ret = 0;
972 struct strbuf out = STRBUF_INIT;
@@ -1019,7 +1019,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
1019 }
1020
1021 if (!out_patch->len) {
1022 - fprintf_ln(stderr, _("No changes selected"));
1022 + if (!quiet)
1023 + fprintf_ln(stderr, _("No changes selected"));
1024 ret = 1;
1025 }
1026
@@ -1098,7 +1099,8 @@ done:
1099
1100 static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1101 int include_untracked, int patch_mode,
1101 - struct stash_info *info, struct strbuf *patch)
1102 + struct stash_info *info, struct strbuf *patch,
1103 + int quiet)
1104 {
1105 int ret = 0;
1106 int flags = 0;
@@ -1118,7 +1120,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1120 refresh_cache(REFRESH_QUIET);
1121
1122 if (get_oid("HEAD", &info->b_commit)) {
1121 - fprintf_ln(stderr, _("You do not have the initial commit yet"));
1123 + if (!quiet)
1124 + fprintf_ln(stderr, _("You do not have "
1125 + "the initial commit yet"));
1126 ret = -1;
1127 goto done;
1128 } else {
@@ -1143,7 +1147,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1147 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1148 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1149 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1146 - fprintf_ln(stderr, _("Cannot save the current index state"));
1150 + if (!quiet)
1151 + fprintf_ln(stderr, _("Cannot save the current "
1152 + "index state"));
1153 ret = -1;
1154 goto done;
1155 }
@@ -1151,26 +1157,29 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1157 if (include_untracked && get_untracked_files(ps, include_untracked,
1158 &untracked_files)) {
1159 if (save_untracked_files(info, &msg, untracked_files)) {
1154 - fprintf_ln(stderr, _("Cannot save "
1155 - "the untracked files"));
1160 + if (!quiet)
1161 + fprintf_ln(stderr, _("Cannot save "
1162 + "the untracked files"));
1163 ret = -1;
1164 goto done;
1165 }
1166 untracked_commit_option = 1;
1167 }
1168 if (patch_mode) {
1162 - ret = stash_patch(info, ps, patch);
1169 + ret = stash_patch(info, ps, patch, quiet);
1170 if (ret < 0) {
1164 - fprintf_ln(stderr, _("Cannot save the current "
1165 - "worktree state"));
1171 + if (!quiet)
1172 + fprintf_ln(stderr, _("Cannot save the current "
1173 + "worktree state"));
1174 goto done;
1175 } else if (ret > 0) {
1176 goto done;
1177 }
1178 } else {
1179 if (stash_working_tree(info, ps)) {
1172 - fprintf_ln(stderr, _("Cannot save the current "
1173 - "worktree state"));
1180 + if (!quiet)
1181 + fprintf_ln(stderr, _("Cannot save the current "
1182 + "worktree state"));
1183 ret = -1;
1184 goto done;
1185 }
@@ -1196,7 +1205,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1205
1206 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1207 parents, &info->w_commit, NULL, NULL)) {
1199 - fprintf_ln(stderr, _("Cannot record working tree state"));
1208 + if (!quiet)
1209 + fprintf_ln(stderr, _("Cannot record "
1210 + "working tree state"));
1211 ret = -1;
1212 goto done;
1213 }
@@ -1231,7 +1242,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1242 memset(&ps, 0, sizeof(ps));
1243 strbuf_addstr(&stash_msg_buf, stash_msg);
1244 ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1234 - NULL);
1245 + NULL, 0);
1246 if (!ret)
1247 printf_ln("%s", oid_to_hex(&info.w_commit));
1248
@@ -1293,26 +1304,29 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1304
1305 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1306 ret = -1;
1296 - fprintf_ln(stderr, _("Cannot initialize stash"));
1307 + if (!quiet)
1308 + fprintf_ln(stderr, _("Cannot initialize stash"));
1309 goto done;
1310 }
1311
1312 if (stash_msg)
1313 strbuf_addstr(&stash_msg_buf, stash_msg);
1314 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1303 - &info, &patch)) {
1315 + &info, &patch, quiet)) {
1316 ret = -1;
1317 goto done;
1318 }
1319
1320 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1321 ret = -1;
1310 - fprintf_ln(stderr, _("Cannot save the current status"));
1322 + if (!quiet)
1323 + fprintf_ln(stderr, _("Cannot save the current status"));
1324 goto done;
1325 }
1326
1314 - printf_ln(_("Saved working directory and index state %s"),
1315 - stash_msg_buf.buf);
1327 + if (!quiet)
1328 + printf_ln(_("Saved working directory and index state %s"),
1329 + stash_msg_buf.buf);
1330
1331 if (!patch_mode) {
1332 if (include_untracked && !ps.nr) {
@@ -1414,7 +1428,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1428 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1429
1430 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1417 - fprintf_ln(stderr, _("Cannot remove worktree changes"));
1431 + if (!quiet)
1432 + fprintf_ln(stderr, _("Cannot remove "
1433 + "worktree changes"));
1434 ret = -1;
1435 goto done;
1436 }
t/t3903-stash.sh
+23
@@ -1072,6 +1072,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' '
1072 test_path_is_file untracked
1073 '
1074
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 +
1098 test_expect_success 'untracked files are left in place when -u is not given' '
1099 >file &&
1100 git add file &&