git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
To: git@vger.kernel.org
Cc: t.gummerer@gmail.com
Subject: [PATCH v11 21/22] stash: optimize `get_untracked_files()` and `check_changes()`
Date: Fri, 23 Nov 2018 01:05:41 +0200	[thread overview]
Message-ID: <7ca8408f1e8c192324ac276ec42559c3343e9eee.1542925164.git.ungureanupaulsebastian@gmail.com> (raw)
In-Reply-To: <cover.1542925164.git.ungureanupaulsebastian@gmail.com>

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/20180818223329.GJ11326@hank.intra.tgummerer.com/

Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
 builtin/stash.c | 50 ++++++++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 21 deletions(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index c76a1936d5..5ad0f443ca 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -879,18 +879,18 @@ 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 are no 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;
 	struct rev_info rev;
 	struct object_id dummy;
-	struct strbuf out = STRBUF_INIT;
 
 	/* No initial commit. */
 	if (get_oid("HEAD", &dummy))
@@ -918,14 +918,26 @@ static int check_changes(struct pathspec ps, int include_untracked)
 	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,
-						     &out)) {
-		strbuf_release(&out);
-		return 1;
-	}
+						     untracked_files))
+		ret = 1;
 
-	strbuf_release(&out);
-	return 0;
+	return ret;
 }
 
 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,
 		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;
 		goto done;
 	}
@@ -1159,8 +1171,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
 		goto done;
 	}
 
-	if (include_untracked && get_untracked_files(ps, include_untracked,
-						     &untracked_files)) {
+	if (include_untracked) {
 		if (save_untracked_files(info, &msg, untracked_files)) {
 			if (!quiet)
 				fprintf_ln(stderr, _("Cannot save "
@@ -1235,18 +1246,14 @@ static int create_stash(int argc, const char **argv, const char *prefix)
 	strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
 
 	memset(&ps, 0, sizeof(ps));
-	ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0);
+	if (!check_changes_tracked_files(ps))
+		return 0;
 
-	if (!ret)
+	if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0)))
 		printf_ln("%s", oid_to_hex(&info.w_commit));
 
 	strbuf_release(&stash_msg_buf);
-
-	/*
-	 * ret can be 1 if there were no changes. In this case, we should
-	 * not error out.
-	 */
-	return ret < 0;
+	return ret;
 }
 
 static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
@@ -1256,6 +1263,7 @@ 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;
@@ -1290,7 +1298,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
 		goto done;
 	}
 
-	if (!check_changes(ps, include_untracked)) {
+	if (!check_changes(ps, include_untracked, &untracked_files)) {
 		if (!quiet)
 			printf_ln(_("No local changes to save"));
 		goto done;
-- 
2.19.1.878.g0482332a22


  parent reply	other threads:[~2018-11-22 23:06 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <https://public-inbox.org/git/cover.1539553398.git.ungureanupaulsebastian@gmail.com/>
2018-11-22 23:05 ` [PATCH v11 00/22] Convert "git stash" to C builtin Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 01/22] sha1-name.c: add `get_oidf()` which acts like `get_oid()` Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 02/22] strbuf.c: add `strbuf_join_argv()` Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 03/22] strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()` Paul-Sebastian Ungureanu
2018-11-25 21:43     ` Thomas Gummerer
2018-11-27 13:35       ` Johannes Schindelin
2018-11-27 22:32         ` Thomas Gummerer
2018-11-22 23:05   ` [PATCH v11 04/22] stash: improve option parsing test coverage Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 05/22] t3903: modernize style Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 06/22] stash: rename test cases to be more descriptive Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 07/22] stash: add tests for `git stash show` config Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 08/22] stash: mention options in `show` synopsis Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 09/22] stash: convert apply to builtin Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 10/22] stash: convert drop and clear " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 11/22] stash: convert branch " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 12/22] stash: convert pop " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 13/22] stash: convert list " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 14/22] stash: convert show " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 15/22] stash: convert store " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 16/22] stash: convert create " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 17/22] stash: convert push " Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 18/22] stash: make push -q quiet Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 19/22] stash: convert save to builtin Paul-Sebastian Ungureanu
2018-11-22 23:05   ` [PATCH v11 20/22] stash: convert `stash--helper.c` into `stash.c` Paul-Sebastian Ungureanu
2018-11-26  5:30     ` Junio C Hamano
2018-11-27 13:46       ` Johannes Schindelin
2018-11-27 23:40         ` Ævar Arnfjörð Bjarmason
2018-11-29 10:58           ` Johannes Schindelin
2018-11-22 23:05   ` Paul-Sebastian Ungureanu [this message]
2018-11-22 23:05   ` [PATCH v11 22/22] stash: replace all `write-tree` child processes with API calls Paul-Sebastian Ungureanu
2018-11-25 21:55   ` [PATCH v11 00/22] Convert "git stash" to C builtin Thomas Gummerer
2018-11-26  5:47     ` Junio C Hamano
2018-11-26  7:38       ` Junio C Hamano
2018-11-29 12:54         ` Johannes Schindelin
2018-11-29 14:06           ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7ca8408f1e8c192324ac276ec42559c3343e9eee.1542925164.git.ungureanupaulsebastian@gmail.com \
    --to=ungureanupaulsebastian@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=t.gummerer@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).