blob: b223af416fee5fc219fbcca7afa2b9e03feaa7d0 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Lukas Sandström8ed05fb2006-06-13 22:21:42 +02006#include "builtin.h"
Linus Torvaldse83c5162005-04-07 15:13:13 -07007#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02008#include "tree.h"
Junio C Hamanoa52139b2006-04-23 16:52:35 -07009#include "cache-tree.h"
Stephen Boyd404d42e2009-07-07 22:15:38 -070010#include "parse-options.h"
Junio C Hamanoa52139b2006-04-23 16:52:35 -070011
Stephen Boyd404d42e2009-07-07 22:15:38 -070012static const char * const write_tree_usage[] = {
13 "git write-tree [--missing-ok] [--prefix=<prefix>/]",
14 NULL
15};
Junio C Hamano75a46f62005-12-05 22:30:07 -080016
Linus Torvaldsa633fca2006-07-28 22:44:25 -070017int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
Lukas Sandström8ed05fb2006-06-13 22:21:42 +020018{
Junio C Hamanod11b8d32009-05-20 11:04:35 -070019 int flags = 0, ret;
Lukas Sandström8ed05fb2006-06-13 22:21:42 +020020 const char *prefix = NULL;
21 unsigned char sha1[20];
Junio C Hamano45525bd2008-01-10 22:49:35 -080022 const char *me = "git-write-tree";
Stephen Boyd404d42e2009-07-07 22:15:38 -070023 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 };
Lukas Sandström8ed05fb2006-06-13 22:21:42 +020035
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010036 git_config(git_default_config, NULL);
Stephen Boyd404d42e2009-07-07 22:15:38 -070037 argc = parse_options(argc, argv, unused_prefix, write_tree_options,
38 write_tree_usage, 0);
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070039
Junio C Hamanod11b8d32009-05-20 11:04:35 -070040 ret = write_cache_as_tree(sha1, flags, prefix);
Junio C Hamano45525bd2008-01-10 22:49:35 -080041 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:
Junio C Hamano331fcb52008-11-28 19:56:34 -080049 die("%s: error building trees", me);
Junio C Hamano45525bd2008-01-10 22:49:35 -080050 break;
51 case WRITE_TREE_PREFIX_ERROR:
52 die("%s: prefix %s not found", me, prefix);
53 break;
54 }
Lukas Sandström8ed05fb2006-06-13 22:21:42 +020055 return ret;
Linus Torvaldse83c5162005-04-07 15:13:13 -070056}