log_ref_write() -- do not chomp reflog message at the first LF
[git/haiku.git] / builtin-write-tree.c
blob88f34ba7d6e2426fae5169eb28b12e93e72082eb
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "builtin.h"
7 #include "cache.h"
8 #include "tree.h"
9 #include "cache-tree.h"
11 static const char write_tree_usage[] =
12 "git-write-tree [--missing-ok] [--prefix=<prefix>/]";
14 int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
16 int entries, was_valid, newfd;
18 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
19 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
21 newfd = hold_locked_index(lock_file, 1);
23 entries = read_cache();
24 if (entries < 0)
25 die("git-write-tree: error reading cache");
27 if (!active_cache_tree)
28 active_cache_tree = cache_tree();
30 was_valid = cache_tree_fully_valid(active_cache_tree);
32 if (!was_valid) {
33 if (cache_tree_update(active_cache_tree,
34 active_cache, active_nr,
35 missing_ok, 0) < 0)
36 die("git-write-tree: error building trees");
37 if (0 <= newfd) {
38 if (!write_cache(newfd, active_cache, active_nr)
39 && !close(newfd)) {
40 commit_lock_file(lock_file);
41 newfd = -1;
44 /* Not being able to write is fine -- we are only interested
45 * in updating the cache-tree part, and if the next caller
46 * ends up using the old index with unupdated cache-tree part
47 * it misses the work we did here, but that is just a
48 * performance penalty and not a big deal.
52 if (prefix) {
53 struct cache_tree *subtree =
54 cache_tree_find(active_cache_tree, prefix);
55 if (!subtree)
56 die("git-write-tree: prefix %s not found", prefix);
57 hashcpy(sha1, subtree->sha1);
59 else
60 hashcpy(sha1, active_cache_tree->sha1);
62 if (0 <= newfd)
63 close(newfd);
64 rollback_lock_file(lock_file);
66 return 0;
69 int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
71 int missing_ok = 0, ret;
72 const char *prefix = NULL;
73 unsigned char sha1[20];
75 while (1 < argc) {
76 const char *arg = argv[1];
77 if (!strcmp(arg, "--missing-ok"))
78 missing_ok = 1;
79 else if (!prefixcmp(arg, "--prefix="))
80 prefix = arg + 9;
81 else
82 usage(write_tree_usage);
83 argc--; argv++;
86 if (argc > 2)
87 die("too many options");
89 ret = write_tree(sha1, missing_ok, prefix);
90 printf("%s\n", sha1_to_hex(sha1));
92 return ret;