add 'git credential' plumbing command
[git/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"
404d42e5 10#include "parse-options.h"
e83c5163 11
404d42e5
SB
12static const char * const write_tree_usage[] = {
13 "git write-tree [--missing-ok] [--prefix=<prefix>/]",
14 NULL
15};
75a46f6b 16
a633fca0 17int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
8ed05fb5 18{
d11b8d34 19 int flags = 0, ret;
8ed05fb5
LS
20 const char *prefix = NULL;
21 unsigned char sha1[20];
45525bd0 22 const char *me = "git-write-tree";
404d42e5
SB
23 struct option write_tree_options[] = {
24 OPT_BIT(0, "missing-ok", &flags, "allow missing objects",
25 WRITE_TREE_MISSING_OK),
26 { OPTION_STRING, 0, "prefix", &prefix, "<prefix>/",
27 "write tree object for a subdirectory <prefix>" ,
28 PARSE_OPT_LITERAL_ARGHELP },
29 { OPTION_BIT, 0, "ignore-cache-tree", &flags, NULL,
30 "only useful for debugging",
31 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, NULL,
32 WRITE_TREE_IGNORE_CACHE_TREE },
33 OPT_END()
34 };
8ed05fb5 35
ef90d6d4 36 git_config(git_default_config, NULL);
404d42e5
SB
37 argc = parse_options(argc, argv, unused_prefix, write_tree_options,
38 write_tree_usage, 0);
8ed05fb5 39
d11b8d34 40 ret = write_cache_as_tree(sha1, flags, prefix);
45525bd0
JH
41 switch (ret) {
42 case 0:
43 printf("%s\n", sha1_to_hex(sha1));
44 break;
45 case WRITE_TREE_UNREADABLE_INDEX:
46 die("%s: error reading the index", me);
47 break;
48 case WRITE_TREE_UNMERGED_INDEX:
331fcb59 49 die("%s: error building trees", me);
45525bd0
JH
50 break;
51 case WRITE_TREE_PREFIX_ERROR:
52 die("%s: prefix %s not found", me, prefix);
53 break;
54 }
8ed05fb5
LS
55 return ret;
56}