t6050: add tests for listing with --format
[git/git.git] / builtin / replace.c
CommitLineData
54b0c1e0
CC
1/*
2 * Builtin "git replace"
3 *
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
5 *
09b7e220 6 * Based on builtin/tag.c by Kristian Høgsberg <krh@redhat.com>
54b0c1e0
CC
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[] = {
2477bebb
NTND
17 N_("git replace [-f] <object> <replacement>"),
18 N_("git replace -d <object>..."),
44f9f850 19 N_("git replace [--format=<format>] [-l [<pattern>]]"),
54b0c1e0
CC
20 NULL
21};
22
44f9f850
CC
23enum repl_fmt { SHORT, MEDIUM, FULL };
24
25struct show_data {
26 const char *pattern;
27 enum repl_fmt fmt;
28};
29
54b0c1e0
CC
30static int show_reference(const char *refname, const unsigned char *sha1,
31 int flag, void *cb_data)
32{
44f9f850
CC
33 struct show_data *data = cb_data;
34
35 if (!fnmatch(data->pattern, refname, 0)) {
36 if (data->fmt == SHORT)
37 printf("%s\n", refname);
38 else if (data->fmt == MEDIUM)
39 printf("%s -> %s\n", refname, sha1_to_hex(sha1));
40 else { /* data->fmt == FULL */
41 unsigned char object[20];
42 enum object_type obj_type, repl_type;
54b0c1e0 43
44f9f850
CC
44 if (get_sha1(refname, object))
45 return error("Failed to resolve '%s' as a valid ref.", refname);
46
47 obj_type = sha1_object_info(object, NULL);
48 repl_type = sha1_object_info(sha1, NULL);
49
50 printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
51 sha1_to_hex(sha1), typename(repl_type));
52 }
53 }
54b0c1e0
CC
54
55 return 0;
56}
57
44f9f850 58static int list_replace_refs(const char *pattern, const char *format)
54b0c1e0 59{
44f9f850
CC
60 struct show_data data;
61
54b0c1e0
CC
62 if (pattern == NULL)
63 pattern = "*";
44f9f850
CC
64 data.pattern = pattern;
65
66 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
67 data.fmt = SHORT;
68 else if (!strcmp(format, "medium"))
69 data.fmt = MEDIUM;
70 else if (!strcmp(format, "full"))
71 data.fmt = FULL;
72 else
73 die("invalid replace format '%s'\n"
74 "valid formats are 'short', 'medium' and 'full'\n",
75 format);
54b0c1e0 76
44f9f850 77 for_each_replace_ref(show_reference, (void *) &data);
54b0c1e0
CC
78
79 return 0;
80}
81
82typedef int (*each_replace_name_fn)(const char *name, const char *ref,
83 const unsigned char *sha1);
84
85static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
86{
9dfc3684 87 const char **p, *full_hex;
54b0c1e0
CC
88 char ref[PATH_MAX];
89 int had_error = 0;
90 unsigned char sha1[20];
91
92 for (p = argv; *p; p++) {
9dfc3684
MG
93 if (get_sha1(*p, sha1)) {
94 error("Failed to resolve '%s' as a valid ref.", *p);
54b0c1e0
CC
95 had_error = 1;
96 continue;
97 }
9dfc3684
MG
98 full_hex = sha1_to_hex(sha1);
99 snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
100 /* read_ref() may reuse the buffer */
101 full_hex = ref + strlen("refs/replace/");
c6893323 102 if (read_ref(ref, sha1)) {
9dfc3684 103 error("replace ref '%s' not found.", full_hex);
54b0c1e0
CC
104 had_error = 1;
105 continue;
106 }
9dfc3684 107 if (fn(full_hex, ref, sha1))
54b0c1e0
CC
108 had_error = 1;
109 }
110 return had_error;
111}
112
113static int delete_replace_ref(const char *name, const char *ref,
114 const unsigned char *sha1)
115{
116 if (delete_ref(ref, sha1, 0))
117 return 1;
118 printf("Deleted replace ref '%s'\n", name);
119 return 0;
120}
121
bebdd271
CC
122static int replace_object(const char *object_ref, const char *replace_ref,
123 int force)
124{
125 unsigned char object[20], prev[20], repl[20];
277336a5 126 enum object_type obj_type, repl_type;
bebdd271
CC
127 char ref[PATH_MAX];
128 struct ref_lock *lock;
129
130 if (get_sha1(object_ref, object))
131 die("Failed to resolve '%s' as a valid ref.", object_ref);
132 if (get_sha1(replace_ref, repl))
133 die("Failed to resolve '%s' as a valid ref.", replace_ref);
134
135 if (snprintf(ref, sizeof(ref),
136 "refs/replace/%s",
137 sha1_to_hex(object)) > sizeof(ref) - 1)
138 die("replace ref name too long: %.*s...", 50, ref);
8d9c5010 139 if (check_refname_format(ref, 0))
bebdd271
CC
140 die("'%s' is not a valid ref name.", ref);
141
277336a5
CC
142 obj_type = sha1_object_info(object, NULL);
143 repl_type = sha1_object_info(repl, NULL);
144 if (!force && obj_type != repl_type)
145 die("Objects must be of the same type.\n"
146 "'%s' points to a replaced object of type '%s'\n"
147 "while '%s' points to a replacement object of type '%s'.",
148 object_ref, typename(obj_type),
149 replace_ref, typename(repl_type));
150
c6893323 151 if (read_ref(ref, prev))
bebdd271
CC
152 hashclr(prev);
153 else if (!force)
154 die("replace ref '%s' already exists", ref);
155
9bbb0fa1 156 lock = lock_any_ref_for_update(ref, prev, 0, NULL);
bebdd271
CC
157 if (!lock)
158 die("%s: cannot lock the ref", ref);
159 if (write_ref_sha1(lock, repl, NULL) < 0)
160 die("%s: cannot update the ref", ref);
161
162 return 0;
163}
164
54b0c1e0
CC
165int cmd_replace(int argc, const char **argv, const char *prefix)
166{
bebdd271 167 int list = 0, delete = 0, force = 0;
44f9f850 168 const char *format = NULL;
54b0c1e0 169 struct option options[] = {
80f165a5
JN
170 OPT_BOOL('l', "list", &list, N_("list replace refs")),
171 OPT_BOOL('d', "delete", &delete, N_("delete replace refs")),
172 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
44f9f850 173 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
54b0c1e0
CC
174 OPT_END()
175 };
176
451bb210 177 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
54b0c1e0
CC
178
179 if (list && delete)
86af2caa
CC
180 usage_msg_opt("-l and -d cannot be used together",
181 git_replace_usage, options);
54b0c1e0 182
44f9f850
CC
183 if (format && delete)
184 usage_msg_opt("--format and -d cannot be used together",
185 git_replace_usage, options);
186
bebdd271 187 if (force && (list || delete))
86af2caa
CC
188 usage_msg_opt("-f cannot be used with -d or -l",
189 git_replace_usage, options);
bebdd271
CC
190
191 /* Delete refs */
54b0c1e0
CC
192 if (delete) {
193 if (argc < 1)
86af2caa
CC
194 usage_msg_opt("-d needs at least one argument",
195 git_replace_usage, options);
54b0c1e0
CC
196 return for_each_replace_name(argv, delete_replace_ref);
197 }
198
bebdd271
CC
199 /* Replace object */
200 if (!list && argc) {
201 if (argc != 2)
86af2caa
CC
202 usage_msg_opt("bad number of arguments",
203 git_replace_usage, options);
44f9f850
CC
204 if (format)
205 usage_msg_opt("--format cannot be used when not listing",
206 git_replace_usage, options);
bebdd271
CC
207 return replace_object(argv[0], argv[1], force);
208 }
209
54b0c1e0
CC
210 /* List refs, even if "list" is not set */
211 if (argc > 1)
86af2caa
CC
212 usage_msg_opt("only one pattern can be given with -l",
213 git_replace_usage, options);
bebdd271 214 if (force)
86af2caa
CC
215 usage_msg_opt("-f needs some arguments",
216 git_replace_usage, options);
54b0c1e0 217
44f9f850 218 return list_replace_refs(argv[0], format);
54b0c1e0 219}