builtin / replace.con commit Merge branch 'mm/status-push-pull-advise' (fb4c622)
   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        N_("git replace [-f] <object> <replacement>"),
  18        N_("git replace -d <object>..."),
  19        N_("git replace -l [<pattern>]"),
  20        NULL
  21};
  22
  23static int show_reference(const char *refname, const unsigned char *sha1,
  24                          int flag, void *cb_data)
  25{
  26        const char *pattern = cb_data;
  27
  28        if (!fnmatch(pattern, refname, 0))
  29                printf("%s\n", refname);
  30
  31        return 0;
  32}
  33
  34static int list_replace_refs(const char *pattern)
  35{
  36        if (pattern == NULL)
  37                pattern = "*";
  38
  39        for_each_replace_ref(show_reference, (void *) pattern);
  40
  41        return 0;
  42}
  43
  44typedef int (*each_replace_name_fn)(const char *name, const char *ref,
  45                                    const unsigned char *sha1);
  46
  47static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
  48{
  49        const char **p, *full_hex;
  50        char ref[PATH_MAX];
  51        int had_error = 0;
  52        unsigned char sha1[20];
  53
  54        for (p = argv; *p; p++) {
  55                if (get_sha1(*p, sha1)) {
  56                        error("Failed to resolve '%s' as a valid ref.", *p);
  57                        had_error = 1;
  58                        continue;
  59                }
  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);
  66                        had_error = 1;
  67                        continue;
  68                }
  69                if (fn(full_hex, ref, sha1))
  70                        had_error = 1;
  71        }
  72        return had_error;
  73}
  74
  75static int delete_replace_ref(const char *name, const char *ref,
  76                              const unsigned char *sha1)
  77{
  78        if (delete_ref(ref, sha1, 0))
  79                return 1;
  80        printf("Deleted replace ref '%s'\n", name);
  81        return 0;
  82}
  83
  84static int replace_object(const char *object_ref, const char *replace_ref,
  85                          int force)
  86{
  87        unsigned char object[20], prev[20], repl[20];
  88        char ref[PATH_MAX];
  89        struct ref_lock *lock;
  90
  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);
  95
  96        if (snprintf(ref, sizeof(ref),
  97                     "refs/replace/%s",
  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);
 102
 103        if (read_ref(ref, prev))
 104                hashclr(prev);
 105        else if (!force)
 106                die("replace ref '%s' already exists", ref);
 107
 108        lock = lock_any_ref_for_update(ref, prev, 0);
 109        if (!lock)
 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);
 113
 114        return 0;
 115}
 116
 117int cmd_replace(int argc, const char **argv, const char *prefix)
 118{
 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")),
 124                OPT_END()
 125        };
 126
 127        argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
 128
 129        if (list && delete)
 130                usage_msg_opt("-l and -d cannot be used together",
 131                              git_replace_usage, options);
 132
 133        if (force && (list || delete))
 134                usage_msg_opt("-f cannot be used with -d or -l",
 135                              git_replace_usage, options);
 136
 137        /* Delete refs */
 138        if (delete) {
 139                if (argc < 1)
 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);
 143        }
 144
 145        /* Replace object */
 146        if (!list && argc) {
 147                if (argc != 2)
 148                        usage_msg_opt("bad number of arguments",
 149                                      git_replace_usage, options);
 150                return replace_object(argv[0], argv[1], force);
 151        }
 152
 153        /* List refs, even if "list" is not set */
 154        if (argc > 1)
 155                usage_msg_opt("only one pattern can be given with -l",
 156                              git_replace_usage, options);
 157        if (force)
 158                usage_msg_opt("-f needs some arguments",
 159                              git_replace_usage, options);
 160
 161        return list_replace_refs(argv[0]);
 162}