hash-object.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. * Copyright (C) Junio C Hamano, 2005
  6. */
  7. #include "builtin.h"
  8. #include "config.h"
  9. #include "object-store.h"
  10. #include "blob.h"
  11. #include "quote.h"
  12. #include "parse-options.h"
  13. #include "exec-cmd.h"
  14. /*
  15. * This is to create corrupt objects for debugging and as such it
  16. * needs to bypass the data conversion performed by, and the type
  17. * limitation imposed by, index_fd() and its callees.
  18. */
  19. static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
  20. {
  21. struct strbuf buf = STRBUF_INIT;
  22. int ret;
  23. if (strbuf_read(&buf, fd, 4096) < 0)
  24. ret = -1;
  25. else
  26. ret = hash_object_file_literally(buf.buf, buf.len, type, oid,
  27. flags);
  28. strbuf_release(&buf);
  29. return ret;
  30. }
  31. static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
  32. int literally)
  33. {
  34. struct stat st;
  35. struct object_id oid;
  36. if (fstat(fd, &st) < 0 ||
  37. (literally
  38. ? hash_literally(&oid, fd, type, flags)
  39. : index_fd(the_repository->index, &oid, fd, &st,
  40. type_from_string(type), path, flags)))
  41. die((flags & HASH_WRITE_OBJECT)
  42. ? "Unable to add %s to database"
  43. : "Unable to hash %s", path);
  44. printf("%s\n", oid_to_hex(&oid));
  45. maybe_flush_or_die(stdout, "hash to stdout");
  46. }
  47. static void hash_object(const char *path, const char *type, const char *vpath,
  48. unsigned flags, int literally)
  49. {
  50. int fd;
  51. fd = open(path, O_RDONLY);
  52. if (fd < 0)
  53. die_errno("Cannot open '%s'", path);
  54. hash_fd(fd, type, vpath, flags, literally);
  55. }
  56. static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
  57. int literally)
  58. {
  59. struct strbuf buf = STRBUF_INIT;
  60. struct strbuf unquoted = STRBUF_INIT;
  61. while (strbuf_getline(&buf, stdin) != EOF) {
  62. if (buf.buf[0] == '"') {
  63. strbuf_reset(&unquoted);
  64. if (unquote_c_style(&unquoted, buf.buf, NULL))
  65. die("line is badly quoted");
  66. strbuf_swap(&buf, &unquoted);
  67. }
  68. hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
  69. literally);
  70. }
  71. strbuf_release(&buf);
  72. strbuf_release(&unquoted);
  73. }
  74. int cmd_hash_object(int argc, const char **argv, const char *prefix)
  75. {
  76. static const char * const hash_object_usage[] = {
  77. N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
  78. N_("git hash-object --stdin-paths"),
  79. NULL
  80. };
  81. const char *type = blob_type;
  82. int hashstdin = 0;
  83. int stdin_paths = 0;
  84. int no_filters = 0;
  85. int literally = 0;
  86. int nongit = 0;
  87. unsigned flags = HASH_FORMAT_CHECK;
  88. const char *vpath = NULL;
  89. const struct option hash_object_options[] = {
  90. OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
  91. OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
  92. HASH_WRITE_OBJECT),
  93. OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
  94. OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
  95. OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
  96. OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
  97. OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
  98. OPT_END()
  99. };
  100. int i;
  101. const char *errstr = NULL;
  102. argc = parse_options(argc, argv, prefix, hash_object_options,
  103. hash_object_usage, 0);
  104. if (flags & HASH_WRITE_OBJECT)
  105. prefix = setup_git_directory();
  106. else
  107. prefix = setup_git_directory_gently(&nongit);
  108. if (vpath && prefix)
  109. vpath = xstrdup(prefix_filename(prefix, vpath));
  110. git_config(git_default_config, NULL);
  111. if (stdin_paths) {
  112. if (hashstdin)
  113. errstr = "Can't use --stdin-paths with --stdin";
  114. else if (argc)
  115. errstr = "Can't specify files with --stdin-paths";
  116. else if (vpath)
  117. errstr = "Can't use --stdin-paths with --path";
  118. }
  119. else {
  120. if (hashstdin > 1)
  121. errstr = "Multiple --stdin arguments are not supported";
  122. if (vpath && no_filters)
  123. errstr = "Can't use --path with --no-filters";
  124. }
  125. if (errstr) {
  126. error("%s", errstr);
  127. usage_with_options(hash_object_usage, hash_object_options);
  128. }
  129. if (hashstdin)
  130. hash_fd(0, type, vpath, flags, literally);
  131. for (i = 0 ; i < argc; i++) {
  132. const char *arg = argv[i];
  133. char *to_free = NULL;
  134. if (prefix)
  135. arg = to_free = prefix_filename(prefix, arg);
  136. hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
  137. flags, literally);
  138. free(to_free);
  139. }
  140. if (stdin_paths)
  141. hash_stdin_paths(type, no_filters, flags, literally);
  142. return 0;
  143. }