Merge branch 'maint'
[tgit.git] / builtin-write-tree.c
blob3a24ce8157be7c4209d831027693653b8d65ca13
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 cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
16 int flags = 0, ret;
17 const char *prefix = NULL;
18 unsigned char sha1[20];
19 const char *me = "git-write-tree";
21 git_config(git_default_config, NULL);
22 while (1 < argc) {
23 const char *arg = argv[1];
24 if (!strcmp(arg, "--missing-ok"))
25 flags |= WRITE_TREE_MISSING_OK;
26 else if (!prefixcmp(arg, "--prefix="))
27 prefix = arg + 9;
28 else if (!prefixcmp(arg, "--ignore-cache-tree"))
30 * This is only useful for debugging, so I
31 * do not bother documenting it.
33 flags |= WRITE_TREE_IGNORE_CACHE_TREE;
34 else
35 usage(write_tree_usage);
36 argc--; argv++;
39 if (argc > 2)
40 die("too many options");
42 ret = write_cache_as_tree(sha1, flags, prefix);
43 switch (ret) {
44 case 0:
45 printf("%s\n", sha1_to_hex(sha1));
46 break;
47 case WRITE_TREE_UNREADABLE_INDEX:
48 die("%s: error reading the index", me);
49 break;
50 case WRITE_TREE_UNMERGED_INDEX:
51 die("%s: error building trees", me);
52 break;
53 case WRITE_TREE_PREFIX_ERROR:
54 die("%s: prefix %s not found", me, prefix);
55 break;
57 return ret;