1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6 7#include"cache.h" 8#include"object.h" 9#include"tree.h" 10#include"tree-walk.h" 11#include"cache-tree.h" 12#include"unpack-trees.h" 13#include"dir.h" 14#include"builtin.h" 15 16static int nr_trees; 17static struct tree *trees[MAX_UNPACK_TREES]; 18 19static intlist_tree(unsigned char*sha1) 20{ 21struct tree *tree; 22 23if(nr_trees >= MAX_UNPACK_TREES) 24die("I cannot read more than%dtrees", MAX_UNPACK_TREES); 25 tree =parse_tree_indirect(sha1); 26if(!tree) 27return-1; 28 trees[nr_trees++] = tree; 29return0; 30} 31 32static const char read_tree_usage[] ="git read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])"; 33 34static struct lock_file lock_file; 35 36intcmd_read_tree(int argc,const char**argv,const char*unused_prefix) 37{ 38int i, newfd, stage =0; 39unsigned char sha1[20]; 40struct tree_desc t[MAX_UNPACK_TREES]; 41struct unpack_trees_options opts; 42 43memset(&opts,0,sizeof(opts)); 44 opts.head_idx = -1; 45 opts.src_index = &the_index; 46 opts.dst_index = &the_index; 47 48git_config(git_default_config, NULL); 49 50 newfd =hold_locked_index(&lock_file,1); 51 52for(i =1; i < argc; i++) { 53const char*arg = argv[i]; 54 55/* "-u" means "update", meaning that a merge will update 56 * the working tree. 57 */ 58if(!strcmp(arg,"-u")) { 59 opts.update =1; 60continue; 61} 62 63if(!strcmp(arg,"-v")) { 64 opts.verbose_update =1; 65continue; 66} 67 68/* "-i" means "index only", meaning that a merge will 69 * not even look at the working tree. 70 */ 71if(!strcmp(arg,"-i")) { 72 opts.index_only =1; 73continue; 74} 75 76if(!prefixcmp(arg,"--index-output=")) { 77set_alternate_index_output(arg +15); 78continue; 79} 80 81/* "--prefix=<subdirectory>/" means keep the current index 82 * entries and put the entries from the tree under the 83 * given subdirectory. 84 */ 85if(!prefixcmp(arg,"--prefix=")) { 86if(stage || opts.merge || opts.prefix) 87usage(read_tree_usage); 88 opts.prefix = arg +9; 89 opts.merge =1; 90 stage =1; 91if(read_cache_unmerged()) 92die("you need to resolve your current index first"); 93continue; 94} 95 96/* This differs from "-m" in that we'll silently ignore 97 * unmerged entries and overwrite working tree files that 98 * correspond to them. 99 */ 100if(!strcmp(arg,"--reset")) { 101if(stage || opts.merge || opts.prefix) 102usage(read_tree_usage); 103 opts.reset =1; 104 opts.merge =1; 105 stage =1; 106read_cache_unmerged(); 107continue; 108} 109 110if(!strcmp(arg,"--trivial")) { 111 opts.trivial_merges_only =1; 112continue; 113} 114 115if(!strcmp(arg,"--aggressive")) { 116 opts.aggressive =1; 117continue; 118} 119 120/* "-m" stands for "merge", meaning we start in stage 1 */ 121if(!strcmp(arg,"-m")) { 122if(stage || opts.merge || opts.prefix) 123usage(read_tree_usage); 124if(read_cache_unmerged()) 125die("you need to resolve your current index first"); 126 stage =1; 127 opts.merge =1; 128continue; 129} 130 131if(!prefixcmp(arg,"--exclude-per-directory=")) { 132struct dir_struct *dir; 133 134if(opts.dir) 135die("more than one --exclude-per-directory are given."); 136 137 dir =xcalloc(1,sizeof(*opts.dir)); 138 dir->show_ignored =1; 139 dir->exclude_per_dir = arg +24; 140 opts.dir = dir; 141/* We do not need to nor want to do read-directory 142 * here; we are merely interested in reusing the 143 * per directory ignore stack mechanism. 144 */ 145continue; 146} 147 148/* using -u and -i at the same time makes no sense */ 149if(1< opts.index_only + opts.update) 150usage(read_tree_usage); 151 152if(get_sha1(arg, sha1)) 153die("Not a valid object name%s", arg); 154if(list_tree(sha1) <0) 155die("failed to unpack tree object%s", arg); 156 stage++; 157} 158if((opts.update||opts.index_only) && !opts.merge) 159usage(read_tree_usage); 160if((opts.dir && !opts.update)) 161die("--exclude-per-directory is meaningless unless -u"); 162if(opts.merge && !opts.index_only) 163setup_work_tree(); 164 165if(opts.merge) { 166if(stage <2) 167die("just how do you expect me to merge%dtrees?", stage-1); 168switch(stage -1) { 169case1: 170 opts.fn = opts.prefix ? bind_merge : oneway_merge; 171break; 172case2: 173 opts.fn = twoway_merge; 174 opts.initial_checkout =is_cache_unborn(); 175break; 176case3: 177default: 178 opts.fn = threeway_merge; 179break; 180} 181 182if(stage -1>=3) 183 opts.head_idx = stage -2; 184else 185 opts.head_idx =1; 186} 187 188cache_tree_free(&active_cache_tree); 189for(i =0; i < nr_trees; i++) { 190struct tree *tree = trees[i]; 191parse_tree(tree); 192init_tree_desc(t+i, tree->buffer, tree->size); 193} 194if(unpack_trees(nr_trees, t, &opts)) 195return128; 196 197/* 198 * When reading only one tree (either the most basic form, 199 * "-m ent" or "--reset ent" form), we can obtain a fully 200 * valid cache-tree because the index must match exactly 201 * what came from the tree. 202 * 203 * The same holds true if we are switching between two trees 204 * using read-tree -m A B. The index must match B after that. 205 */ 206if(nr_trees ==1&& !opts.prefix) 207prime_cache_tree(&active_cache_tree, trees[0]); 208else if(nr_trees ==2&& opts.merge) 209prime_cache_tree(&active_cache_tree, trees[1]); 210 211if(write_cache(newfd, active_cache, active_nr) || 212commit_locked_index(&lock_file)) 213die("unable to write new index file"); 214return0; 215}