1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include"cache.h" 7#include"tree.h" 8#include"cache-tree.h" 9 10static int missing_ok =0; 11static char*prefix = NULL; 12 13static const char write_tree_usage[] = 14"git-write-tree [--missing-ok] [--prefix=<prefix>/]"; 15 16static struct lock_file lock_file; 17 18intmain(int argc,char**argv) 19{ 20int entries, was_valid, newfd; 21 22setup_git_directory(); 23 24 newfd =hold_lock_file_for_update(&lock_file,get_index_file()); 25 entries =read_cache(); 26 27while(1< argc) { 28char*arg = argv[1]; 29if(!strcmp(arg,"--missing-ok")) 30 missing_ok =1; 31else if(!strncmp(arg,"--prefix=",9)) 32 prefix = arg +9; 33else 34die(write_tree_usage); 35 argc--; argv++; 36} 37 38if(argc >2) 39die("too many options"); 40 41if(entries <0) 42die("git-write-tree: error reading cache"); 43 44if(!active_cache_tree) 45 active_cache_tree =cache_tree(); 46 47 was_valid =cache_tree_fully_valid(active_cache_tree); 48if(!was_valid) { 49if(cache_tree_update(active_cache_tree, 50 active_cache, active_nr, 51 missing_ok,0) <0) 52die("git-write-tree: error building trees"); 53if(0<= newfd) { 54if(!write_cache(newfd, active_cache, active_nr)) 55commit_lock_file(&lock_file); 56} 57/* Not being able to write is fine -- we are only interested 58 * in updating the cache-tree part, and if the next caller 59 * ends up using the old index with unupdated cache-tree part 60 * it misses the work we did here, but that is just a 61 * performance penalty and not a big deal. 62 */ 63} 64if(prefix) { 65struct cache_tree *subtree = 66cache_tree_find(active_cache_tree, prefix); 67printf("%s\n",sha1_to_hex(subtree->sha1)); 68} 69else 70printf("%s\n",sha1_to_hex(active_cache_tree->sha1)); 71return0; 72}