Code

Add new "git replace" command
[git.git] / builtin-replace.c
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  */
11 #include "cache.h"
12 #include "builtin.h"
13 #include "refs.h"
14 #include "parse-options.h"
16 static const char * const git_replace_usage[] = {
17         "git replace -d <object>...",
18         "git replace -l [<pattern>]",
19         NULL
20 };
22 static int show_reference(const char *refname, const unsigned char *sha1,
23                           int flag, void *cb_data)
24 {
25         const char *pattern = cb_data;
27         if (!fnmatch(pattern, refname, 0))
28                 printf("%s\n", refname);
30         return 0;
31 }
33 static int list_replace_refs(const char *pattern)
34 {
35         if (pattern == NULL)
36                 pattern = "*";
38         for_each_replace_ref(show_reference, (void *) pattern);
40         return 0;
41 }
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)
47 {
48         const char **p;
49         char ref[PATH_MAX];
50         int had_error = 0;
51         unsigned char sha1[20];
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 }
71 static 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 }
80 int 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         };
89         argc = parse_options(argc, argv, options, git_replace_usage, 0);
91         if (list && delete)
92                 usage_with_options(git_replace_usage, options);
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         }
100         /* List refs, even if "list" is not set */
101         if (argc > 1)
102                 usage_with_options(git_replace_usage, options);
104         return list_replace_refs(argv[0]);