checkout-index.con commit git-blame: Make the output human readable (ea4c7f9)
   1/*
   2 * Check-out files from the "current cache directory"
   3 *
   4 * Copyright (C) 2005 Linus Torvalds
   5 *
   6 * Careful: order of argument flags does matter. For example,
   7 *
   8 *      git-checkout-index -a -f file.c
   9 *
  10 * Will first check out all files listed in the cache (but not
  11 * overwrite any old ones), and then force-checkout "file.c" a
  12 * second time (ie that one _will_ overwrite any old contents
  13 * with the same filename).
  14 *
  15 * Also, just doing "git-checkout-index" does nothing. You probably
  16 * meant "git-checkout-index -a". And if you want to force it, you
  17 * want "git-checkout-index -f -a".
  18 *
  19 * Intuitiveness is not the goal here. Repeatability is. The
  20 * reason for the "no arguments means no work" thing is that
  21 * from scripts you are supposed to be able to do things like
  22 *
  23 *      find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
  24 *
  25 * or:
  26 *
  27 *      find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
  28 *
  29 * which will force all existing *.h files to be replaced with
  30 * their cached copies. If an empty command line implied "all",
  31 * then this would force-refresh everything in the cache, which
  32 * was not the point.
  33 *
  34 * Oh, and the "--" is just a good idea when you know the rest
  35 * will be filenames. Just so that you wouldn't have a filename
  36 * of "-a" causing problems (not possible in the above example,
  37 * but get used to it in scripting!).
  38 */
  39#include "cache.h"
  40#include "strbuf.h"
  41#include "quote.h"
  42
  43static const char *prefix;
  44static int prefix_length;
  45static int checkout_stage; /* default to checkout stage0 */
  46
  47static struct checkout state = {
  48        .base_dir = "",
  49        .base_dir_len = 0,
  50        .force = 0,
  51        .quiet = 0,
  52        .not_new = 0,
  53        .refresh_cache = 0,
  54};
  55
  56static int checkout_file(const char *name)
  57{
  58        int namelen = strlen(name);
  59        int pos = cache_name_pos(name, namelen);
  60        int has_same_name = 0;
  61
  62        if (pos < 0)
  63                pos = -pos - 1;
  64
  65        while (pos < active_nr) {
  66                struct cache_entry *ce = active_cache[pos];
  67                if (ce_namelen(ce) != namelen ||
  68                    memcmp(ce->name, name, namelen))
  69                        break;
  70                has_same_name = 1;
  71                if (checkout_stage == ce_stage(ce))
  72                        return checkout_entry(ce, &state);
  73                pos++;
  74        }
  75
  76        if (!state.quiet) {
  77                fprintf(stderr, "git-checkout-index: %s ", name);
  78                if (!has_same_name)
  79                        fprintf(stderr, "is not in the cache");
  80                else if (checkout_stage)
  81                        fprintf(stderr, "does not exist at stage %d",
  82                                checkout_stage);
  83                else
  84                        fprintf(stderr, "is unmerged");
  85                fputc('\n', stderr);
  86        }
  87        return -1;
  88}
  89
  90static int checkout_all(void)
  91{
  92        int i, errs = 0;
  93
  94        for (i = 0; i < active_nr ; i++) {
  95                struct cache_entry *ce = active_cache[i];
  96                if (ce_stage(ce) != checkout_stage)
  97                        continue;
  98                if (prefix && *prefix &&
  99                    (ce_namelen(ce) <= prefix_length ||
 100                     memcmp(prefix, ce->name, prefix_length)))
 101                        continue;
 102                if (checkout_entry(ce, &state) < 0)
 103                        errs++;
 104        }
 105        if (errs)
 106                /* we have already done our error reporting.
 107                 * exit with the same code as die().
 108                 */
 109                exit(128);
 110        return 0;
 111}
 112
 113static const char checkout_cache_usage[] =
 114"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
 115
 116static struct cache_file cache_file;
 117
 118int main(int argc, char **argv)
 119{
 120        int i;
 121        int newfd = -1;
 122        int all = 0;
 123        int read_from_stdin = 0;
 124        int line_termination = '\n';
 125
 126        prefix = setup_git_directory();
 127        git_config(git_default_config);
 128        prefix_length = prefix ? strlen(prefix) : 0;
 129
 130        if (read_cache() < 0) {
 131                die("invalid cache");
 132        }
 133
 134        for (i = 1; i < argc; i++) {
 135                const char *arg = argv[i];
 136
 137                if (!strcmp(arg, "--")) {
 138                        i++;
 139                        break;
 140                }
 141                if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
 142                        all = 1;
 143                        continue;
 144                }
 145                if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
 146                        state.force = 1;
 147                        continue;
 148                }
 149                if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
 150                        state.quiet = 1;
 151                        continue;
 152                }
 153                if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
 154                        state.not_new = 1;
 155                        continue;
 156                }
 157                if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
 158                        state.refresh_cache = 1;
 159                        if (newfd < 0)
 160                                newfd = hold_index_file_for_update
 161                                        (&cache_file,
 162                                         get_index_file());
 163                        if (newfd < 0)
 164                                die("cannot open index.lock file.");
 165                        continue;
 166                }
 167                if (!strcmp(arg, "-z")) {
 168                        line_termination = 0;
 169                        continue;
 170                }
 171                if (!strcmp(arg, "--stdin")) {
 172                        if (i != argc - 1)
 173                                die("--stdin must be at the end");
 174                        read_from_stdin = 1;
 175                        i++; /* do not consider arg as a file name */
 176                        break;
 177                }
 178                if (!strncmp(arg, "--prefix=", 9)) {
 179                        state.base_dir = arg+9;
 180                        state.base_dir_len = strlen(state.base_dir);
 181                        continue;
 182                }
 183                if (!strncmp(arg, "--stage=", 8)) {
 184                        int ch = arg[8];
 185                        if ('1' <= ch && ch <= '3')
 186                                checkout_stage = arg[8] - '0';
 187                        else
 188                                die("stage should be between 1 and 3");
 189                        continue;
 190                }
 191                if (arg[0] == '-')
 192                        usage(checkout_cache_usage);
 193                break;
 194        }
 195
 196        if (state.base_dir_len) {
 197                /* when --prefix is specified we do not
 198                 * want to update cache.
 199                 */
 200                if (state.refresh_cache) {
 201                        close(newfd); newfd = -1;
 202                        rollback_index_file(&cache_file);
 203                }
 204                state.refresh_cache = 0;
 205        }
 206
 207        /* Check out named files first */
 208        for ( ; i < argc; i++) {
 209                const char *arg = argv[i];
 210
 211                if (all)
 212                        die("git-checkout-index: don't mix '--all' and explicit filenames");
 213                if (read_from_stdin)
 214                        die("git-checkout-index: don't mix '--stdin' and explicit filenames");
 215                checkout_file(prefix_path(prefix, prefix_length, arg));
 216        }
 217
 218        if (read_from_stdin) {
 219                struct strbuf buf;
 220                if (all)
 221                        die("git-checkout-index: don't mix '--all' and '--stdin'");
 222                strbuf_init(&buf);
 223                while (1) {
 224                        char *path_name;
 225                        read_line(&buf, stdin, line_termination);
 226                        if (buf.eof)
 227                                break;
 228                        if (line_termination && buf.buf[0] == '"')
 229                                path_name = unquote_c_style(buf.buf, NULL);
 230                        else
 231                                path_name = buf.buf;
 232                        checkout_file(prefix_path(prefix, prefix_length, path_name));
 233                        if (path_name != buf.buf)
 234                                free(path_name);
 235                }
 236        }
 237
 238        if (all)
 239                checkout_all();
 240
 241        if (0 <= newfd &&
 242            (write_cache(newfd, active_cache, active_nr) ||
 243             commit_index_file(&cache_file)))
 244                die("Unable to write new cachefile");
 245        return 0;
 246}