fixup! stash: optimize get_untracked_files() and check_changes()

In preparation for a newer patch series.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin
2019-03-11 16:36:14 +01:00
parent 7f224087fe
commit 2b4813df50

View File

@@ -886,18 +886,18 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
}
/*
* The return value of `check_changes_tracked_files()` can be:
* The return value of `check_changes()` can be:
*
* < 0 if there was an error
* = 0 if there are no changes.
* > 0 if there are changes.
*/
static int check_changes_tracked_files(struct pathspec ps)
static int check_changes(struct pathspec ps, int include_untracked)
{
int result;
struct rev_info rev;
struct object_id dummy;
struct strbuf out = STRBUF_INIT;
/* No initial commit. */
if (get_oid("HEAD", &dummy))
@@ -925,26 +925,14 @@ static int check_changes_tracked_files(struct pathspec ps)
if (diff_result_code(&rev.diffopt, result))
return 1;
return 0;
}
/*
* The function will fill `untracked_files` with the names of untracked files
* It will return 1 if there were any changes and 0 if there were not.
*/
static int check_changes(struct pathspec ps, int include_untracked,
struct strbuf *untracked_files)
{
int ret = 0;
if (check_changes_tracked_files(ps))
ret = 1;
if (include_untracked && get_untracked_files(ps, include_untracked,
untracked_files))
ret = 1;
&out)) {
strbuf_release(&out);
return 1;
}
return ret;
strbuf_release(&out);
return 0;
}
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
@@ -1155,7 +1143,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
head_commit = lookup_commit(the_repository, &info->b_commit);
}
if (!check_changes(ps, include_untracked, &untracked_files)) {
if (!check_changes(ps, include_untracked)) {
ret = 1;
goto done;
}
@@ -1180,7 +1168,8 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
goto done;
}
if (include_untracked) {
if (include_untracked && get_untracked_files(ps, include_untracked,
&untracked_files)) {
if (save_untracked_files(info, &msg, untracked_files)) {
if (!quiet)
fprintf_ln(stderr, _("Cannot save "
@@ -1265,15 +1254,20 @@ static int create_stash(int argc, const char **argv, const char *prefix)
0);
memset(&ps, 0, sizeof(ps));
if (!check_changes_tracked_files(ps))
return 0;
strbuf_addstr(&stash_msg_buf, stash_msg);
if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0)))
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
NULL, 0);
if (!ret)
printf_ln("%s", oid_to_hex(&info.w_commit));
strbuf_release(&stash_msg_buf);
return ret;
/*
* ret can be 1 if there were no changes. In this case, we should
* not error out.
*/
return ret < 0;
}
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@@ -1283,7 +1277,6 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
struct stash_info info;
struct strbuf patch = STRBUF_INIT;
struct strbuf stash_msg_buf = STRBUF_INIT;
struct strbuf untracked_files = STRBUF_INIT;
if (patch_mode && keep_index == -1)
keep_index = 1;
@@ -1318,7 +1311,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
goto done;
}
if (!check_changes(ps, include_untracked, &untracked_files)) {
if (!check_changes(ps, include_untracked)) {
if (!quiet)
printf_ln(_("No local changes to save"));
goto done;