write-tree.c 1.5 KB

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