cache_tree_update: give an option to update cache-tree only.
[git/git.git] / write-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
8e440259 7#include "tree.h"
a52139b4 8#include "cache-tree.h"
e83c5163 9
a52139b4 10static int missing_ok = 0;
d6d3f9d0 11
75a46f6b
JH
12static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
13
bad68ec9
JH
14static struct cache_file cache_file;
15
d6d3f9d0
LT
16int main(int argc, char **argv)
17{
bad68ec9 18 int entries, was_valid, newfd;
a52139b4 19
53228a5f
JH
20 setup_git_directory();
21
bad68ec9
JH
22 newfd = hold_index_file_for_update(&cache_file, get_index_file());
23 entries = read_cache();
0b124bb4 24 if (argc == 2) {
9c1fa70a
BL
25 if (!strcmp(argv[1], "--missing-ok"))
26 missing_ok = 1;
27 else
75a46f6b 28 die(write_tree_usage);
9c1fa70a
BL
29 }
30
0b124bb4 31 if (argc > 2)
9c1fa70a 32 die("too many options");
d6d3f9d0 33
c899350e 34 if (entries < 0)
667bb59b 35 die("git-write-tree: error reading cache");
c347ea5d 36
bad68ec9
JH
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,
2956dd3b 44 missing_ok, 0) < 0)
bad68ec9
JH
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 }
a52139b4 57 printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
e83c5163
LT
58 return 0;
59}