879
}
880
881
/*
882
- * The return value of `check_changes()` can be:
882
+ * The return value of `check_changes_tracked_files()` can be:
883
*
884
* < 0 if there was an error
885
* = 0 if there are no changes.
886
* > 0 if there are changes.
887
*/
888
-static int check_changes(struct pathspec ps, int include_untracked)
888
+static int check_changes_tracked_files(struct pathspec ps)
889
{
890
int result;
891
struct rev_info rev;
892
struct object_id dummy;
893
- struct strbuf out = STRBUF_INIT;
893
894
/* No initial commit. */
895
if (get_oid("HEAD", &dummy))
917
if (diff_result_code(&rev.diffopt, result))
918
return 1;
919
920
+ return 0;
921
+}
922
+
923
+/*
924
+ * The function will fill `untracked_files` with the names of untracked files
925
+ * It will return 1 if there were any changes and 0 if there were not.
926
+ */
927
+static int check_changes(struct pathspec ps, int include_untracked,
928
+ struct strbuf *untracked_files)
929
+{
930
+ int ret = 0;
931
+ if (check_changes_tracked_files(ps))
932
+ ret = 1;
933
+
934
if (include_untracked && get_untracked_files(ps, include_untracked,
922
- &out)) {
923
- strbuf_release(&out);
924
- return 1;
925
- }
935
+ untracked_files))
936
+ ret = 1;
937
927
- strbuf_release(&out);
928
- return 0;
938
+ return ret;
939
}
940
941
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1147
head_commit = lookup_commit(the_repository, &info->b_commit);
1148
}
1149
1140
- if (!check_changes(ps, include_untracked)) {
1150
+ if (!check_changes(ps, include_untracked, &untracked_files)) {
1151
ret = 1;
1152
goto done;
1153
}
1172
goto done;
1173
}
1174
1165
- if (include_untracked && get_untracked_files(ps, include_untracked,
1166
- &untracked_files)) {
1175
+ if (include_untracked) {
1176
if (save_untracked_files(info, &msg, untracked_files)) {
1177
if (!quiet)
1178
fprintf_ln(stderr, _("Cannot save "
1257
0);
1258
1259
memset(&ps, 0, sizeof(ps));
1260
+ if (!check_changes_tracked_files(ps))
1261
+ return 0;
1262
+
1263
strbuf_addstr(&stash_msg_buf, stash_msg);
1264
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1265
NULL, 0);
1267
printf_ln("%s", oid_to_hex(&info.w_commit));
1268
1269
strbuf_release(&stash_msg_buf);
1258
-
1259
- /*
1260
- * ret can be 1 if there were no changes. In this case, we should
1261
- * not error out.
1262
- */
1263
- return ret < 0;
1270
+ return ret;
1271
}
1272
1273
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1277
struct stash_info info;
1278
struct strbuf patch = STRBUF_INIT;
1279
struct strbuf stash_msg_buf = STRBUF_INIT;
1280
+ struct strbuf untracked_files = STRBUF_INIT;
1281
1282
if (patch_mode && keep_index == -1)
1283
keep_index = 1;
1312
goto done;
1313
}
1314
1307
- if (!check_changes(ps, include_untracked)) {
1315
+ if (!check_changes(ps, include_untracked, &untracked_files)) {
1316
if (!quiet)
1317
printf_ln(_("No local changes to save"));
1318
goto done;