Clean up builtin-update-ref's option parsing
builtin-update-ref's option parsing was somewhat tricky to follow, especially if the -d option was given. This patch cleans it upp a bit, at the expense of making it a bit longer. Signed-off-by: Karl Hasselström <kha@treskal.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karl Hasselström committed
Jun 3, 2008 at 01:34 UTC
973a70ea4d9fc98e9ed20c261c5f6c8f1c1df2b0
1 file changed
+19
-15
builtin-update-ref.c
+19
-15
@@ -11,7 +11,7 @@ static const char * const git_update_ref_usage[] = {
11
12
int cmd_update_ref(int argc, const char **argv, const char *prefix)
13
{
14
- const char *refname, *value, *oldval, *msg=NULL;
14
+ const char *refname, *oldval, *msg=NULL;
15
unsigned char sha1[20], oldsha1[20];
16
int delete = 0, no_deref = 0;
17
struct option options[] = {
@@ -27,25 +27,29 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
27
if (msg && !*msg)
28
die("Refusing to perform update with empty message.");
29
30
- if (argc < 2 || argc > 3)
31
- usage_with_options(git_update_ref_usage, options);
32
- refname = argv[0];
33
- value = argv[1];
34
- oldval = argv[2];
35
-
36
- if (get_sha1(value, sha1))
37
- die("%s: not a valid SHA1", value);
38
-
30
if (delete) {
40
- if (oldval)
31
+ if (argc != 2)
32
+ usage_with_options(git_update_ref_usage, options);
33
+ refname = argv[0];
34
+ oldval = argv[1];
35
+ } else {
36
+ const char *value;
37
+ if (argc < 2 || argc > 3)
38
usage_with_options(git_update_ref_usage, options);
42
- return delete_ref(refname, sha1);
39
+ refname = argv[0];
40
+ value = argv[1];
41
+ oldval = argv[2];
42
+ if (get_sha1(value, sha1))
43
+ die("%s: not a valid SHA1", value);
44
}
45
45
- hashclr(oldsha1);
46
+ hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
47
if (oldval && *oldval && get_sha1(oldval, oldsha1))
48
die("%s: not a valid old SHA1", oldval);
49
49
- return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
50
- no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
50
+ if (delete)
51
+ return delete_ref(refname, oldsha1);
52
+ else
53
+ return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
54
+ no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
55
}