threads / patch / 33245

patchRun git status in the background.

Subject: [PATCH] Run git status in the background.

## tl;dr

2 messages between Mar 21, 2013 and Mar 25, 2013. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Fredrik Gustafsson· Mar 21, 2013, 12:29 UTC · lore

If core.preload is set to a non-zero value, every time a git command is executed, git status will be runned in the background if the value of core.preload is lower than the number of seconds since last run.

Please see this thread: http://article.gmane.org/gmane.comp.version-control.git/218587

This solution solves many of the problems discussed there, but introduces new ones. For example, it does have a bigger impact.

With this solution beeing functional but a bit gross, it's not sure that it should be placed here at all. However, it's a good place to place it for all git-tools to be able to use it without knowing about it. (It would speed up all git wrappers and not just bash-prompt like the previous solution).

There's a few more things to address before shipping this if this is
considered a good approach. Such as:
	* Don't run if a "git status"-like git command has been runned. Or a
	  non-repo git command (lite git status or git help) is runned.
	* Better names for settings and files.
	* Better(?) invokation of git status (a forked internal call instead
	  of a system call?).
Signed-off-by: Fredrik Gustafsson <iveqy@iveqy.com>
---
 git.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
Show changes to git.c +30 −0
diff --git a/git.c b/git.c
index 39ba6b1..829aa9c 100644
--- a/git.c
+++ b/git.c
@@ -231,6 +231,14 @@ static int handle_alias(int *argcp, const char ***argv)
 	return ret;
 }
 
+static int preload_rate = 0;
+static int preload_cb(const char *k, const char *v, void *cb)
+{
+	if (strcmp(k, "core.preload") == 0)
+		preload_rate = git_config_int(k, v);
+	return 0;
+}
+
 #define RUN_SETUP		(1<<0)
 #define RUN_SETUP_GENTLY	(1<<1)
 #define USE_PAGER		(1<<2)
@@ -278,6 +286,28 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 
 	trace_argv_printf(argv, "trace: built-in: git");
 
+	/* Check if we shall run git status in the background */
+	git_config(preload_cb, NULL);
+	if (preload_rate > 0) {
+		const char * git_dir = get_git_dir();
+		char lastrun[512];
+		strcpy(lastrun, git_dir);
+		strcat(lastrun, "/lastrun");
+
+		struct stat * lr = malloc(sizeof(struct stat));
+		stat(lastrun, lr);
+
+		if ((time(NULL) - lr->st_mtime) > preload_rate) {
+			system("git status > /dev/null 2>&1 &");
+			printf("RUN\n");
+		}
+
+		// This should be done for a few other commands as well.
+		// So that we don't spawn git-status if the user just runned that command.
+		FILE * touch = fopen(lastrun, "w");
+		fclose(touch);
+	}
+
 	status = p->fn(argc, argv, prefix);
 	if (status)
 		return status;
-- 
1.8.1.5
Ramkumar Ramachandra· Mar 25, 2013, 09:12 UTC · re: Fredrik Gustafsson · lore

Re: [PATCH] Run git status in the background.

Fredrik Gustafsson wrote:
> If core.preload is set to a non-zero value, every time a git command is
> executed, git status will be runned in the background if the value of
> core.preload is lower than the number of seconds since last run.

Counting the number of seconds since the last run is gross. This kind of setting makes sense in the credential-helper, where you specify how long to cache your password. I don't think you should use a time-based trigger for this feature.

Show 11 quoted lines
> Please see this thread:
> http://article.gmane.org/gmane.comp.version-control.git/218587
>
> This solution solves many of the problems discussed there, but
> introduces new ones. For example, it does have a bigger impact.
>
> With this solution beeing functional but a bit gross, it's not sure that
> it should be placed here at all. However, it's a good place to place it
> for all git-tools to be able to use it without knowing about it. (It
> would speed up all git wrappers and not just bash-prompt like the
> previous solution).

Yes, but you're proposing including a very gross feature in core git. I'm sorry, but this is a non-starter.

Show 12 quoted lines
> There's a few more things to address before shipping this if this is
> considered a good approach. Such as:
>         * Don't run if a "git status"-like git command has been runned. Or a
>           non-repo git command (lite git status or git help) is runned.
>         * Better names for settings and files.
>         * Better(?) invokation of git status (a forked internal call instead
>           of a system call?).
>
> Signed-off-by: Fredrik Gustafsson <iveqy@iveqy.com>
> ---
>  git.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
I would argue that git.c is the wrong place to implement this feature.
 You're essentially doing fopen(), fclose(), stat(), and system():
shouldn't this be a shell script?  I earlier suggested making it
something we can hook to chpwd() in zsh, and I think this is the most
sane suggestion.  This is what z() [1] uses, and I would argue that
your feature shares many similarities with it.
[1]: https://github.com/rupa/z

← back to recent threads