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-cache -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-cache" does nothing. You probably 16 * meant "git-checkout-cache -a". And if you want to force it, you 17 * want "git-checkout-cache -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-cache -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 struct checkout state = { 38.base_dir ="", 39.base_dir_len =0, 40.force =0, 41.quiet =0, 42.not_new =0, 43.refresh_cache =0, 44}; 45 46static intcheckout_file(const char*name) 47{ 48int pos =cache_name_pos(name,strlen(name)); 49if(pos <0) { 50if(!state.quiet) { 51 pos = -pos -1; 52fprintf(stderr, 53"git-checkout-cache:%sis%s.\n", 54 name, 55(pos < active_nr && 56!strcmp(active_cache[pos]->name, name)) ? 57"unmerged":"not in the cache"); 58} 59return-1; 60} 61returncheckout_entry(active_cache[pos], &state); 62} 63 64static intcheckout_all(void) 65{ 66int i; 67 68for(i =0; i < active_nr ; i++) { 69struct cache_entry *ce = active_cache[i]; 70if(ce_stage(ce)) 71continue; 72if(checkout_entry(ce, &state) <0) 73return-1; 74} 75return0; 76} 77 78static const char checkout_cache_usage[] = 79"git-checkout-cache [-u] [-q] [-a] [-f] [-n] [--prefix=<string>] [--] <file>..."; 80 81intmain(int argc,char**argv) 82{ 83int i, force_filename =0; 84struct cache_file cache_file; 85int newfd = -1; 86 87if(read_cache() <0) { 88die("invalid cache"); 89} 90 91for(i =1; i < argc; i++) { 92const char*arg = argv[i]; 93if(!force_filename) { 94if(!strcmp(arg,"-a")) { 95checkout_all(); 96continue; 97} 98if(!strcmp(arg,"--")) { 99 force_filename =1; 100continue; 101} 102if(!strcmp(arg,"-f")) { 103 state.force =1; 104continue; 105} 106if(!strcmp(arg,"-q")) { 107 state.quiet =1; 108continue; 109} 110if(!strcmp(arg,"-n")) { 111 state.not_new =1; 112continue; 113} 114if(!strcmp(arg,"-u")) { 115 state.refresh_cache =1; 116if(newfd <0) 117 newfd = hold_index_file_for_update 118(&cache_file, 119get_index_file()); 120if(newfd <0) 121die("cannot open index.lock file."); 122continue; 123} 124if(!memcmp(arg,"--prefix=",9)) { 125 state.base_dir = arg+9; 126 state.base_dir_len =strlen(state.base_dir); 127continue; 128} 129if(arg[0] =='-') 130usage(checkout_cache_usage); 131} 132if(state.base_dir_len) { 133/* when --prefix is specified we do not 134 * want to update cache. 135 */ 136if(state.refresh_cache) { 137close(newfd); newfd = -1; 138rollback_index_file(&cache_file); 139} 140 state.refresh_cache =0; 141} 142checkout_file(arg); 143} 144 145if(0<= newfd && 146(write_cache(newfd, active_cache, active_nr) || 147commit_index_file(&cache_file))) 148die("Unable to write new cachefile"); 149return0; 150}