Make builtin-update-ref.c use parse_options
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Pierre Habouzit committed
Oct 7, 2007 at 23:14 UTC
89942be6a13f69a34e5e824977b45204c36cf9e8
1 file changed
+25
-40
builtin-update-ref.c
+25
-40
@@ -1,59 +1,44 @@
1
#include "cache.h"
2
#include "refs.h"
3
#include "builtin.h"
4
+#include "parse-options.h"
5
5
-static const char git_update_ref_usage[] =
6
-"git-update-ref [-m <reason>] (-d <refname> <value> | [--no-deref] <refname> <value> [<oldval>])";
6
+static const char * const git_update_ref_usage[] = {
7
+ "git-update-ref [options] -d <refname> <oldval>",
8
+ "git-update-ref [options] <refname> <newval> [<oldval>]",
9
+ NULL
10
+};
11
12
int cmd_update_ref(int argc, const char **argv, const char *prefix)
13
{
10
- const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
14
+ const char *refname, *value, *oldval, *msg=NULL;
15
unsigned char sha1[20], oldsha1[20];
12
- int i, delete, ref_flags;
16
+ int delete = 0, no_deref = 0;
17
+ struct option options[] = {
18
+ OPT_STRING( 'm', NULL, &msg, "reason", "reason of the update"),
19
+ OPT_BOOLEAN('d', NULL, &delete, "deletes the reference"),
20
+ OPT_BOOLEAN( 0 , "no-deref", &no_deref,
21
+ "update <refname> not the one it points to"),
22
+ OPT_END(),
23
+ };
24
14
- delete = 0;
15
- ref_flags = 0;
25
git_config(git_default_config);
26
+ argc = parse_options(argc, argv, options, git_update_ref_usage, 0);
27
+ if (msg && !*msg)
28
+ die("Refusing to perform update with empty message.");
29
18
- for (i = 1; i < argc; i++) {
19
- if (!strcmp("-m", argv[i])) {
20
- if (i+1 >= argc)
21
- usage(git_update_ref_usage);
22
- msg = argv[++i];
23
- if (!*msg)
24
- die("Refusing to perform update with empty message.");
25
- continue;
26
- }
27
- if (!strcmp("-d", argv[i])) {
28
- delete = 1;
29
- continue;
30
- }
31
- if (!strcmp("--no-deref", argv[i])) {
32
- ref_flags |= REF_NODEREF;
33
- continue;
34
- }
35
- if (!refname) {
36
- refname = argv[i];
37
- continue;
38
- }
39
- if (!value) {
40
- value = argv[i];
41
- continue;
42
- }
43
- if (!oldval) {
44
- oldval = argv[i];
45
- continue;
46
- }
47
- }
48
- if (!refname || !value)
49
- usage(git_update_ref_usage);
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
39
if (delete) {
40
if (oldval)
56
- usage(git_update_ref_usage);
41
+ usage_with_options(git_update_ref_usage, options);
42
return delete_ref(refname, sha1);
43
}
44
@@ -62,5 +47,5 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
47
die("%s: not a valid old SHA1", oldval);
48
49
return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
65
- ref_flags, DIE_ON_ERR);
50
+ no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
51
}