Skip to content

Commit 43a4888

Browse files
ungpsdscho
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. https://public-inbox.org/git/[email protected]/ Signed-off-by: Paul-Sebastian Ungureanu <[email protected]>
1 parent 4ad0e88 commit 43a4888

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

builtin/stash.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -879,18 +879,18 @@ 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+
889+
static int check_changes_tracked_files(struct pathspec ps)
889890
{
890891
int result;
891892
struct rev_info rev;
892893
struct object_id dummy;
893-
struct strbuf out = STRBUF_INIT;
894894

895895
/* No initial commit. */
896896
if (get_oid("HEAD", &dummy))
@@ -918,14 +918,26 @@ static int check_changes(struct pathspec ps, int include_untracked)
918918
if (diff_result_code(&rev.diffopt, result))
919919
return 1;
920920

921+
return 0;
922+
}
923+
924+
/*
925+
* The function will fill `untracked_files` with the names of untracked files
926+
* It will return 1 if there were any changes and 0 if there were not.
927+
*/
928+
929+
static int check_changes(struct pathspec ps, int include_untracked,
930+
struct strbuf *untracked_files)
931+
{
932+
int ret = 0;
933+
if (check_changes_tracked_files(ps))
934+
ret = 1;
935+
921936
if (include_untracked && get_untracked_files(ps, include_untracked,
922-
&out)) {
923-
strbuf_release(&out);
924-
return 1;
925-
}
937+
untracked_files))
938+
ret = 1;
926939

927-
strbuf_release(&out);
928-
return 0;
940+
return ret;
929941
}
930942

931943
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
@@ -1134,7 +1146,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11341146
head_commit = lookup_commit(the_repository, &info->b_commit);
11351147
}
11361148

1137-
if (!check_changes(ps, include_untracked)) {
1149+
if (!check_changes(ps, include_untracked, &untracked_files)) {
11381150
ret = 1;
11391151
goto done;
11401152
}
@@ -1159,8 +1171,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11591171
goto done;
11601172
}
11611173

1162-
if (include_untracked && get_untracked_files(ps, include_untracked,
1163-
&untracked_files)) {
1174+
if (include_untracked) {
11641175
if (save_untracked_files(info, &msg, untracked_files)) {
11651176
if (!quiet)
11661177
fprintf_ln(stderr, _("Cannot save "
@@ -1241,18 +1252,14 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12411252
strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
12421253

12431254
memset(&ps, 0, sizeof(ps));
1244-
ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0);
1255+
if (!check_changes_tracked_files(ps))
1256+
return 0;
12451257

1246-
if (!ret)
1258+
if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0)))
12471259
printf_ln("%s", oid_to_hex(&info.w_commit));
12481260

12491261
strbuf_release(&stash_msg_buf);
1250-
1251-
/*
1252-
* ret can be 1 if there were no changes. In this case, we should
1253-
* not error out.
1254-
*/
1255-
return ret < 0;
1262+
return ret;
12561263
}
12571264

12581265
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@@ -1262,6 +1269,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12621269
struct stash_info info;
12631270
struct strbuf patch = STRBUF_INIT;
12641271
struct strbuf stash_msg_buf = STRBUF_INIT;
1272+
struct strbuf untracked_files = STRBUF_INIT;
12651273

12661274
if (patch_mode && keep_index == -1)
12671275
keep_index = 1;
@@ -1296,7 +1304,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
12961304
goto done;
12971305
}
12981306

1299-
if (!check_changes(ps, include_untracked)) {
1307+
if (!check_changes(ps, include_untracked, &untracked_files)) {
13001308
if (!quiet)
13011309
printf_ln(_("No local changes to save"));
13021310
goto done;

0 commit comments

Comments
 (0)