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.
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>]"),
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
);
34 static int list_replace_refs(const char *pattern
)
39 for_each_replace_ref(show_reference
, (void *) pattern
);
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
;
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
);
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
);
69 if (fn(full_hex
, ref
, sha1
))
75 static int delete_replace_ref(const char *name
, const char *ref
,
76 const unsigned char *sha1
)
78 if (delete_ref(ref
, sha1
, 0))
80 printf("Deleted replace ref '%s'\n", name
);
84 static int replace_object(const char *object_ref
, const char *replace_ref
,
87 unsigned char object
[20], prev
[20], repl
[20];
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
),
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
))
106 die("replace ref '%s' already exists", ref
);
108 lock
= lock_any_ref_for_update(ref
, prev
, 0);
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
);
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")),
127 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
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
);
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
);
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 */
155 usage_msg_opt("only one pattern can be given with -l",
156 git_replace_usage
, options
);
158 usage_msg_opt("-f needs some arguments",
159 git_replace_usage
, options
);
161 return list_replace_refs(argv
[0]);