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 -d <object>...",
18 "git replace -l [<pattern>]",
22 static int show_reference(const char *refname
, const unsigned char *sha1
,
23 int flag
, void *cb_data
)
25 const char *pattern
= cb_data
;
27 if (!fnmatch(pattern
, refname
, 0))
28 printf("%s\n", refname
);
33 static int list_replace_refs(const char *pattern
)
38 for_each_replace_ref(show_reference
, (void *) pattern
);
43 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
44 const unsigned char *sha1
);
46 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
51 unsigned char sha1
[20];
53 for (p
= argv
; *p
; p
++) {
54 if (snprintf(ref
, sizeof(ref
), "refs/replace/%s", *p
)
56 error("replace ref name too long: %.*s...", 50, *p
);
60 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
61 error("replace ref '%s' not found.", *p
);
65 if (fn(*p
, ref
, sha1
))
71 static int delete_replace_ref(const char *name
, const char *ref
,
72 const unsigned char *sha1
)
74 if (delete_ref(ref
, sha1
, 0))
76 printf("Deleted replace ref '%s'\n", name
);
80 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
82 int list
= 0, delete = 0;
83 struct option options
[] = {
84 OPT_BOOLEAN('l', NULL
, &list
, "list replace refs"),
85 OPT_BOOLEAN('d', NULL
, &delete, "delete replace refs"),
89 argc
= parse_options(argc
, argv
, options
, git_replace_usage
, 0);
92 usage_with_options(git_replace_usage
, options
);
96 usage_with_options(git_replace_usage
, options
);
97 return for_each_replace_name(argv
, delete_replace_ref
);
100 /* List refs, even if "list" is not set */
102 usage_with_options(git_replace_usage
, options
);
104 return list_replace_refs(argv
[0]);