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

[PATCH v7 09/11] fast-import: add support to delete refs

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Nov 12, 2013, 20:57 UTC
Message-ID
<1384289830-5471-15-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1384289830-5471-1-git-send-email-felipe.contreras@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/git-fast-import.txt |  3 +++
 fast-import.c                     | 13 ++++++++++---
 t/t9300-fast-import.sh            | 18 ++++++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-fast-import.txt b/Documentation/git-fast-import.txt
index 73f9806..2ffae42 100644
--- a/Documentation/git-fast-import.txt
+++ b/Documentation/git-fast-import.txt
@@ -483,6 +483,9 @@ Marks must be declared (via `mark`) before they can be used.
 * Any valid Git SHA-1 expression that resolves to a commit.  See
   ``SPECIFYING REVISIONS'' in linkgit:gitrevisions[7] for details.
 
+* The special null SHA-1 (40 zeros) specifies that the branch is to be
+  removed.
+
 The special case of restarting an incremental import from the
 current branch value should be written as:
 ----
diff --git a/fast-import.c b/fast-import.c
index f4d9969..fdce0b7 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -248,6 +248,7 @@ struct branch {
 	uintmax_t last_commit;
 	uintmax_t num_notes;
 	unsigned active : 1;
+	unsigned delete : 1;
 	unsigned pack_id : PACK_ID_BITS;
 	unsigned char sha1[20];
 };
@@ -1690,10 +1691,13 @@ static int update_branch(struct branch *b)
 	struct ref_lock *lock;
 	unsigned char old_sha1[20];
 
-	if (is_null_sha1(b->sha1))
-		return 0;
 	if (read_ref(b->name, old_sha1))
 		hashclr(old_sha1);
+	if (is_null_sha1(b->sha1)) {
+		if (b->delete)
+			delete_ref(b->name, old_sha1, 0);
+		return 0;
+	}
 	lock = lock_any_ref_for_update(b->name, old_sha1, 0, NULL);
 	if (!lock)
 		return error("Unable to lock %s", b->name);
@@ -2620,8 +2624,11 @@ static int parse_from(struct branch *b)
 			free(buf);
 		} else
 			parse_from_existing(b);
-	} else if (!get_sha1(from, b->sha1))
+	} else if (!get_sha1(from, b->sha1)) {
 		parse_from_existing(b);
+		if (is_null_sha1(b->sha1))
+			b->delete = 1;
+	}
 	else
 		die("Invalid ref name or SHA1 expression: %s", from);
 
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 27263df..5fc9ef2 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -2999,4 +2999,22 @@ test_expect_success 'T: ls root tree' '
 	test_cmp expect actual
 '
 
+test_expect_success 'T: delete branch' '
+	git branch to-delete &&
+	git fast-import <<-EOF &&
+	reset refs/heads/to-delete
+	from 0000000000000000000000000000000000000000
+	EOF
+	test_must_fail git rev-parse --verify refs/heads/to-delete
+'
+
+test_expect_success 'T: empty reset doesnt delete branch' '
+	git branch not-to-delete &&
+	git fast-import <<-EOF &&
+	reset refs/heads/not-to-delete
+	EOF
+	git show-ref &&
+	git rev-parse --verify refs/heads/not-to-delete
+'
+
 test_done
-- 
1.8.4.2+fc1
Previous: Felipe ContrerasNext: Felipe Contreras
Message 15 of 20 in “transport-helper: updates”
  1. 00/11 transport-helper: updatesFelipe Contreras, Nov 12, 2013
  2. 01/11 transport-helper: fix extra linesFelipe Contreras, Nov 12, 2013
  3. 02/11 transport-helper: don't update refs in dry-runFelipe Contreras, Nov 12, 2013
  4. 03/11 transport-helper: add 'force' to 'export' helpersFelipe Contreras, Nov 12, 2013
  5. 04/11 transport-helper: check for 'forced update' messageFelipe Contreras, Nov 12, 2013
  6. 05/10 fast-export: improve argument parsingFelipe Contreras, Nov 12, 2013
  7. 05/11 test-hg.sh: tests are now expected to passFelipe Contreras, Nov 12, 2013
  8. 06/10 fast-export: add new --refspec optionFelipe Contreras, Nov 12, 2013
  9. 06/11 fast-export: improve argument parsingFelipe Contreras, Nov 12, 2013
  10. 07/11 fast-export: add new --refspec optionFelipe Contreras, Nov 12, 2013
  11. 07/10 transport-helper: add support for old:new refspecFelipe Contreras, Nov 12, 2013
  12. 08/10 fast-import: add support to delete refsFelipe Contreras, Nov 12, 2013
  13. 08/11 transport-helper: add support for old:new refspecFelipe Contreras, Nov 12, 2013
  14. 09/10 fast-export: add support to delete refsFelipe Contreras, Nov 12, 2013
  15. 09/11 fast-import: add support to delete refsFelipe Contreras, Nov 12, 2013
  16. 10/11 fast-export: add support to delete refsFelipe Contreras, Nov 12, 2013
  17. 10/10 transport-helper: add support to delete branchesFelipe Contreras, Nov 12, 2013
  18. 11/11 transport-helper: add support to delete branchesFelipe Contreras, Nov 12, 2013
  19. Junio C HamanoNov 12, 2013
  20. remote-bzr: support the new 'force' optionRichard Hansen, Nov 18, 2013

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.