builtin / replace.con commit [fr] update french translation 2210/2210 (561580e)
   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 [--format=<format>] [-l [<pattern>]]"),
  20        NULL
  21};
  22
  23enum replace_format {
  24      REPLACE_FORMAT_SHORT,
  25      REPLACE_FORMAT_MEDIUM,
  26      REPLACE_FORMAT_LONG
  27};
  28
  29struct show_data {
  30        const char *pattern;
  31        enum replace_format format;
  32};
  33
  34static int show_reference(const char *refname, const unsigned char *sha1,
  35                          int flag, void *cb_data)
  36{
  37        struct show_data *data = cb_data;
  38
  39        if (!fnmatch(data->pattern, refname, 0)) {
  40                if (data->format == REPLACE_FORMAT_SHORT)
  41                        printf("%s\n", refname);
  42                else if (data->format == REPLACE_FORMAT_MEDIUM)
  43                        printf("%s -> %s\n", refname, sha1_to_hex(sha1));
  44                else { /* data->format == REPLACE_FORMAT_LONG */
  45                        unsigned char object[20];
  46                        enum object_type obj_type, repl_type;
  47
  48                        if (get_sha1(refname, object))
  49                                return error("Failed to resolve '%s' as a valid ref.", refname);
  50
  51                        obj_type = sha1_object_info(object, NULL);
  52                        repl_type = sha1_object_info(sha1, NULL);
  53
  54                        printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
  55                               sha1_to_hex(sha1), typename(repl_type));
  56                }
  57        }
  58
  59        return 0;
  60}
  61
  62static int list_replace_refs(const char *pattern, const char *format)
  63{
  64        struct show_data data;
  65
  66        if (pattern == NULL)
  67                pattern = "*";
  68        data.pattern = pattern;
  69
  70        if (format == NULL || *format == '\0' || !strcmp(format, "short"))
  71                data.format = REPLACE_FORMAT_SHORT;
  72        else if (!strcmp(format, "medium"))
  73                data.format = REPLACE_FORMAT_MEDIUM;
  74        else if (!strcmp(format, "long"))
  75                data.format = REPLACE_FORMAT_LONG;
  76        else
  77                die("invalid replace format '%s'\n"
  78                    "valid formats are 'short', 'medium' and 'long'\n",
  79                    format);
  80
  81        for_each_replace_ref(show_reference, (void *) &data);
  82
  83        return 0;
  84}
  85
  86typedef int (*each_replace_name_fn)(const char *name, const char *ref,
  87                                    const unsigned char *sha1);
  88
  89static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
  90{
  91        const char **p, *full_hex;
  92        char ref[PATH_MAX];
  93        int had_error = 0;
  94        unsigned char sha1[20];
  95
  96        for (p = argv; *p; p++) {
  97                if (get_sha1(*p, sha1)) {
  98                        error("Failed to resolve '%s' as a valid ref.", *p);
  99                        had_error = 1;
 100                        continue;
 101                }
 102                full_hex = sha1_to_hex(sha1);
 103                snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
 104                /* read_ref() may reuse the buffer */
 105                full_hex = ref + strlen("refs/replace/");
 106                if (read_ref(ref, sha1)) {
 107                        error("replace ref '%s' not found.", full_hex);
 108                        had_error = 1;
 109                        continue;
 110                }
 111                if (fn(full_hex, ref, sha1))
 112                        had_error = 1;
 113        }
 114        return had_error;
 115}
 116
 117static int delete_replace_ref(const char *name, const char *ref,
 118                              const unsigned char *sha1)
 119{
 120        if (delete_ref(ref, sha1, 0))
 121                return 1;
 122        printf("Deleted replace ref '%s'\n", name);
 123        return 0;
 124}
 125
 126static int replace_object(const char *object_ref, const char *replace_ref,
 127                          int force)
 128{
 129        unsigned char object[20], prev[20], repl[20];
 130        enum object_type obj_type, repl_type;
 131        char ref[PATH_MAX];
 132        struct ref_lock *lock;
 133
 134        if (get_sha1(object_ref, object))
 135                die("Failed to resolve '%s' as a valid ref.", object_ref);
 136        if (get_sha1(replace_ref, repl))
 137                die("Failed to resolve '%s' as a valid ref.", replace_ref);
 138
 139        if (snprintf(ref, sizeof(ref),
 140                     "refs/replace/%s",
 141                     sha1_to_hex(object)) > sizeof(ref) - 1)
 142                die("replace ref name too long: %.*s...", 50, ref);
 143        if (check_refname_format(ref, 0))
 144                die("'%s' is not a valid ref name.", ref);
 145
 146        obj_type = sha1_object_info(object, NULL);
 147        repl_type = sha1_object_info(repl, NULL);
 148        if (!force && obj_type != repl_type)
 149                die("Objects must be of the same type.\n"
 150                    "'%s' points to a replaced object of type '%s'\n"
 151                    "while '%s' points to a replacement object of type '%s'.",
 152                    object_ref, typename(obj_type),
 153                    replace_ref, typename(repl_type));
 154
 155        if (read_ref(ref, prev))
 156                hashclr(prev);
 157        else if (!force)
 158                die("replace ref '%s' already exists", ref);
 159
 160        lock = lock_any_ref_for_update(ref, prev, 0, NULL);
 161        if (!lock)
 162                die("%s: cannot lock the ref", ref);
 163        if (write_ref_sha1(lock, repl, NULL) < 0)
 164                die("%s: cannot update the ref", ref);
 165
 166        return 0;
 167}
 168
 169int cmd_replace(int argc, const char **argv, const char *prefix)
 170{
 171        int list = 0, delete = 0, force = 0;
 172        const char *format = NULL;
 173        struct option options[] = {
 174                OPT_BOOL('l', "list", &list, N_("list replace refs")),
 175                OPT_BOOL('d', "delete", &delete, N_("delete replace refs")),
 176                OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
 177                OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
 178                OPT_END()
 179        };
 180
 181        read_replace_refs = 0;
 182
 183        argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
 184
 185        if (list && delete)
 186                usage_msg_opt("-l and -d cannot be used together",
 187                              git_replace_usage, options);
 188
 189        if (format && delete)
 190                usage_msg_opt("--format and -d cannot be used together",
 191                              git_replace_usage, options);
 192
 193        if (force && (list || delete))
 194                usage_msg_opt("-f cannot be used with -d or -l",
 195                              git_replace_usage, options);
 196
 197        /* Delete refs */
 198        if (delete) {
 199                if (argc < 1)
 200                        usage_msg_opt("-d needs at least one argument",
 201                                      git_replace_usage, options);
 202                return for_each_replace_name(argv, delete_replace_ref);
 203        }
 204
 205        /* Replace object */
 206        if (!list && argc) {
 207                if (argc != 2)
 208                        usage_msg_opt("bad number of arguments",
 209                                      git_replace_usage, options);
 210                if (format)
 211                        usage_msg_opt("--format cannot be used when not listing",
 212                                      git_replace_usage, options);
 213                return replace_object(argv[0], argv[1], force);
 214        }
 215
 216        /* List refs, even if "list" is not set */
 217        if (argc > 1)
 218                usage_msg_opt("only one pattern can be given with -l",
 219                              git_replace_usage, options);
 220        if (force)
 221                usage_msg_opt("-f needs some arguments",
 222                              git_replace_usage, options);
 223
 224        return list_replace_refs(argv[0], format);
 225}