git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Sverre Rabbelier" <srabbelier@gmail.com>,
	"Jeff King" <peff@peff.net>, "Nicolas Pitre" <nico@fluxnic.net>,
	"Fernando Vezzosi" <buccia@repnz.net>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v2 1/5] Add convenient function to do automatic garbage collection
Date: Wed, 16 May 2012 19:29:33 +0700	[thread overview]
Message-ID: <1337171377-26960-2-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1337171377-26960-1-git-send-email-pclouds@gmail.com>

This function also avoids forking most of the time by performing some
check in process.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/gc.c           |   27 +++++++++++++++++++++++++--
 builtin/gc.h           |    8 ++++++++
 builtin/merge.c        |    8 ++------
 builtin/receive-pack.c |   14 ++------------
 4 files changed, 37 insertions(+), 20 deletions(-)
 create mode 100644 builtin/gc.h

diff --git a/builtin/gc.c b/builtin/gc.c
index 9b4232c..ce60225 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -15,6 +15,7 @@
 #include "parse-options.h"
 #include "run-command.h"
 #include "argv-array.h"
+#include "gc.h"
 
 #define FAILED_RUN "failed to run %s"
 
@@ -28,6 +29,7 @@ static int aggressive_window = 250;
 static int gc_auto_threshold = 6700;
 static int gc_auto_pack_limit = 50;
 static const char *prune_expire = "2.weeks.ago";
+static int auto_gc = 1;
 
 static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
 static struct argv_array reflog = ARGV_ARRAY_INIT;
@@ -64,6 +66,10 @@ static int gc_config(const char *var, const char *value, void *cb)
 		}
 		return git_config_string(&prune_expire, var, value);
 	}
+	if (cb && !strcmp(var, cb)) {
+		auto_gc = git_config_bool(var, value);
+		return 0;
+	}
 	return git_default_config(var, value, cb);
 }
 
@@ -162,11 +168,24 @@ static int need_to_gc(void)
 	else if (!too_many_loose_objects())
 		return 0;
 
-	if (run_hook(NULL, "pre-auto-gc", NULL))
-		return 0;
 	return 1;
 }
 
+int gc(const char *cmd, int flags)
+{
+	const char *av[] = { "gc", "--auto", NULL, NULL };
+	int ac = 2;
+
+	git_config(gc_config, (void*)cmd);
+
+	if (!auto_gc || !need_to_gc())
+		return 0;
+
+	if (flags & GC_QUIET)
+		av[ac++] = "--quiet";
+	return run_command_v_opt(av, RUN_GIT_CMD);
+}
+
 int cmd_gc(int argc, const char **argv, const char *prefix)
 {
 	int aggressive = 0;
@@ -217,6 +236,10 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		 */
 		if (!need_to_gc())
 			return 0;
+
+		if (run_hook(NULL, "pre-auto-gc", NULL))
+			return 0;
+
 		if (quiet)
 			fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
 		else
diff --git a/builtin/gc.h b/builtin/gc.h
new file mode 100644
index 0000000..3482e92
--- /dev/null
+++ b/builtin/gc.h
@@ -0,0 +1,8 @@
+#ifndef GC_H
+#define GC_H
+
+#define GC_QUIET  1
+
+extern int gc(const char *cmd, int flags);
+
+#endif
diff --git a/builtin/merge.c b/builtin/merge.c
index 470fc57..940259d 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -28,6 +28,7 @@
 #include "remote.h"
 #include "fmt-merge-msg.h"
 #include "gpg-interface.h"
+#include "gc.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -385,15 +386,10 @@ static void finish(struct commit *head_commit,
 		if (verbosity >= 0 && !merge_msg.len)
 			printf(_("No merge message -- not updating HEAD\n"));
 		else {
-			const char *argv_gc_auto[] = { "gc", "--auto", NULL };
 			update_ref(reflog_message.buf, "HEAD",
 				new_head, head, 0,
 				DIE_ON_ERR);
-			/*
-			 * We ignore errors in 'gc --auto', since the
-			 * user should see them.
-			 */
-			run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
+			gc(NULL, 0);
 		}
 	}
 	if (new_head && show_diffstat) {
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 0afb8b2..0c1fe25 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -12,6 +12,7 @@
 #include "string-list.h"
 #include "sha1-array.h"
 #include "connected.h"
+#include "gc.h"
 
 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
 
@@ -36,7 +37,6 @@ static int use_sideband;
 static int quiet;
 static int prefer_ofs_delta = 1;
 static int auto_update_server_info;
-static int auto_gc = 1;
 static const char *head_name;
 static void *head_name_to_free;
 static int sent_capabilities;
@@ -108,11 +108,6 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	if (strcmp(var, "receive.autogc") == 0) {
-		auto_gc = git_config_bool(var, value);
-		return 0;
-	}
-
 	return git_default_config(var, value, cb);
 }
 
@@ -973,12 +968,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
 			report(commands, unpack_status);
 		run_receive_hook(commands, post_receive_hook, 1);
 		run_update_post_hook(commands);
-		if (auto_gc) {
-			const char *argv_gc_auto[] = {
-				"gc", "--auto", "--quiet", NULL,
-			};
-			run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
-		}
+		gc("receive.autogc", GC_QUIET);
 		if (auto_update_server_info)
 			update_server_info(0);
 	}
-- 
1.7.8.36.g69ee2

  reply	other threads:[~2012-05-16 12:33 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-12  8:08 [PATCH] Update "gc" behavior in commit, merge, am, rebase and index-pack Nguyễn Thái Ngọc Duy
2012-05-12  8:18 ` Nguyen Thai Ngoc Duy
2012-05-12 17:36 ` Nicolas Pitre
2012-05-14  9:09   ` Nguyen Thai Ngoc Duy
2012-05-14 15:18     ` Sverre Rabbelier
2012-05-15 11:25       ` Nguyen Thai Ngoc Duy
2012-05-14 20:11   ` Jeff King
2012-05-14 20:50 ` Jeff King
2012-05-14 23:35   ` Junio C Hamano
2012-05-16 12:29 ` [PATCH v2 0/5] "git gc --auto" update Nguyễn Thái Ngọc Duy
2012-05-16 12:29   ` Nguyễn Thái Ngọc Duy [this message]
2012-05-18 22:37     ` [PATCH v2 1/5] Add convenient function to do automatic garbage collection Junio C Hamano
2012-05-19  4:56       ` Nguyen Thai Ngoc Duy
2012-05-16 12:29   ` [PATCH v2 2/5] index-pack: perform automatic gc, share receive.gc config with receive-pack Nguyễn Thái Ngọc Duy
2012-05-16 12:29   ` [PATCH v2 3/5] commit: reinstate "gc --auto" Nguyễn Thái Ngọc Duy
2012-05-16 12:29   ` [PATCH v2 4/5] gc: add --dry-run Nguyễn Thái Ngọc Duy
2012-05-16 12:29   ` [PATCH v2 5/5] Update "gc" behavior in commit, merge, am and rebase Nguyễn Thái Ngọc Duy

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=1337171377-26960-2-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=buccia@repnz.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=nico@fluxnic.net \
    --cc=peff@peff.net \
    --cc=srabbelier@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).