Add a basic test case for git send-email, and fix some real bugs discovered.
[git.git] / write-tree.c
blob7a4f691d8ab8f6f560685ec6fe71cf2d82e8a018
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 cache_file cache_file;
16 int main(int argc, char **argv)
18 int entries, was_valid, newfd;
20 setup_git_directory();
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);
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_index_file(&cache_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;