]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-write-tree.c
Make git-write-tree a builtin
[thirdparty/git.git] / builtin-write-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
8ed05fb5 6#include "builtin.h"
e83c5163 7#include "cache.h"
8e440259 8#include "tree.h"
a52139b4 9#include "cache-tree.h"
e83c5163 10
6bd20358
JH
11static const char write_tree_usage[] =
12"git-write-tree [--missing-ok] [--prefix=<prefix>/]";
75a46f6b 13
8ed05fb5 14int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
d6d3f9d0 15{
bad68ec9 16 int entries, was_valid, newfd;
a52139b4 17
8ed05fb5
LS
18 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
19 struct lock_file *lock_file = xmalloc(sizeof(struct lock_file));
53228a5f 20
8ed05fb5 21 newfd = hold_lock_file_for_update(lock_file, get_index_file());
d6d3f9d0 22
8ed05fb5 23 entries = read_cache();
c899350e 24 if (entries < 0)
667bb59b 25 die("git-write-tree: error reading cache");
c347ea5d 26
bad68ec9
JH
27 if (!active_cache_tree)
28 active_cache_tree = cache_tree();
29
30 was_valid = cache_tree_fully_valid(active_cache_tree);
8ed05fb5 31
bad68ec9
JH
32 if (!was_valid) {
33 if (cache_tree_update(active_cache_tree,
34 active_cache, active_nr,
2956dd3b 35 missing_ok, 0) < 0)
bad68ec9
JH
36 die("git-write-tree: error building trees");
37 if (0 <= newfd) {
38 if (!write_cache(newfd, active_cache, active_nr))
8ed05fb5 39 commit_lock_file(lock_file);
bad68ec9
JH
40 }
41 /* Not being able to write is fine -- we are only interested
42 * in updating the cache-tree part, and if the next caller
43 * ends up using the old index with unupdated cache-tree part
44 * it misses the work we did here, but that is just a
45 * performance penalty and not a big deal.
46 */
47 }
8ed05fb5 48
6bd20358
JH
49 if (prefix) {
50 struct cache_tree *subtree =
51 cache_tree_find(active_cache_tree, prefix);
8ed05fb5 52 memcpy(sha1, subtree->sha1, 20);
6bd20358
JH
53 }
54 else
8ed05fb5
LS
55 memcpy(sha1, active_cache_tree->sha1, 20);
56
57 rollback_lock_file(lock_file);
58
e83c5163
LT
59 return 0;
60}
8ed05fb5
LS
61
62int cmd_write_tree(int argc, const char **argv, char **envp)
63{
64 int missing_ok = 0, ret;
65 const char *prefix = NULL;
66 unsigned char sha1[20];
67
68 setup_git_directory();
69
70 while (1 < argc) {
71 const char *arg = argv[1];
72 if (!strcmp(arg, "--missing-ok"))
73 missing_ok = 1;
74 else if (!strncmp(arg, "--prefix=", 9))
75 prefix = arg + 9;
76 else
77 die(write_tree_usage);
78 argc--; argv++;
79 }
80
81 if (argc > 2)
82 die("too many options");
83
84 ret = write_tree(sha1, missing_ok, prefix);
85 printf("%s\n", sha1_to_hex(sha1));
86
87 return ret;
88}