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 */
10
11#include "cache.h"
12#include "builtin.h"
13#include "refs.h"
14#include "parse-options.h"
15
16static 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
21};
22
23static int show_reference(const char *refname, const unsigned char *sha1,
24 int flag, void *cb_data)
25{
26 const char *pattern = cb_data;
27
28 if (!fnmatch(pattern, refname, 0))
29 printf("%s\n", refname);
30
31 return 0;
32}
33
34static int list_replace_refs(const char *pattern)
35{
36 if (pattern == NULL)
37 pattern = "*";
38
39 for_each_replace_ref(show_reference, (void *) pattern);
40
41 return 0;
42}
43
44typedef int (*each_replace_name_fn)(const char *name, const char *ref,
45 const unsigned char *sha1);
46
47static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
48{
49 const char **p, *full_hex;
50 char ref[PATH_MAX];
51 int had_error = 0;
52 unsigned char sha1[20];
53
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;
59 }
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;
68 }
69 if (fn(full_hex, ref, sha1))
70 had_error = 1;
71 }
72 return had_error;
73}
74
75static int delete_replace_ref(const char *name, const char *ref,
76 const unsigned char *sha1)
77{
78 if (delete_ref(ref, sha1, 0))
79 return 1;
80 printf("Deleted replace ref '%s'\n", name);
81 return 0;
82}
83
84static int replace_object(const char *object_ref, const char *replace_ref,
85 int force)
86{
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;
91
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);
96
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);
103
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));
112
113 if (read_ref(ref, prev))
114 hashclr(prev);
115 else if (!force)
116 die("replace ref '%s' already exists", ref);
117
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);
123
124 return 0;
125}
126
127int cmd_replace(int argc, const char **argv, const char *prefix)
128{
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()
135 };
136
137 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
138
139 if (list && delete)
140 usage_msg_opt("-l and -d cannot be used together",
141 git_replace_usage, options);
142
143 if (force && (list || delete))
144 usage_msg_opt("-f cannot be used with -d or -l",
145 git_replace_usage, options);
146
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);
153 }
154
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);
161 }
162
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);
170
171 return list_replace_refs(argv[0]);
172}