@matheusbsbs / matheus-bagliano / commits / ac5409e420
Fork 0

update-ref: -d flag and ref creation safety.

This adds -d flag to update-ref to allow safe deletion of ref. Before deleting it, the command checks if the given <oldvalue> still matches the value the caller thought the ref contained. Similarly, it also accepts 0{40} or an empty string as <oldvalue> to allow safe creation of a new ref. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 27, 2006 at 01:58 UTC ac5409e420e5fdd7c4a381f873ffcedfb83d7117
4 files changed +50 -5
Documentation/git-update-ref.txt
+8 -2
@@ -7,7 +7,7 @@ git-update-ref - update the object name stored in a ref safely
7
8 SYNOPSIS
9 --------
10 -'git-update-ref' [-m <reason>] <ref> <newvalue> [<oldvalue>]
10 +'git-update-ref' [-m <reason>] (-d <ref> <oldvalue> | <ref> <newvalue> [<oldvalue>])
11
12 DESCRIPTION
13 -----------
@@ -20,7 +20,9 @@ possibly dereferencing the symbolic refs, after verifying that
20 the current value of the <ref> matches <oldvalue>.
21 E.g. `git-update-ref refs/heads/master <newvalue> <oldvalue>`
22 updates the master branch head to <newvalue> only if its current
23 -value is <oldvalue>.
23 +value is <oldvalue>. You can specify 40 "0" or an empty string
24 +as <oldvalue> to make sure that the ref you are creating does
25 +not exist.
26
27 It also allows a "ref" file to be a symbolic pointer to another
28 ref file by starting with the four-byte header sequence of
@@ -49,6 +51,10 @@ for reading but not for writing (so we'll never write through a
51 ref symlink to some other tree, if you have copied a whole
52 archive by creating a symlink tree).
53
54 +With `-d` flag, it deletes the named <ref> after verifying it
55 +still contains <oldvalue>.
56 +
57 +
58 Logging Updates
59 ---------------
60 If config parameter "core.logAllRefUpdates" is true or the file
builtin-update-ref.c
+15 -3
@@ -3,15 +3,16 @@
3 #include "builtin.h"
4
5 static const char git_update_ref_usage[] =
6 -"git-update-ref <refname> <value> [<oldval>] [-m <reason>]";
6 +"git-update-ref [-m <reason>] (-d <refname> <value> | <refname> <value> [<oldval>])";
7
8 int cmd_update_ref(int argc, const char **argv, const char *prefix)
9 {
10 const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
11 struct ref_lock *lock;
12 unsigned char sha1[20], oldsha1[20];
13 - int i;
13 + int i, delete;
14
15 + delete = 0;
16 setup_ident();
17 git_config(git_default_config);
18
@@ -26,6 +27,10 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
27 die("Refusing to perform update with \\n in message.");
28 continue;
29 }
30 + if (!strcmp("-d", argv[i])) {
31 + delete = 1;
32 + continue;
33 + }
34 if (!refname) {
35 refname = argv[i];
36 continue;
@@ -44,8 +49,15 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
49
50 if (get_sha1(value, sha1))
51 die("%s: not a valid SHA1", value);
52 +
53 + if (delete) {
54 + if (oldval)
55 + usage(git_update_ref_usage);
56 + return delete_ref(refname, sha1);
57 + }
58 +
59 hashclr(oldsha1);
48 - if (oldval && get_sha1(oldval, oldsha1))
60 + if (oldval && *oldval && get_sha1(oldval, oldsha1))
61 die("%s: not a valid old SHA1", oldval);
62
63 lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL);
cache.h
+1
@@ -179,6 +179,7 @@ struct lock_file {
179 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
180 extern int commit_lock_file(struct lock_file *);
181 extern void rollback_lock_file(struct lock_file *);
182 +extern int delete_ref(const char *, unsigned char *sha1);
183
184 /* Environment bits from configuration mechanism */
185 extern int use_legacy_headers;
refs.c
+26
@@ -378,6 +378,32 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)
378 return read_ref(mkpath("refs/%s", ref), sha1);
379 }
380
381 +int delete_ref(const char *refname, unsigned char *sha1)
382 +{
383 + struct ref_lock *lock;
384 + int err, i, ret = 0;
385 +
386 + lock = lock_any_ref_for_update(refname, sha1);
387 + if (!lock)
388 + return 1;
389 + i = strlen(lock->lk->filename) - 5; /* .lock */
390 + lock->lk->filename[i] = 0;
391 + err = unlink(lock->lk->filename);
392 + if (err) {
393 + ret = 1;
394 + error("unlink(%s) failed: %s",
395 + lock->lk->filename, strerror(errno));
396 + }
397 + lock->lk->filename[i] = '.';
398 +
399 + err = unlink(lock->log_file);
400 + if (err && errno != ENOENT)
401 + fprintf(stderr, "warning: unlink(%s) failed: %s",
402 + lock->log_file, strerror(errno));
403 +
404 + return ret;
405 +}
406 +
407 /*
408 * Make sure "ref" is something reasonable to have under ".git/refs/";
409 * We do not like it if: