Code

Merge branch 'vb/sendemail' into next
[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"
10 static int missing_ok = 0;
11 static char *prefix = NULL;
13 static const char write_tree_usage[] =
14 "git-write-tree [--missing-ok] [--prefix=<prefix>/]";
16 static struct cache_file cache_file;
18 int main(int argc, char **argv)
19 {
20         int entries, was_valid, newfd;
22         setup_git_directory();
24         newfd = hold_index_file_for_update(&cache_file, get_index_file());
25         entries = read_cache();
27         while (1 < argc) {
28                 char *arg = argv[1];
29                 if (!strcmp(arg, "--missing-ok"))
30                         missing_ok = 1;
31                 else if (!strncmp(arg, "--prefix=", 9))
32                         prefix = arg + 9;
33                 else
34                         die(write_tree_usage);
35                 argc--; argv++;
36         }
38         if (argc > 2)
39                 die("too many options");
41         if (entries < 0)
42                 die("git-write-tree: error reading cache");
44         if (!active_cache_tree)
45                 active_cache_tree = cache_tree();
47         was_valid = cache_tree_fully_valid(active_cache_tree);
48         if (!was_valid) {
49                 if (cache_tree_update(active_cache_tree,
50                                       active_cache, active_nr,
51                                       missing_ok, 0) < 0)
52                         die("git-write-tree: error building trees");
53                 if (0 <= newfd) {
54                         if (!write_cache(newfd, active_cache, active_nr))
55                                 commit_index_file(&cache_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         }
64         if (prefix) {
65                 struct cache_tree *subtree =
66                         cache_tree_find(active_cache_tree, prefix);
67                 printf("%s\n", sha1_to_hex(subtree->sha1));
68         }
69         else
70                 printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
71         return 0;
72 }