repack: introduce repack.writeBitmaps config option
[git.git] / builtin / replace.c
blobb1bd3ef9946761b146a38d5977a7378175cda047
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 enum object_type obj_type, repl_type;
89 char ref[PATH_MAX];
90 struct ref_lock *lock;
92 if (get_sha1(object_ref, object))
93 die("Failed to resolve '%s' as a valid ref.", object_ref);
94 if (get_sha1(replace_ref, repl))
95 die("Failed to resolve '%s' as a valid ref.", replace_ref);
97 if (snprintf(ref, sizeof(ref),
98 "refs/replace/%s",
99 sha1_to_hex(object)) > sizeof(ref) - 1)
100 die("replace ref name too long: %.*s...", 50, ref);
101 if (check_refname_format(ref, 0))
102 die("'%s' is not a valid ref name.", ref);
104 obj_type = sha1_object_info(object, NULL);
105 repl_type = sha1_object_info(repl, NULL);
106 if (!force && obj_type != repl_type)
107 die("Objects must be of the same type.\n"
108 "'%s' points to a replaced object of type '%s'\n"
109 "while '%s' points to a replacement object of type '%s'.",
110 object_ref, typename(obj_type),
111 replace_ref, typename(repl_type));
113 if (read_ref(ref, prev))
114 hashclr(prev);
115 else if (!force)
116 die("replace ref '%s' already exists", ref);
118 lock = lock_any_ref_for_update(ref, prev, 0, NULL);
119 if (!lock)
120 die("%s: cannot lock the ref", ref);
121 if (write_ref_sha1(lock, repl, NULL) < 0)
122 die("%s: cannot update the ref", ref);
124 return 0;
127 int cmd_replace(int argc, const char **argv, const char *prefix)
129 int list = 0, delete = 0, force = 0;
130 struct option options[] = {
131 OPT_BOOL('l', "list", &list, N_("list replace refs")),
132 OPT_BOOL('d', "delete", &delete, N_("delete replace refs")),
133 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
134 OPT_END()
137 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
139 if (list && delete)
140 usage_msg_opt("-l and -d cannot be used together",
141 git_replace_usage, options);
143 if (force && (list || delete))
144 usage_msg_opt("-f cannot be used with -d or -l",
145 git_replace_usage, options);
147 /* Delete refs */
148 if (delete) {
149 if (argc < 1)
150 usage_msg_opt("-d needs at least one argument",
151 git_replace_usage, options);
152 return for_each_replace_name(argv, delete_replace_ref);
155 /* Replace object */
156 if (!list && argc) {
157 if (argc != 2)
158 usage_msg_opt("bad number of arguments",
159 git_replace_usage, options);
160 return replace_object(argv[0], argv[1], force);
163 /* List refs, even if "list" is not set */
164 if (argc > 1)
165 usage_msg_opt("only one pattern can be given with -l",
166 git_replace_usage, options);
167 if (force)
168 usage_msg_opt("-f needs some arguments",
169 git_replace_usage, options);
171 return list_replace_refs(argv[0]);