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 "git replace [-f] <object> <replacement>",
18 "git replace -d <object>...",
19 "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
)
52 unsigned char sha1
[20];
54 for (p
= argv
; *p
; p
++) {
55 if (snprintf(ref
, sizeof(ref
), "refs/replace/%s", *p
)
57 error("replace ref name too long: %.*s...", 50, *p
);
61 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
62 error("replace ref '%s' not found.", *p
);
66 if (fn(*p
, ref
, sha1
))
72 static int delete_replace_ref(const char *name
, const char *ref
,
73 const unsigned char *sha1
)
75 if (delete_ref(ref
, sha1
, 0))
77 printf("Deleted replace ref '%s'\n", name
);
81 static int replace_object(const char *object_ref
, const char *replace_ref
,
84 unsigned char object
[20], prev
[20], repl
[20];
86 struct ref_lock
*lock
;
88 if (get_sha1(object_ref
, object
))
89 die("Failed to resolve '%s' as a valid ref.", object_ref
);
90 if (get_sha1(replace_ref
, repl
))
91 die("Failed to resolve '%s' as a valid ref.", replace_ref
);
93 if (snprintf(ref
, sizeof(ref
),
95 sha1_to_hex(object
)) > sizeof(ref
) - 1)
96 die("replace ref name too long: %.*s...", 50, ref
);
97 if (check_refname_format(ref
, 0))
98 die("'%s' is not a valid ref name.", ref
);
100 if (!resolve_ref(ref
, prev
, 1, NULL
))
103 die("replace ref '%s' already exists", ref
);
105 lock
= lock_any_ref_for_update(ref
, prev
, 0);
107 die("%s: cannot lock the ref", ref
);
108 if (write_ref_sha1(lock
, repl
, NULL
) < 0)
109 die("%s: cannot update the ref", ref
);
114 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
116 int list
= 0, delete = 0, force
= 0;
117 struct option options
[] = {
118 OPT_BOOLEAN('l', NULL
, &list
, "list replace refs"),
119 OPT_BOOLEAN('d', NULL
, &delete, "delete replace refs"),
120 OPT_BOOLEAN('f', NULL
, &force
, "replace the ref if it exists"),
124 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
127 usage_msg_opt("-l and -d cannot be used together",
128 git_replace_usage
, options
);
130 if (force
&& (list
|| delete))
131 usage_msg_opt("-f cannot be used with -d or -l",
132 git_replace_usage
, options
);
137 usage_msg_opt("-d needs at least one argument",
138 git_replace_usage
, options
);
139 return for_each_replace_name(argv
, delete_replace_ref
);
145 usage_msg_opt("bad number of arguments",
146 git_replace_usage
, options
);
147 return replace_object(argv
[0], argv
[1], force
);
150 /* List refs, even if "list" is not set */
152 usage_msg_opt("only one pattern can be given with -l",
153 git_replace_usage
, options
);
155 usage_msg_opt("-f needs some arguments",
156 git_replace_usage
, options
);
158 return list_replace_refs(argv
[0]);