1
0
mirror of https://github.com/git/git.git synced 2024-09-21 21:23:11 +02:00

stash: optimize get_untracked_files() and check_changes()

This commits introduces a optimization by avoiding calling the
same functions again. For example, `git stash push -u`
would call at some points the following functions:

 * `check_changes()` (inside `do_push_stash()`)
 * `do_create_stash()`, which calls: `check_changes()` and
`get_untracked_files()`

Note that `check_changes()` also calls `get_untracked_files()`.
So, `check_changes()` is called 2 times and `get_untracked_files()`
3 times.

The old function `check_changes()` now consists of two functions:
`get_untracked_files()` and `check_changes_tracked_files()`.

These are the call chains for `push` and `create`:

 * `push_stash()` -> `do_push_stash()` -> `do_create_stash()`

 * `create_stash()` -> `do_create_stash()`

To prevent calling the same functions over and over again,
`check_changes()` inside `do_create_stash()` is now placed
in the caller functions (`create_stash()` and `do_push_stash()`).
This way `check_changes()` and `get_untracked files()` are called
only one time.

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>
This commit is contained in:
Paul-Sebastian Ungureanu 2019-02-25 23:16:26 +00:00 committed by Junio C Hamano
parent 64fe9c26a4
commit ef0f0b4509

View File

@ -879,18 +879,17 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
} }
/* /*
* The return value of `check_changes()` can be: * The return value of `check_changes_tracked_files()` can be:
* *
* < 0 if there was an error * < 0 if there was an error
* = 0 if there are no changes. * = 0 if there are no changes.
* > 0 if there are changes. * > 0 if there are changes.
*/ */
static int check_changes(struct pathspec ps, int include_untracked) static int check_changes_tracked_files(struct pathspec ps)
{ {
int result; int result;
struct rev_info rev; struct rev_info rev;
struct object_id dummy; struct object_id dummy;
struct strbuf out = STRBUF_INIT;
/* No initial commit. */ /* No initial commit. */
if (get_oid("HEAD", &dummy)) if (get_oid("HEAD", &dummy))
@ -918,16 +917,27 @@ static int check_changes(struct pathspec ps, int include_untracked)
if (diff_result_code(&rev.diffopt, result)) if (diff_result_code(&rev.diffopt, result))
return 1; return 1;
if (include_untracked && get_untracked_files(ps, include_untracked,
&out)) {
strbuf_release(&out);
return 1;
}
strbuf_release(&out);
return 0; 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;
return ret;
}
static int save_untracked_files(struct stash_info *info, struct strbuf *msg, static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
struct strbuf files) struct strbuf files)
{ {
@ -1137,7 +1147,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
head_commit = lookup_commit(the_repository, &info->b_commit); head_commit = lookup_commit(the_repository, &info->b_commit);
} }
if (!check_changes(ps, include_untracked)) { if (!check_changes(ps, include_untracked, &untracked_files)) {
ret = 1; ret = 1;
goto done; goto done;
} }
@ -1162,8 +1172,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
goto done; goto done;
} }
if (include_untracked && get_untracked_files(ps, include_untracked, if (include_untracked) {
&untracked_files)) {
if (save_untracked_files(info, &msg, untracked_files)) { if (save_untracked_files(info, &msg, untracked_files)) {
if (!quiet) if (!quiet)
fprintf_ln(stderr, _("Cannot save " fprintf_ln(stderr, _("Cannot save "
@ -1248,6 +1257,9 @@ static int create_stash(int argc, const char **argv, const char *prefix)
0); 0);
memset(&ps, 0, sizeof(ps)); memset(&ps, 0, sizeof(ps));
if (!check_changes_tracked_files(ps))
return 0;
strbuf_addstr(&stash_msg_buf, stash_msg); strbuf_addstr(&stash_msg_buf, stash_msg);
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info, ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
NULL, 0); NULL, 0);
@ -1255,12 +1267,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
printf_ln("%s", oid_to_hex(&info.w_commit)); printf_ln("%s", oid_to_hex(&info.w_commit));
strbuf_release(&stash_msg_buf); 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, static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@ -1270,6 +1277,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
struct stash_info info; struct stash_info info;
struct strbuf patch = STRBUF_INIT; struct strbuf patch = STRBUF_INIT;
struct strbuf stash_msg_buf = STRBUF_INIT; struct strbuf stash_msg_buf = STRBUF_INIT;
struct strbuf untracked_files = STRBUF_INIT;
if (patch_mode && keep_index == -1) if (patch_mode && keep_index == -1)
keep_index = 1; keep_index = 1;
@ -1304,7 +1312,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
goto done; goto done;
} }
if (!check_changes(ps, include_untracked)) { if (!check_changes(ps, include_untracked, &untracked_files)) {
if (!quiet) if (!quiet)
printf_ln(_("No local changes to save")); printf_ln(_("No local changes to save"));
goto done; goto done;