Merge branch 'bc/hash-algo'
[git] / builtin / write-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "builtin.h"
7 #include "cache.h"
8 #include "config.h"
9 #include "tree.h"
10 #include "cache-tree.h"
11 #include "parse-options.h"
12
13 static const char * const write_tree_usage[] = {
14         N_("git write-tree [--missing-ok] [--prefix=<prefix>/]"),
15         NULL
16 };
17
18 int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
19 {
20         int flags = 0, ret;
21         const char *prefix = NULL;
22         unsigned char sha1[20];
23         const char *me = "git-write-tree";
24         struct option write_tree_options[] = {
25                 OPT_BIT(0, "missing-ok", &flags, N_("allow missing objects"),
26                         WRITE_TREE_MISSING_OK),
27                 { OPTION_STRING, 0, "prefix", &prefix, N_("<prefix>/"),
28                   N_("write tree object for a subdirectory <prefix>") ,
29                   PARSE_OPT_LITERAL_ARGHELP },
30                 { OPTION_BIT, 0, "ignore-cache-tree", &flags, NULL,
31                   N_("only useful for debugging"),
32                   PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, NULL,
33                   WRITE_TREE_IGNORE_CACHE_TREE },
34                 OPT_END()
35         };
36
37         git_config(git_default_config, NULL);
38         argc = parse_options(argc, argv, unused_prefix, write_tree_options,
39                              write_tree_usage, 0);
40
41         ret = write_cache_as_tree(sha1, flags, prefix);
42         switch (ret) {
43         case 0:
44                 printf("%s\n", sha1_to_hex(sha1));
45                 break;
46         case WRITE_TREE_UNREADABLE_INDEX:
47                 die("%s: error reading the index", me);
48                 break;
49         case WRITE_TREE_UNMERGED_INDEX:
50                 die("%s: error building trees", me);
51                 break;
52         case WRITE_TREE_PREFIX_ERROR:
53                 die("%s: prefix %s not found", me, prefix);
54                 break;
55         }
56         return ret;
57 }