Merge branch 'yl/build'
[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;
6bd20358 11static char *prefix = NULL;
d6d3f9d0 12
6bd20358
JH
13static const char write_tree_usage[] =
14"git-write-tree [--missing-ok] [--prefix=<prefix>/]";
75a46f6b 15
021b6e45 16static struct lock_file lock_file;
bad68ec9 17
d6d3f9d0
LT
18int main(int argc, char **argv)
19{
bad68ec9 20 int entries, was_valid, newfd;
a52139b4 21
53228a5f
JH
22 setup_git_directory();
23
021b6e45 24 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
bad68ec9 25 entries = read_cache();
6bd20358
JH
26
27 while (1 < argc) {
28 char *arg = argv[1];
29 if (!strcmp(arg, "--missing-ok"))
9c1fa70a 30 missing_ok = 1;
6bd20358
JH
31 else if (!strncmp(arg, "--prefix=", 9))
32 prefix = arg + 9;
9c1fa70a 33 else
75a46f6b 34 die(write_tree_usage);
6bd20358 35 argc--; argv++;
9c1fa70a 36 }
6bd20358 37
0b124bb4 38 if (argc > 2)
9c1fa70a 39 die("too many options");
d6d3f9d0 40
c899350e 41 if (entries < 0)
667bb59b 42 die("git-write-tree: error reading cache");
c347ea5d 43
bad68ec9
JH
44 if (!active_cache_tree)
45 active_cache_tree = cache_tree();
46
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,
2956dd3b 51 missing_ok, 0) < 0)
bad68ec9
JH
52 die("git-write-tree: error building trees");
53 if (0 <= newfd) {
54 if (!write_cache(newfd, active_cache, active_nr))
021b6e45 55 commit_lock_file(&lock_file);
bad68ec9
JH
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 }
6bd20358
JH
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));
e83c5163
LT
71 return 0;
72}