git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/5] gc: add --dry-run

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 16, 2012, 12:29 UTC
Message-ID
<1337171377-26960-5-git-send-email-pclouds@gmail.com>
In-Reply-To
<1337171377-26960-1-git-send-email-pclouds@gmail.com>

"git gc --auto --dry-run" will print instead of doing housekeeping. This option is to be called by other commands rather than by user and will not print more than one warning every hour. gc() function also behaves this way.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-gc.txt |    7 ++++++-
 builtin/gc.c             |   19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletions(-)
diff --git a/Documentation/git-gc.txt b/Documentation/git-gc.txt
index b370b02..f69f5e1 100644
--- a/Documentation/git-gc.txt
+++ b/Documentation/git-gc.txt
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto [--dry-run]] [--quiet] [--prune=<date> | --no-prune]
 
 DESCRIPTION
 -----------
@@ -60,6 +60,11 @@ are consolidated into a single pack by using the `-A` option of
 'git repack'. Setting `gc.autopacklimit` to 0 disables
 automatic consolidation of packs.
 
+--dry-run::
+	Only show warning if housekeeping is required. The warning
+	is only shown at most once every hour even if
+	"gc --auto --dry-run" is run continuously.
+
 --prune=<date>::
 	Prune loose objects older than date (default is 2 weeks ago,
 	overridable by the config variable `gc.pruneExpire`).  This
diff --git a/builtin/gc.c b/builtin/gc.c
index ce60225..f82b9ef 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -150,6 +150,8 @@ static void add_repack_all_option(void)
 
 static int need_to_gc(void)
 {
+	struct stat st;
+
 	/*
 	 * Setting gc.auto to 0 or negative can disable the
 	 * automatic gc.
@@ -168,6 +170,13 @@ static int need_to_gc(void)
 	else if (!too_many_loose_objects())
 		return 0;
 
+	if (stat(git_path("gc_needed"), &st))
+		open(git_path("gc_needed"), O_CREAT | O_RDWR, 0644);
+	else if (time(NULL) - st.st_mtime < 3600)
+		return 0;
+	else
+		utime(git_path("gc_needed"), NULL);
+
 	return 1;
 }
 
@@ -190,6 +199,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 {
 	int aggressive = 0;
 	int auto_gc = 0;
+	int check_gc = 0;
 	int quiet = 0;
 
 	struct option builtin_gc_options[] = {
@@ -199,12 +209,16 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
 		OPT_BOOLEAN(0, "aggressive", &aggressive, "be more thorough (increased runtime)"),
 		OPT_BOOLEAN(0, "auto", &auto_gc, "enable auto-gc mode"),
+		OPT_BOOLEAN(0, "dry-run", &check_gc, "warn if auto gc is needed"),
 		OPT_END()
 	};
 
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage_with_options(builtin_gc_usage, builtin_gc_options);
 
+	if (!auto_gc && check_gc)
+		die(_("--dry-run is useless without --auto"));
+
 	argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
 	argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
 	argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
@@ -240,6 +254,11 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		if (run_hook(NULL, "pre-auto-gc", NULL))
 			return 0;
 
+		if (check_gc) {
+			warning(_("This repository needs maintenance. "
+				  "Please run \"git gc\" as soon as possible."));
+			return 0;
+		}
 		if (quiet)
 			fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
 		else
-- 
1.7.8.36.g69ee2
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 16 of 17 in “Update "gc" behavior in commit, merge, am, rebase and index-pack”
  1. Update "gc" behavior in commit, merge, am, rebase and index-packNguyễn Thái Ngọc Duy, May 12, 2012
  2. Nguyen Thai Ngoc DuyMay 12, 2012
  3. Nicolas PitreMay 12, 2012
  4. Nguyen Thai Ngoc DuyMay 14, 2012
  5. Sverre RabbelierMay 14, 2012
  6. Nguyen Thai Ngoc DuyMay 15, 2012
  7. Jeff KingMay 14, 2012
  8. Jeff KingMay 14, 2012
  9. Junio C HamanoMay 14, 2012
  10. 0/5 "git gc --auto" updateNguyễn Thái Ngọc Duy, May 16, 2012
  11. 1/5 Add convenient function to do automatic garbage collectionNguyễn Thái Ngọc Duy, May 16, 2012
  12. Junio C HamanoMay 18, 2012
  13. Nguyen Thai Ngoc DuyMay 19, 2012
  14. 2/5 index-pack: perform automatic gc, share receive.gc config with receive-packNguyễn Thái Ngọc Duy, May 16, 2012
  15. 3/5 commit: reinstate "gc --auto"Nguyễn Thái Ngọc Duy, May 16, 2012
  16. 4/5 gc: add --dry-runNguyễn Thái Ngọc Duy, May 16, 2012
  17. 5/5 Update "gc" behavior in commit, merge, am and rebaseNguyễn Thái Ngọc Duy, May 16, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.