1/*2* Check-out files from the "current cache directory"3*4* Copyright (C) 2005 Linus Torvalds5*6* Careful: order of argument flags does matter. For example,7*8* git-checkout-index -a -f file.c9*10* Will first check out all files listed in the cache (but not11* overwrite any old ones), and then force-checkout "file.c" a12* second time (ie that one _will_ overwrite any old contents13* with the same filename).14*15* Also, just doing "git-checkout-index" does nothing. You probably16* meant "git-checkout-index -a". And if you want to force it, you17* want "git-checkout-index -f -a".18*19* Intuitiveness is not the goal here. Repeatability is. The20* reason for the "no arguments means no work" thing is that21* from scripts you are supposed to be able to do things like22*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 --stdin28*29* which will force all existing *.h files to be replaced with30* their cached copies. If an empty command line implied "all",31* then this would force-refresh everything in the cache, which32* was not the point.33*34* Oh, and the "--" is just a good idea when you know the rest35* will be filenames. Just so that you wouldn't have a filename36* 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"4243static const char *prefix;44static int prefix_length;45static int checkout_stage; /* default to checkout stage0 */4647static 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};5556static int checkout_file(const char *name)57{58int namelen = strlen(name);59int pos = cache_name_pos(name, namelen);60int has_same_name = 0;6162if (pos < 0)63pos = -pos - 1;6465while (pos < active_nr) {66struct cache_entry *ce = active_cache[pos];67if (ce_namelen(ce) != namelen ||68memcmp(ce->name, name, namelen))69break;70has_same_name = 1;71if (checkout_stage == ce_stage(ce))72return checkout_entry(ce, &state);73pos++;74}7576if (!state.quiet) {77fprintf(stderr, "git-checkout-index: %s ", name);78if (!has_same_name)79fprintf(stderr, "is not in the cache");80else if (checkout_stage)81fprintf(stderr, "does not exist at stage %d",82checkout_stage);83else84fprintf(stderr, "is unmerged");85fputc('\n', stderr);86}87return -1;88}8990static int checkout_all(void)91{92int i, errs = 0;9394for (i = 0; i < active_nr ; i++) {95struct cache_entry *ce = active_cache[i];96if (ce_stage(ce) != checkout_stage)97continue;98if (prefix && *prefix &&99(ce_namelen(ce) <= prefix_length ||100memcmp(prefix, ce->name, prefix_length)))101continue;102if (checkout_entry(ce, &state) < 0)103errs++;104}105if (errs)106/* we have already done our error reporting.107* exit with the same code as die().108*/109exit(128);110return 0;111}112113static const char checkout_cache_usage[] =114"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";115116static struct cache_file cache_file;117118int main(int argc, char **argv)119{120int i;121int newfd = -1;122int all = 0;123int read_from_stdin = 0;124int line_termination = '\n';125126prefix = setup_git_directory();127git_config(git_default_config);128prefix_length = prefix ? strlen(prefix) : 0;129130if (read_cache() < 0) {131die("invalid cache");132}133134for (i = 1; i < argc; i++) {135const char *arg = argv[i];136137if (!strcmp(arg, "--")) {138i++;139break;140}141if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {142all = 1;143continue;144}145if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {146state.force = 1;147continue;148}149if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {150state.quiet = 1;151continue;152}153if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {154state.not_new = 1;155continue;156}157if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {158state.refresh_cache = 1;159if (newfd < 0)160newfd = hold_index_file_for_update161(&cache_file,162get_index_file());163if (newfd < 0)164die("cannot open index.lock file.");165continue;166}167if (!strcmp(arg, "-z")) {168line_termination = 0;169continue;170}171if (!strcmp(arg, "--stdin")) {172if (i != argc - 1)173die("--stdin must be at the end");174read_from_stdin = 1;175i++; /* do not consider arg as a file name */176break;177}178if (!strncmp(arg, "--prefix=", 9)) {179state.base_dir = arg+9;180state.base_dir_len = strlen(state.base_dir);181continue;182}183if (!strncmp(arg, "--stage=", 8)) {184int ch = arg[8];185if ('1' <= ch && ch <= '3')186checkout_stage = arg[8] - '0';187else188die("stage should be between 1 and 3");189continue;190}191if (arg[0] == '-')192usage(checkout_cache_usage);193break;194}195196if (state.base_dir_len) {197/* when --prefix is specified we do not198* want to update cache.199*/200if (state.refresh_cache) {201close(newfd); newfd = -1;202rollback_index_file(&cache_file);203}204state.refresh_cache = 0;205}206207/* Check out named files first */208for ( ; i < argc; i++) {209const char *arg = argv[i];210211if (all)212die("git-checkout-index: don't mix '--all' and explicit filenames");213if (read_from_stdin)214die("git-checkout-index: don't mix '--stdin' and explicit filenames");215checkout_file(prefix_path(prefix, prefix_length, arg));216}217218if (read_from_stdin) {219struct strbuf buf;220if (all)221die("git-checkout-index: don't mix '--all' and '--stdin'");222strbuf_init(&buf);223while (1) {224char *path_name;225read_line(&buf, stdin, line_termination);226if (buf.eof)227break;228if (line_termination && buf.buf[0] == '"')229path_name = unquote_c_style(buf.buf, NULL);230else231path_name = buf.buf;232checkout_file(prefix_path(prefix, prefix_length, path_name));233if (path_name != buf.buf)234free(path_name);235}236}237238if (all)239checkout_all();240241if (0 <= newfd &&242(write_cache(newfd, active_cache, active_nr) ||243commit_index_file(&cache_file)))244die("Unable to write new cachefile");245return 0;246}