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 * which will force all existing *.h files to be replaced with 26 * their cached copies. If an empty command line implied "all", 27 * then this would force-refresh everything in the cache, which 28 * was not the point. 29 * 30 * Oh, and the "--" is just a good idea when you know the rest 31 * will be filenames. Just so that you wouldn't have a filename 32 * of "-a" causing problems (not possible in the above example, 33 * but get used to it in scripting!). 34 */ 35#include"cache.h" 36 37static const char*prefix; 38static int prefix_length; 39static int checkout_stage;/* default to checkout stage0 */ 40 41static struct checkout state = { 42.base_dir ="", 43.base_dir_len =0, 44.force =0, 45.quiet =0, 46.not_new =0, 47.refresh_cache =0, 48}; 49 50static intcheckout_file(const char*name) 51{ 52int namelen =strlen(name); 53int pos =cache_name_pos(name, namelen); 54int has_same_name =0; 55 56if(pos <0) 57 pos = -pos -1; 58 59while(pos < active_nr) { 60struct cache_entry *ce = active_cache[pos]; 61if(ce_namelen(ce) != namelen || 62memcmp(ce->name, name, namelen)) 63break; 64 has_same_name =1; 65if(checkout_stage ==ce_stage(ce)) 66returncheckout_entry(ce, &state); 67 pos++; 68} 69 70if(!state.quiet) { 71fprintf(stderr,"git-checkout-index:%s", name); 72if(!has_same_name) 73fprintf(stderr,"is not in the cache"); 74else if(checkout_stage) 75fprintf(stderr,"does not exist at stage%d", 76 checkout_stage); 77else 78fprintf(stderr,"is unmerged"); 79fputc('\n', stderr); 80} 81return-1; 82} 83 84static intcheckout_all(void) 85{ 86int i, errs =0; 87 88for(i =0; i < active_nr ; i++) { 89struct cache_entry *ce = active_cache[i]; 90if(ce_stage(ce) != checkout_stage) 91continue; 92if(prefix && *prefix && 93(ce_namelen(ce) <= prefix_length || 94memcmp(prefix, ce->name, prefix_length))) 95continue; 96if(checkout_entry(ce, &state) <0) 97 errs++; 98} 99if(errs) 100/* we have already done our error reporting. 101 * exit with the same code as die(). 102 */ 103exit(128); 104return0; 105} 106 107static const char checkout_cache_usage[] = 108"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>..."; 109 110static struct cache_file cache_file; 111 112intmain(int argc,char**argv) 113{ 114int i; 115int newfd = -1; 116int all =0; 117 118 prefix =setup_git_directory(); 119git_config(git_default_config); 120 prefix_length = prefix ?strlen(prefix) :0; 121 122if(read_cache() <0) { 123die("invalid cache"); 124} 125 126for(i =1; i < argc; i++) { 127const char*arg = argv[i]; 128 129if(!strcmp(arg,"--")) { 130 i++; 131break; 132} 133if(!strcmp(arg,"-a") || !strcmp(arg,"--all")) { 134 all =1; 135continue; 136} 137if(!strcmp(arg,"-f") || !strcmp(arg,"--force")) { 138 state.force =1; 139continue; 140} 141if(!strcmp(arg,"-q") || !strcmp(arg,"--quiet")) { 142 state.quiet =1; 143continue; 144} 145if(!strcmp(arg,"-n") || !strcmp(arg,"--no-create")) { 146 state.not_new =1; 147continue; 148} 149if(!strcmp(arg,"-u") || !strcmp(arg,"--index")) { 150 state.refresh_cache =1; 151if(newfd <0) 152 newfd = hold_index_file_for_update 153(&cache_file, 154get_index_file()); 155if(newfd <0) 156die("cannot open index.lock file."); 157continue; 158} 159if(!strncmp(arg,"--prefix=",9)) { 160 state.base_dir = arg+9; 161 state.base_dir_len =strlen(state.base_dir); 162continue; 163} 164if(!strncmp(arg,"--stage=",8)) { 165int ch = arg[8]; 166if('1'<= ch && ch <='3') 167 checkout_stage = arg[8] -'0'; 168else 169die("stage should be between 1 and 3"); 170continue; 171} 172if(arg[0] =='-') 173usage(checkout_cache_usage); 174break; 175} 176 177if(state.base_dir_len) { 178/* when --prefix is specified we do not 179 * want to update cache. 180 */ 181if(state.refresh_cache) { 182close(newfd); newfd = -1; 183rollback_index_file(&cache_file); 184} 185 state.refresh_cache =0; 186} 187 188/* Check out named files first */ 189for( ; i < argc; i++) { 190const char*arg = argv[i]; 191 192if(all) 193die("git-checkout-index: don't mix '--all' and explicit filenames"); 194checkout_file(prefix_path(prefix, prefix_length, arg)); 195} 196 197if(all) 198checkout_all(); 199 200if(0<= newfd && 201(write_cache(newfd, active_cache, active_nr) || 202commit_index_file(&cache_file))) 203die("Unable to write new cachefile"); 204return0; 205}