Skip to content

Commit ef0f0b4

Browse files
ungpsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64fe9c2 commit ef0f0b4

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

builtin/stash--helper.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -879,18 +879,17 @@ static int get_untracked_files(struct pathspec ps, int include_untracked,
879879
}
880880

881881
/*
882-
* The return value of `check_changes()` can be:
882+
* The return value of `check_changes_tracked_files()` can be:
883883
*
884884
* < 0 if there was an error
885885
* = 0 if there are no changes.
886886
* > 0 if there are changes.
887887
*/
888-
static int check_changes(struct pathspec ps, int include_untracked)
888+
static int check_changes_tracked_files(struct pathspec ps)
889889
{
890890
int result;
891891
struct rev_info rev;
892892
struct object_id dummy;
893-
struct strbuf out = STRBUF_INIT;
894893

895894
/* No initial commit. */
896895
if (get_oid("HEAD", &dummy))
@@ -918,14 +917,25 @@ static int check_changes(struct pathspec ps, int include_untracked)
918917
if (diff_result_code(&rev.diffopt, result))
919918
return 1;
920919

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+
921934
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;
926937

927-
strbuf_release(&out);
928-
return 0;
938+
return ret;
929939
}
930940

931941
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
@@ -1137,7 +1147,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11371147
head_commit = lookup_commit(the_repository, &info->b_commit);
11381148
}
11391149

1140-
if (!check_changes(ps, include_untracked)) {
1150+
if (!check_changes(ps, include_untracked, &untracked_files)) {
11411151
ret = 1;
11421152
goto done;
11431153
}
@@ -1162,8 +1172,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11621172
goto done;
11631173
}
11641174

1165-
if (include_untracked && get_untracked_files(ps, include_untracked,
1166-
&untracked_files)) {
1175+
if (include_untracked) {
11671176
if (save_untracked_files(info, &msg, untracked_files)) {
11681177
if (!quiet)
11691178
fprintf_ln(stderr, _("Cannot save "
@@ -1248,19 +1257,17 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12481257
0);
12491258

12501259
memset(&ps, 0, sizeof(ps));
1260+
if (!check_changes_tracked_files(ps))
1261+
return 0;
1262+
12511263
strbuf_addstr(&stash_msg_buf, stash_msg);
12521264
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
12531265
NULL, 0);
12541266
if (!ret)
12551267
printf_ln("%s", oid_to_hex(&info.w_commit));
12561268

12571269
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;
12641271
}
12651272

12661273
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,
12701277
struct stash_info info;
12711278
struct strbuf patch = STRBUF_INIT;
12721279
struct strbuf stash_msg_buf = STRBUF_INIT;
1280+
struct strbuf untracked_files = STRBUF_INIT;
12731281

12741282
if (patch_mode && keep_index == -1)
12751283
keep_index = 1;
@@ -1304,7 +1312,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
13041312
goto done;
13051313
}
13061314

1307-
if (!check_changes(ps, include_untracked)) {
1315+
if (!check_changes(ps, include_untracked, &untracked_files)) {
13081316
if (!quiet)
13091317
printf_ln(_("No local changes to save"));
13101318
goto done;

0 commit comments

Comments
 (0)