Windows: boost startup by avoiding a static dependency on shell32.dll
[git/dscho.git] / builtin-replace.c
blobfe3a647a36c9a064d32c8bf9d9868da041bd82a2
1 /*
2  * Builtin "git replace"
3  *
4  * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
5  *
6  * Based on builtin-tag.c by Kristian Høgsberg <krh@redhat.com>
7  * and Carlos Rica <jasampler@gmail.com> that was itself based on
8  * git-tag.sh and mktag.c by Linus Torvalds.
9  */
11 #include "cache.h"
12 #include "builtin.h"
13 #include "refs.h"
14 #include "parse-options.h"
16 static const char * const git_replace_usage[] = {
17         "git replace [-f] <object> <replacement>",
18         "git replace -d <object>...",
19         "git replace -l [<pattern>]",
20         NULL
23 static int show_reference(const char *refname, const unsigned char *sha1,
24                           int flag, void *cb_data)
26         const char *pattern = cb_data;
28         if (!fnmatch(pattern, refname, 0))
29                 printf("%s\n", refname);
31         return 0;
34 static int list_replace_refs(const char *pattern)
36         if (pattern == NULL)
37                 pattern = "*";
39         for_each_replace_ref(show_reference, (void *) pattern);
41         return 0;
44 typedef int (*each_replace_name_fn)(const char *name, const char *ref,
45                                     const unsigned char *sha1);
47 static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
49         const char **p;
50         char ref[PATH_MAX];
51         int had_error = 0;
52         unsigned char sha1[20];
54         for (p = argv; *p; p++) {
55                 if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p)
56                                         >= sizeof(ref)) {
57                         error("replace ref name too long: %.*s...", 50, *p);
58                         had_error = 1;
59                         continue;
60                 }
61                 if (!resolve_ref(ref, sha1, 1, NULL)) {
62                         error("replace ref '%s' not found.", *p);
63                         had_error = 1;
64                         continue;
65                 }
66                 if (fn(*p, ref, sha1))
67                         had_error = 1;
68         }
69         return had_error;
72 static int delete_replace_ref(const char *name, const char *ref,
73                               const unsigned char *sha1)
75         if (delete_ref(ref, sha1, 0))
76                 return 1;
77         printf("Deleted replace ref '%s'\n", name);
78         return 0;
81 static int replace_object(const char *object_ref, const char *replace_ref,
82                           int force)
84         unsigned char object[20], prev[20], repl[20];
85         char ref[PATH_MAX];
86         struct ref_lock *lock;
88         if (get_sha1(object_ref, object))
89                 die("Failed to resolve '%s' as a valid ref.", object_ref);
90         if (get_sha1(replace_ref, repl))
91                 die("Failed to resolve '%s' as a valid ref.", replace_ref);
93         if (snprintf(ref, sizeof(ref),
94                      "refs/replace/%s",
95                      sha1_to_hex(object)) > sizeof(ref) - 1)
96                 die("replace ref name too long: %.*s...", 50, ref);
97         if (check_ref_format(ref))
98                 die("'%s' is not a valid ref name.", ref);
100         if (!resolve_ref(ref, prev, 1, NULL))
101                 hashclr(prev);
102         else if (!force)
103                 die("replace ref '%s' already exists", ref);
105         lock = lock_any_ref_for_update(ref, prev, 0);
106         if (!lock)
107                 die("%s: cannot lock the ref", ref);
108         if (write_ref_sha1(lock, repl, NULL) < 0)
109                 die("%s: cannot update the ref", ref);
111         return 0;
114 int cmd_replace(int argc, const char **argv, const char *prefix)
116         int list = 0, delete = 0, force = 0;
117         struct option options[] = {
118                 OPT_BOOLEAN('l', NULL, &list, "list replace refs"),
119                 OPT_BOOLEAN('d', NULL, &delete, "delete replace refs"),
120                 OPT_BOOLEAN('f', NULL, &force, "replace the ref if it exists"),
121                 OPT_END()
122         };
124         argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
126         if (list && delete)
127                 usage_msg_opt("-l and -d cannot be used together",
128                               git_replace_usage, options);
130         if (force && (list || delete))
131                 usage_msg_opt("-f cannot be used with -d or -l",
132                               git_replace_usage, options);
134         /* Delete refs */
135         if (delete) {
136                 if (argc < 1)
137                         usage_msg_opt("-d needs at least one argument",
138                                       git_replace_usage, options);
139                 return for_each_replace_name(argv, delete_replace_ref);
140         }
142         /* Replace object */
143         if (!list && argc) {
144                 if (argc != 2)
145                         usage_msg_opt("bad number of arguments",
146                                       git_replace_usage, options);
147                 return replace_object(argv[0], argv[1], force);
148         }
150         /* List refs, even if "list" is not set */
151         if (argc > 1)
152                 usage_msg_opt("only one pattern can be given with -l",
153                               git_replace_usage, options);
154         if (force)
155                 usage_msg_opt("-f needs some arguments",
156                               git_replace_usage, options);
158         return list_replace_refs(argv[0]);