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

[PATCHv11 20/20] builtin-gc: Teach the new --notes option to garbage-collect notes

From
Johan Herland <johan@herland.net>
Date
Jan 17, 2010, 21:04 UTC
Message-ID
<1263762277-31419-21-git-send-email-johan@herland.net>
In-Reply-To
<1263762277-31419-1-git-send-email-johan@herland.net>

The new --notes option triggers a call to gc_notes(), which removes note objects that reference non-existing objects.

Signed-off-by: Johan Herland <johan@herland.net>
---
 Documentation/git-gc.txt |    5 ++-
 builtin-gc.c             |   13 ++++++
 t/t3306-notes-gc.sh      |  106 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 1 deletions(-)
 create mode 100755 t/t3306-notes-gc.sh
diff --git a/Documentation/git-gc.txt b/Documentation/git-gc.txt
index 4cd9cdf..b3d1107 100644
--- a/Documentation/git-gc.txt
+++ b/Documentation/git-gc.txt
@@ -8,7 +8,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 
 SYNOPSIS
 --------
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
+'git gc' [--aggressive] [--auto] [--quiet] [--notes] [--prune=<date> | --no-prune]
 
 DESCRIPTION
 -----------
@@ -70,6 +70,9 @@ automatic consolidation of packs.
 --quiet::
 	Suppress all progress reports.
 
+--notes::
+	Prune note objects where the referenced object no longer exists.
+
 Configuration
 -------------
 
diff --git a/builtin-gc.c b/builtin-gc.c
index 093517e..b051bc8 100644
--- a/builtin-gc.c
+++ b/builtin-gc.c
@@ -12,6 +12,7 @@
 
 #include "builtin.h"
 #include "cache.h"
+#include "notes.h"
 #include "parse-options.h"
 #include "run-command.h"
 
@@ -177,7 +178,9 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	int aggressive = 0;
 	int auto_gc = 0;
 	int quiet = 0;
+	int notes = 0;
 	char buf[80];
+	struct notes_tree *t;
 
 	struct option builtin_gc_options[] = {
 		{ OPTION_STRING, 0, "prune", &prune_expire, "date",
@@ -186,6 +189,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN(0, "aggressive", &aggressive, "be more thorough (increased runtime)"),
 		OPT_BOOLEAN(0, "auto", &auto_gc, "enable auto-gc mode"),
 		OPT_BOOLEAN('q', "quiet", &quiet, "suppress progress reports"),
+		OPT_BOOLEAN(0, "notes", &notes, "delete orphaned note objects"),
 		OPT_END()
 	};
 
@@ -235,6 +239,15 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
 		return error(FAILED_RUN, argv_reflog[0]);
 
+	if (notes) {
+		init_notes(NULL, NULL, NULL, 0);
+		t = &default_notes_tree;
+		gc_notes(t);
+		if (commit_notes(t, "Notes tree cleaned up by 'git gc --notes'"))
+			die("Failed to garbage-collect notes tree @ '%s'", t->ref);
+		free_notes(t);
+	}
+
 	if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
 		return error(FAILED_RUN, argv_repack[0]);
 
diff --git a/t/t3306-notes-gc.sh b/t/t3306-notes-gc.sh
new file mode 100755
index 0000000..1d8233d
--- /dev/null
+++ b/t/t3306-notes-gc.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+test_description='Test git gc --notes'
+
+. ./test-lib.sh
+
+test_expect_success 'setup: create a few commits with notes' '
+
+	: > file1 &&
+	git add file1 &&
+	test_tick &&
+	git commit -m 1st &&
+	git notes edit -m "Note #1" &&
+	: > file2 &&
+	git add file2 &&
+	test_tick &&
+	git commit -m 2nd &&
+	git notes edit -m "Note #2" &&
+	: > file3 &&
+	git add file3 &&
+	test_tick &&
+	git commit -m 3rd &&
+	git notes edit -m "Note #3"
+'
+
+cat > expect <<END_OF_LOG
+commit 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes:
+    Note #3
+
+commit 08341ad9e94faa089d60fd3f523affb25c6da189
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes:
+    Note #2
+
+commit ab5f302035f2e7aaf04265f08b42034c23256e1f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes:
+    Note #1
+END_OF_LOG
+
+test_expect_success 'verify commits and notes' '
+
+	git log > actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'remove some commits' '
+
+	git reset --hard HEAD~2 &&
+	git reflog expire --expire=now HEAD &&
+	git gc --prune=now
+'
+
+test_expect_success 'verify that commits are gone' '
+
+	! git cat-file -p 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&
+	! git cat-file -p 08341ad9e94faa089d60fd3f523affb25c6da189 &&
+	git cat-file -p ab5f302035f2e7aaf04265f08b42034c23256e1f
+'
+
+test_expect_success 'verify that notes are still present' '
+
+	git notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&
+	git notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&
+	git notes show ab5f302035f2e7aaf04265f08b42034c23256e1f
+'
+
+test_expect_success 'garbage-collect (without --notes)' '
+
+	git gc --prune=now --aggressive
+'
+
+test_expect_success 'verify that notes are still present' '
+
+	git notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&
+	git notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&
+	git notes show ab5f302035f2e7aaf04265f08b42034c23256e1f
+'
+
+test_expect_success 'garbage-collect notes' '
+
+	git gc --notes --prune=now --aggressive
+'
+
+test_expect_success 'verify that notes are gone' '
+
+	! git notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&
+	! git notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&
+	git notes show ab5f302035f2e7aaf04265f08b42034c23256e1f
+'
+
+test_done
-- 
1.6.6.rc1.321.g0496e
Previous: Johan HerlandNext: Stephen Boyd
Message 23 of 26 in “[PATCHv11 00/20] git notes”
  1. Johan HerlandJan 17, 2010
  2. 01/20 Minor non-functional fixes to notes.cJohan Herland, Jan 17, 2010
  3. 02/20 Notes API: get_commit_notes() -> format_note() + remove the commit restrictionJohan Herland, Jan 17, 2010
  4. 03/20 Add tests for checking correct handling of $GIT_NOTES_REF and core.notesRefJohan Herland, Jan 17, 2010
  5. 04/20 Notes API: init_notes(): Initialize the notes tree from the given notes refJohan Herland, Jan 17, 2010
  6. 05/20 Notes API: add_note(): Add note objects to the internal notes tree structureJohan Herland, Jan 17, 2010
  7. 06/20 Notes API: remove_note(): Remove note objects from the notes tree structureJohan Herland, Jan 17, 2010
  8. 07/20 Notes API: get_note(): Return the note annotating the given objectJohan Herland, Jan 17, 2010
  9. 08/20 Notes API: for_each_note(): Traverse the entire notes tree with a callbackJohan Herland, Jan 17, 2010
  10. 09/20 Notes API: write_notes_tree(): Store the notes tree in the databaseJohan Herland, Jan 17, 2010
  11. 10/20 Notes API: Allow multiple concurrent notes trees with new struct notes_treeJohan Herland, Jan 17, 2010
  12. 11/20 Refactor notes concatenation into a flexible interface for combining notesJohan Herland, Jan 17, 2010
  13. 12/20 Builtin-ify git-notesJohan Herland, Jan 17, 2010
  14. Stephen BoydJan 21, 2010
  15. Johan HerlandJan 26, 2010
  16. 13/20 t3301: Verify successful annotation of non-commitsJohan Herland, Jan 17, 2010
  17. 14/20 t3305: Verify that adding many notes with git-notes triggers increased fanoutJohan Herland, Jan 17, 2010
  18. 15/20 Teach notes code to properly preserve non-notes in the notes treeJohan Herland, Jan 17, 2010
  19. 16/20 Teach builtin-notes to remove empty notesJohan Herland, Jan 17, 2010
  20. 17/20 builtin-notes: Teach -d option for deleting existing notesJohan Herland, Jan 17, 2010
  21. 18/20 t3305: Verify that removing notes triggers automatic fanout consolidationJohan Herland, Jan 17, 2010
  22. 19/20 Notes API: gc_notes(): Prune notes that belong to non-existing objectsJohan Herland, Jan 17, 2010
  23. 20/20 builtin-gc: Teach the new --notes option to garbage-collect notesJohan Herland, Jan 17, 2010
  24. Stephen BoydJan 21, 2010
  25. Junio C HamanoJan 21, 2010
  26. Johan HerlandJan 27, 2010

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.