Convert update-ref to use ref_lock API.
This conversion also adds the '-m' switch to update-ref allowing the caller to record why the ref is changing. At present this is merely copied down into the ref_lock API. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn Pearce committed
May 17, 2006 at 05:55 UTC
5b16b090211f549c501f2cd9e8ea1bd227a575c5
2 files changed
+38
-67
Documentation/git-update-ref.txt
+1
-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' <ref> <newvalue> [<oldvalue>]
10
+'git-update-ref' <ref> <newvalue> [<oldvalue>] [-m <reason>]
11
12
DESCRIPTION
13
-----------
update-ref.c
+37
-66
@@ -1,85 +1,56 @@
1
#include "cache.h"
2
#include "refs.h"
3
4
-static const char git_update_ref_usage[] = "git-update-ref <refname> <value> [<oldval>]";
5
-
6
-static int re_verify(const char *path, unsigned char *oldsha1, unsigned char *currsha1)
7
-{
8
- char buf[40];
9
- int fd = open(path, O_RDONLY), nr;
10
- if (fd < 0)
11
- return -1;
12
- nr = read(fd, buf, 40);
13
- close(fd);
14
- if (nr != 40 || get_sha1_hex(buf, currsha1) < 0)
15
- return -1;
16
- return memcmp(oldsha1, currsha1, 20) ? -1 : 0;
17
-}
4
+static const char git_update_ref_usage[] =
5
+"git-update-ref <refname> <value> [<oldval>] [-m <reason>]";
6
7
int main(int argc, char **argv)
8
{
21
- char *hex;
22
- const char *refname, *value, *oldval, *path;
23
- char *lockpath;
24
- unsigned char sha1[20], oldsha1[20], currsha1[20];
25
- int fd, written;
9
+ const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
10
+ struct ref_lock *lock;
11
+ unsigned char sha1[20], oldsha1[20];
12
+ int i;
13
14
setup_git_directory();
15
git_config(git_default_config);
29
- if (argc < 3 || argc > 4)
16
+
17
+ for (i = 1; i < argc; i++) {
18
+ if (!strcmp("-m", argv[i])) {
19
+ if (i+1 >= argc)
20
+ usage(git_update_ref_usage);
21
+ msg = argv[++i];
22
+ if (!*msg)
23
+ die("Refusing to perform update with empty message.");
24
+ if (strchr(msg, '\n'))
25
+ die("Refusing to perform update with \\n in message.");
26
+ continue;
27
+ }
28
+ if (!refname) {
29
+ refname = argv[i];
30
+ continue;
31
+ }
32
+ if (!value) {
33
+ value = argv[i];
34
+ continue;
35
+ }
36
+ if (!oldval) {
37
+ oldval = argv[i];
38
+ continue;
39
+ }
40
+ }
41
+ if (!refname || !value)
42
usage(git_update_ref_usage);
43
32
- refname = argv[1];
33
- value = argv[2];
34
- oldval = argv[3];
44
if (get_sha1(value, sha1))
45
die("%s: not a valid SHA1", value);
46
memset(oldsha1, 0, 20);
47
if (oldval && get_sha1(oldval, oldsha1))
48
die("%s: not a valid old SHA1", oldval);
49
41
- path = resolve_ref(git_path("%s", refname), currsha1, !!oldval);
42
- if (!path)
43
- die("No such ref: %s", refname);
44
-
45
- if (oldval) {
46
- if (memcmp(currsha1, oldsha1, 20))
47
- die("Ref %s is at %s but expected %s", refname, sha1_to_hex(currsha1), sha1_to_hex(oldsha1));
48
- /* Nothing to do? */
49
- if (!memcmp(oldsha1, sha1, 20))
50
- exit(0);
51
- }
52
- path = strdup(path);
53
- lockpath = mkpath("%s.lock", path);
54
- if (safe_create_leading_directories(lockpath) < 0)
55
- die("Unable to create all of %s", lockpath);
56
-
57
- fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
58
- if (fd < 0)
59
- die("Unable to create %s", lockpath);
60
- hex = sha1_to_hex(sha1);
61
- hex[40] = '\n';
62
- written = write(fd, hex, 41);
63
- close(fd);
64
- if (written != 41) {
65
- unlink(lockpath);
66
- die("Unable to write to %s", lockpath);
67
- }
68
-
69
- /*
70
- * Re-read the ref after getting the lock to verify
71
- */
72
- if (oldval && re_verify(path, oldsha1, currsha1) < 0) {
73
- unlink(lockpath);
74
- die("Ref lock failed");
75
- }
76
-
77
- /*
78
- * Finally, replace the old ref with the new one
79
- */
80
- if (rename(lockpath, path) < 0) {
81
- unlink(lockpath);
82
- die("Unable to create %s", path);
83
- }
50
+ lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, 0);
51
+ if (!lock)
52
+ return 1;
53
+ if (write_ref_sha1(lock, sha1, msg) < 0)
54
+ return 1;
55
return 0;
56
}