git_mkstemps: correctly test return value of open()
[git/mingw.git] / builtin / replace.c
blob398ccd5eaa243045c5d21af1e37462846665568a
1 /*
2 * Builtin "git replace"
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
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 N_("git replace [-f] <object> <replacement>"),
18 N_("git replace -d <object>..."),
19 N_("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, *full_hex;
50 char ref[PATH_MAX];
51 int had_error = 0;
52 unsigned char sha1[20];
54 for (p = argv; *p; p++) {
55 if (get_sha1(*p, sha1)) {
56 error("Failed to resolve '%s' as a valid ref.", *p);
57 had_error = 1;
58 continue;
60 full_hex = sha1_to_hex(sha1);
61 snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
62 /* read_ref() may reuse the buffer */
63 full_hex = ref + strlen("refs/replace/");
64 if (read_ref(ref, sha1)) {
65 error("replace ref '%s' not found.", full_hex);
66 had_error = 1;
67 continue;
69 if (fn(full_hex, ref, sha1))
70 had_error = 1;
72 return had_error;
75 static int delete_replace_ref(const char *name, const char *ref,
76 const unsigned char *sha1)
78 if (delete_ref(ref, sha1, 0))
79 return 1;
80 printf("Deleted replace ref '%s'\n", name);
81 return 0;
84 static int replace_object(const char *object_ref, const char *replace_ref,
85 int force)
87 unsigned char object[20], prev[20], repl[20];
88 char ref[PATH_MAX];
89 struct ref_lock *lock;
91 if (get_sha1(object_ref, object))
92 die("Failed to resolve '%s' as a valid ref.", object_ref);
93 if (get_sha1(replace_ref, repl))
94 die("Failed to resolve '%s' as a valid ref.", replace_ref);
96 if (snprintf(ref, sizeof(ref),
97 "refs/replace/%s",
98 sha1_to_hex(object)) > sizeof(ref) - 1)
99 die("replace ref name too long: %.*s...", 50, ref);
100 if (check_refname_format(ref, 0))
101 die("'%s' is not a valid ref name.", ref);
103 if (read_ref(ref, prev))
104 hashclr(prev);
105 else if (!force)
106 die("replace ref '%s' already exists", ref);
108 lock = lock_any_ref_for_update(ref, prev, 0);
109 if (!lock)
110 die("%s: cannot lock the ref", ref);
111 if (write_ref_sha1(lock, repl, NULL) < 0)
112 die("%s: cannot update the ref", ref);
114 return 0;
117 int cmd_replace(int argc, const char **argv, const char *prefix)
119 int list = 0, delete = 0, force = 0;
120 struct option options[] = {
121 OPT_BOOLEAN('l', NULL, &list, N_("list replace refs")),
122 OPT_BOOLEAN('d', NULL, &delete, N_("delete replace refs")),
123 OPT_BOOLEAN('f', NULL, &force, N_("replace the ref if it exists")),
124 OPT_END()
127 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
129 if (list && delete)
130 usage_msg_opt("-l and -d cannot be used together",
131 git_replace_usage, options);
133 if (force && (list || delete))
134 usage_msg_opt("-f cannot be used with -d or -l",
135 git_replace_usage, options);
137 /* Delete refs */
138 if (delete) {
139 if (argc < 1)
140 usage_msg_opt("-d needs at least one argument",
141 git_replace_usage, options);
142 return for_each_replace_name(argv, delete_replace_ref);
145 /* Replace object */
146 if (!list && argc) {
147 if (argc != 2)
148 usage_msg_opt("bad number of arguments",
149 git_replace_usage, options);
150 return replace_object(argv[0], argv[1], force);
153 /* List refs, even if "list" is not set */
154 if (argc > 1)
155 usage_msg_opt("only one pattern can be given with -l",
156 git_replace_usage, options);
157 if (force)
158 usage_msg_opt("-f needs some arguments",
159 git_replace_usage, options);
161 return list_replace_refs(argv[0]);