a5069921a0f01e872c5e7c62f836cd0b4441e551
[git/git.git] / write-tree.c
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
10 static int missing_ok = 0;
11
12 static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
13
14 static struct cache_file cache_file;
15
16 int main(int argc, char **argv)
17 {
18 int entries, was_valid, newfd;
19
20 setup_git_directory();
21
22 newfd = hold_index_file_for_update(&cache_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);
29 }
30
31 if (argc > 2)
32 die("too many options");
33
34 if (entries < 0)
35 die("git-write-tree: error reading cache");
36
37 if (!active_cache_tree)
38 active_cache_tree = cache_tree();
39
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)
45 die("git-write-tree: error building trees");
46 if (0 <= newfd) {
47 if (!write_cache(newfd, active_cache, active_nr))
48 commit_index_file(&cache_file);
49 }
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.
55 */
56 }
57 printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
58 return 0;
59 }