sha1_file: perform object replacement in sha1_object_info_extended()
[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>..."),
19 N_("git replace -l [<pattern>]"),
54b0c1e0
CC
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{
9dfc3684 49 const char **p, *full_hex;
54b0c1e0
CC
50 char ref[PATH_MAX];
51 int had_error = 0;
52 unsigned char sha1[20];
53
54 for (p = argv; *p; p++) {
9dfc3684
MG
55 if (get_sha1(*p, sha1)) {
56 error("Failed to resolve '%s' as a valid ref.", *p);
54b0c1e0
CC
57 had_error = 1;
58 continue;
59 }
9dfc3684
MG
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/");
c6893323 64 if (read_ref(ref, sha1)) {
9dfc3684 65 error("replace ref '%s' not found.", full_hex);
54b0c1e0
CC
66 had_error = 1;
67 continue;
68 }
9dfc3684 69 if (fn(full_hex, ref, sha1))
54b0c1e0
CC
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
bebdd271
CC
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];
277336a5 88 enum object_type obj_type, repl_type;
bebdd271
CC
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);
8d9c5010 101 if (check_refname_format(ref, 0))
bebdd271
CC
102 die("'%s' is not a valid ref name.", ref);
103
277336a5
CC
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
c6893323 113 if (read_ref(ref, prev))
bebdd271
CC
114 hashclr(prev);
115 else if (!force)
116 die("replace ref '%s' already exists", ref);
117
9bbb0fa1 118 lock = lock_any_ref_for_update(ref, prev, 0, NULL);
bebdd271
CC
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
54b0c1e0
CC
127int cmd_replace(int argc, const char **argv, const char *prefix)
128{
bebdd271 129 int list = 0, delete = 0, force = 0;
54b0c1e0 130 struct option options[] = {
80f165a5
JN
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")),
54b0c1e0
CC
134 OPT_END()
135 };
136
451bb210 137 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
54b0c1e0
CC
138
139 if (list && delete)
86af2caa
CC
140 usage_msg_opt("-l and -d cannot be used together",
141 git_replace_usage, options);
54b0c1e0 142
bebdd271 143 if (force && (list || delete))
86af2caa
CC
144 usage_msg_opt("-f cannot be used with -d or -l",
145 git_replace_usage, options);
bebdd271
CC
146
147 /* Delete refs */
54b0c1e0
CC
148 if (delete) {
149 if (argc < 1)
86af2caa
CC
150 usage_msg_opt("-d needs at least one argument",
151 git_replace_usage, options);
54b0c1e0
CC
152 return for_each_replace_name(argv, delete_replace_ref);
153 }
154
bebdd271
CC
155 /* Replace object */
156 if (!list && argc) {
157 if (argc != 2)
86af2caa
CC
158 usage_msg_opt("bad number of arguments",
159 git_replace_usage, options);
bebdd271
CC
160 return replace_object(argv[0], argv[1], force);
161 }
162
54b0c1e0
CC
163 /* List refs, even if "list" is not set */
164 if (argc > 1)
86af2caa
CC
165 usage_msg_opt("only one pattern can be given with -l",
166 git_replace_usage, options);
bebdd271 167 if (force)
86af2caa
CC
168 usage_msg_opt("-f needs some arguments",
169 git_replace_usage, options);
54b0c1e0
CC
170
171 return list_replace_refs(argv[0]);
172}