Add new "git replace" command
[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 *
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.
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[] = {
17 "git replace -d <object>...",
18 "git replace -l [<pattern>]",
19 NULL
20};
21
22static int show_reference(const char *refname, const unsigned char *sha1,
23 int flag, void *cb_data)
24{
25 const char *pattern = cb_data;
26
27 if (!fnmatch(pattern, refname, 0))
28 printf("%s\n", refname);
29
30 return 0;
31}
32
33static int list_replace_refs(const char *pattern)
34{
35 if (pattern == NULL)
36 pattern = "*";
37
38 for_each_replace_ref(show_reference, (void *) pattern);
39
40 return 0;
41}
42
43typedef int (*each_replace_name_fn)(const char *name, const char *ref,
44 const unsigned char *sha1);
45
46static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
47{
48 const char **p;
49 char ref[PATH_MAX];
50 int had_error = 0;
51 unsigned char sha1[20];
52
53 for (p = argv; *p; p++) {
54 if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p)
55 >= sizeof(ref)) {
56 error("replace ref name too long: %.*s...", 50, *p);
57 had_error = 1;
58 continue;
59 }
60 if (!resolve_ref(ref, sha1, 1, NULL)) {
61 error("replace ref '%s' not found.", *p);
62 had_error = 1;
63 continue;
64 }
65 if (fn(*p, ref, sha1))
66 had_error = 1;
67 }
68 return had_error;
69}
70
71static int delete_replace_ref(const char *name, const char *ref,
72 const unsigned char *sha1)
73{
74 if (delete_ref(ref, sha1, 0))
75 return 1;
76 printf("Deleted replace ref '%s'\n", name);
77 return 0;
78}
79
80int cmd_replace(int argc, const char **argv, const char *prefix)
81{
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"),
86 OPT_END()
87 };
88
89 argc = parse_options(argc, argv, options, git_replace_usage, 0);
90
91 if (list && delete)
92 usage_with_options(git_replace_usage, options);
93
94 if (delete) {
95 if (argc < 1)
96 usage_with_options(git_replace_usage, options);
97 return for_each_replace_name(argv, delete_replace_ref);
98 }
99
100 /* List refs, even if "list" is not set */
101 if (argc > 1)
102 usage_with_options(git_replace_usage, options);
103
104 return list_replace_refs(argv[0]);
105}