git-update-ref: add --no-deref option for overwriting/detaching ref
git-checkout is also adapted to make use of this new option instead of the handcrafted command sequence. Signed-off-by: Sven Verdoolaege <skimo@kotnet.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Sven Verdoolaege committed
May 9, 2007 at 12:33 UTC
68db31cc289c686c4b4454dfbb121aff59a6c602
10 files changed
+39
-30
Documentation/git-update-ref.txt
+4
-1
@@ -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>] (-d <ref> <oldvalue> | <ref> <newvalue> [<oldvalue>])
10
+'git-update-ref' [-m <reason>] (-d <ref> <oldvalue> | [--no-deref] <ref> <newvalue> [<oldvalue>])
11
12
DESCRIPTION
13
-----------
@@ -36,6 +36,9 @@ them and update them as a regular file (i.e. it will allow the
36
filesystem to follow them, but will overwrite such a symlink to
37
somewhere else with a regular filename).
38
39
+If --no-deref is given, <ref> itself is overwritten, rather than
40
+the result of following the symbolic pointers.
41
+
42
In general, using
43
44
git-update-ref HEAD "$head"
builtin-branch.c
+1
-1
@@ -462,7 +462,7 @@ static void create_branch(const char *name, const char *start_name,
462
die("Not a valid branch point: '%s'.", start_name);
463
hashcpy(sha1, commit->object.sha1);
464
465
- lock = lock_any_ref_for_update(ref, NULL);
465
+ lock = lock_any_ref_for_update(ref, NULL, 0);
466
if (!lock)
467
die("Failed to lock ref for update: %s.", strerror(errno));
468
builtin-fetch--tool.c
+1
-1
@@ -42,7 +42,7 @@ static int update_ref(const char *action,
42
if (!rla)
43
rla = "(reflog update)";
44
snprintf(msg, sizeof(msg), "%s: %s", rla, action);
45
- lock = lock_any_ref_for_update(refname, oldval);
45
+ lock = lock_any_ref_for_update(refname, oldval, 0);
46
if (!lock)
47
return 1;
48
if (write_ref_sha1(lock, sha1, msg) < 0)
builtin-reflog.c
+1
-1
@@ -249,7 +249,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
249
/* we take the lock for the ref itself to prevent it from
250
* getting updated.
251
*/
252
- lock = lock_any_ref_for_update(ref, sha1);
252
+ lock = lock_any_ref_for_update(ref, sha1, 0);
253
if (!lock)
254
return error("cannot lock ref '%s'", ref);
255
log_file = xstrdup(git_path("logs/%s", ref));
builtin-update-ref.c
+8
-3
@@ -3,16 +3,17 @@
3
#include "builtin.h"
4
5
static const char git_update_ref_usage[] =
6
-"git-update-ref [-m <reason>] (-d <refname> <value> | <refname> <value> [<oldval>])";
6
+"git-update-ref [-m <reason>] (-d <refname> <value> | [--no-deref] <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, delete;
13
+ int i, delete, ref_flags;
14
15
delete = 0;
16
+ ref_flags = 0;
17
git_config(git_default_config);
18
19
for (i = 1; i < argc; i++) {
@@ -30,6 +31,10 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
31
delete = 1;
32
continue;
33
}
34
+ if (!strcmp("--no-deref", argv[i])) {
35
+ ref_flags |= REF_NODEREF;
36
+ continue;
37
+ }
38
if (!refname) {
39
refname = argv[i];
40
continue;
@@ -59,7 +64,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
64
if (oldval && *oldval && get_sha1(oldval, oldsha1))
65
die("%s: not a valid old SHA1", oldval);
66
62
- lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL);
67
+ lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags);
68
if (!lock)
69
die("%s: cannot lock the ref", refname);
70
if (write_ref_sha1(lock, sha1, msg) < 0)
fast-import.c
+1
-1
@@ -1271,7 +1271,7 @@ static int update_branch(struct branch *b)
1271
1272
if (read_ref(b->name, old_sha1))
1273
hashclr(old_sha1);
1274
- lock = lock_any_ref_for_update(b->name, old_sha1);
1274
+ lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1275
if (!lock)
1276
return error("Unable to lock %s", b->name);
1277
if (!force_update && !is_null_sha1(old_sha1)) {
git-checkout.sh
+1
-9
@@ -270,15 +270,7 @@ if [ "$?" -eq 0 ]; then
270
fi
271
elif test -n "$detached"
272
then
273
- # NEEDSWORK: we would want a command to detach the HEAD
274
- # atomically, instead of this handcrafted command sequence.
275
- # Perhaps:
276
- # git update-ref --detach HEAD $new
277
- # or something like that...
278
- #
279
- git-rev-parse HEAD >"$GIT_DIR/HEAD.new" &&
280
- mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" &&
281
- git-update-ref -m "checkout: moving to $arg" HEAD "$detached" ||
273
+ git-update-ref --no-deref -m "checkout: moving to $arg" HEAD "$detached" ||
274
die "Cannot detach HEAD"
275
if test -n "$detach_warn"
276
then
receive-pack.c
+1
-1
@@ -209,7 +209,7 @@ static const char *update(struct command *cmd)
209
return NULL; /* good */
210
}
211
else {
212
- lock = lock_any_ref_for_update(name, old_sha1);
212
+ lock = lock_any_ref_for_update(name, old_sha1, 0);
213
if (!lock) {
214
error("failed to lock %s", name);
215
return "failed to lock";
refs.c
+19
-11
@@ -736,19 +736,20 @@ static int is_refname_available(const char *ref, const char *oldref,
736
return 1;
737
}
738
739
-static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
739
+static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int flags, int *type_p)
740
{
741
char *ref_file;
742
const char *orig_ref = ref;
743
struct ref_lock *lock;
744
struct stat st;
745
int last_errno = 0;
746
+ int type;
747
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
748
749
lock = xcalloc(1, sizeof(struct ref_lock));
750
lock->lock_fd = -1;
751
751
- ref = resolve_ref(ref, lock->old_sha1, mustexist, flag);
752
+ ref = resolve_ref(ref, lock->old_sha1, mustexist, &type);
753
if (!ref && errno == EISDIR) {
754
/* we are trying to lock foo but we used to
755
* have foo/bar which now does not exist;
@@ -761,8 +762,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
762
error("there are still refs under '%s'", orig_ref);
763
goto error_return;
764
}
764
- ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, flag);
765
+ ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, &type);
766
}
767
+ if (type_p)
768
+ *type_p = type;
769
if (!ref) {
770
last_errno = errno;
771
error("unable to resolve reference %s: %s",
@@ -780,10 +783,15 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
783
784
lock->lk = xcalloc(1, sizeof(struct lock_file));
785
786
+ if (flags & REF_NODEREF)
787
+ ref = orig_ref;
788
lock->ref_name = xstrdup(ref);
789
lock->orig_ref_name = xstrdup(orig_ref);
790
ref_file = git_path("%s", ref);
786
- lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
791
+ if (lstat(ref_file, &st) && errno == ENOENT)
792
+ lock->force_write = 1;
793
+ if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
794
+ lock->force_write = 1;
795
796
if (safe_create_leading_directories(ref_file)) {
797
last_errno = errno;
@@ -806,14 +814,14 @@ struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
814
if (check_ref_format(ref))
815
return NULL;
816
strcpy(refpath, mkpath("refs/%s", ref));
809
- return lock_ref_sha1_basic(refpath, old_sha1, NULL);
817
+ return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
818
}
819
812
-struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1)
820
+struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int flags)
821
{
822
if (check_ref_format(ref) == -1)
823
return NULL;
816
- return lock_ref_sha1_basic(ref, old_sha1, NULL);
824
+ return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
825
}
826
827
static struct lock_file packlock;
@@ -858,7 +866,7 @@ int delete_ref(const char *refname, const unsigned char *sha1)
866
struct ref_lock *lock;
867
int err, i, ret = 0, flag = 0;
868
861
- lock = lock_ref_sha1_basic(refname, sha1, &flag);
869
+ lock = lock_ref_sha1_basic(refname, sha1, 0, &flag);
870
if (!lock)
871
return 1;
872
if (!(flag & REF_ISPACKED)) {
@@ -909,7 +917,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
917
if (!is_refname_available(newref, oldref, get_loose_refs(), 0))
918
return 1;
919
912
- lock = lock_ref_sha1_basic(renamed_ref, NULL, NULL);
920
+ lock = lock_ref_sha1_basic(renamed_ref, NULL, 0, NULL);
921
if (!lock)
922
return error("unable to lock %s", renamed_ref);
923
lock->force_write = 1;
@@ -963,7 +971,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
971
}
972
logmoved = log;
973
966
- lock = lock_ref_sha1_basic(newref, NULL, NULL);
974
+ lock = lock_ref_sha1_basic(newref, NULL, 0, NULL);
975
if (!lock) {
976
error("unable to lock %s for update", newref);
977
goto rollback;
@@ -979,7 +987,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
987
return 0;
988
989
rollback:
982
- lock = lock_ref_sha1_basic(oldref, NULL, NULL);
990
+ lock = lock_ref_sha1_basic(oldref, NULL, 0, NULL);
991
if (!lock) {
992
error("unable to lock %s for rollback", oldref);
993
goto rollbacklog;
refs.h
+2
-1
@@ -33,7 +33,8 @@ extern int get_ref_sha1(const char *ref, unsigned char *sha1);
33
extern struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1);
34
35
/** Locks any ref (for 'HEAD' type refs). */
36
-extern struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1);
36
+#define REF_NODEREF 0x01
37
+extern struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int flags);
38
39
/** Release any lock taken but not written. **/
40
extern void unlock_ref(struct ref_lock *lock);