fast-import: add support to delete refs
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Felipe Contreras committed
Apr 20, 2014 at 13:59 UTC
4ee1b225b99f155cc9372397b5e120c2c0eee2bf
3 files changed
+31
-3
Documentation/git-fast-import.txt
+3
@@ -483,6 +483,9 @@ Marks must be declared (via `mark`) before they can be used.
483
* Any valid Git SHA-1 expression that resolves to a commit. See
484
``SPECIFYING REVISIONS'' in linkgit:gitrevisions[7] for details.
485
486
+* The special null SHA-1 (40 zeros) specifies that the branch is to be
487
+ removed.
488
+
489
The special case of restarting an incremental import from the
490
current branch value should be written as:
491
----
fast-import.c
+10
-3
@@ -248,6 +248,7 @@ struct branch {
248
uintmax_t last_commit;
249
uintmax_t num_notes;
250
unsigned active : 1;
251
+ unsigned delete : 1;
252
unsigned pack_id : PACK_ID_BITS;
253
unsigned char sha1[20];
254
};
@@ -1681,10 +1682,13 @@ static int update_branch(struct branch *b)
1682
struct ref_lock *lock;
1683
unsigned char old_sha1[20];
1684
1684
- if (is_null_sha1(b->sha1))
1685
- return 0;
1685
if (read_ref(b->name, old_sha1))
1686
hashclr(old_sha1);
1687
+ if (is_null_sha1(b->sha1)) {
1688
+ if (b->delete)
1689
+ delete_ref(b->name, old_sha1, 0);
1690
+ return 0;
1691
+ }
1692
lock = lock_any_ref_for_update(b->name, old_sha1, 0, NULL);
1693
if (!lock)
1694
return error("Unable to lock %s", b->name);
@@ -2611,8 +2615,11 @@ static int parse_from(struct branch *b)
2615
free(buf);
2616
} else
2617
parse_from_existing(b);
2614
- } else if (!get_sha1(from, b->sha1))
2618
+ } else if (!get_sha1(from, b->sha1)) {
2619
parse_from_existing(b);
2620
+ if (is_null_sha1(b->sha1))
2621
+ b->delete = 1;
2622
+ }
2623
else
2624
die("Invalid ref name or SHA1 expression: %s", from);
2625
t/t9300-fast-import.sh
+18
@@ -2999,4 +2999,22 @@ test_expect_success 'T: ls root tree' '
2999
test_cmp expect actual
3000
'
3001
3002
+test_expect_success 'T: delete branch' '
3003
+ git branch to-delete &&
3004
+ git fast-import <<-EOF &&
3005
+ reset refs/heads/to-delete
3006
+ from 0000000000000000000000000000000000000000
3007
+ EOF
3008
+ test_must_fail git rev-parse --verify refs/heads/to-delete
3009
+'
3010
+
3011
+test_expect_success 'T: empty reset doesnt delete branch' '
3012
+ git branch not-to-delete &&
3013
+ git fast-import <<-EOF &&
3014
+ reset refs/heads/not-to-delete
3015
+ EOF
3016
+ git show-ref &&
3017
+ git rev-parse --verify refs/heads/not-to-delete
3018
+'
3019
+
3020
test_done