git-svn: support -C<num> passing to git-diff-tree
[git.git] / write-tree.c
blobd6a605893dcbb4d8d65979309cf9ce1199aa8279
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "tree.h"
8 #include "cache-tree.h"
10 static int missing_ok = 0;
12 static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
14 static struct lock_file lock_file;
16 int main(int argc, char **argv)
18 int entries, was_valid, newfd;
20 setup_git_directory();
22 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
23 entries = read_cache();
24 if (argc == 2) {
25 if (!strcmp(argv[1], "--missing-ok"))
26 missing_ok = 1;
27 else
28 die(write_tree_usage);
31 if (argc > 2)
32 die("too many options");
34 if (entries < 0)
35 die("git-write-tree: error reading cache");
37 if (!active_cache_tree)
38 active_cache_tree = cache_tree();
40 was_valid = cache_tree_fully_valid(active_cache_tree);
41 if (!was_valid) {
42 if (cache_tree_update(active_cache_tree,
43 active_cache, active_nr,
44 missing_ok, 0) < 0)
45 die("git-write-tree: error building trees");
46 if (0 <= newfd) {
47 if (!write_cache(newfd, active_cache, active_nr))
48 commit_lock_file(&lock_file);
50 /* Not being able to write is fine -- we are only interested
51 * in updating the cache-tree part, and if the next caller
52 * ends up using the old index with unupdated cache-tree part
53 * it misses the work we did here, but that is just a
54 * performance penalty and not a big deal.
57 printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
58 return 0;