Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 5 | * Copyright (C) Junio C Hamano, 2005 |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 6 | */ |
| 7 | #include "cache.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 8 | #include "blob.h" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 9 | #include "quote.h" |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 10 | #include "parse-options.h" |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 11 | #include "exec_cmd.h" |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 12 | |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 13 | static void hash_fd(int fd, const char *type, int write_object, const char *path) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 14 | { |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 15 | struct stat st; |
| 16 | unsigned char sha1[20]; |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 17 | if (fstat(fd, &st) < 0 || |
| 18 | index_fd(sha1, fd, &st, write_object, type_from_string(type), path)) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 19 | die(write_object |
| 20 | ? "Unable to add %s to database" |
| 21 | : "Unable to hash %s", path); |
| 22 | printf("%s\n", sha1_to_hex(sha1)); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 23 | maybe_flush_or_die(stdout, "hash to stdout"); |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 26 | static void hash_object(const char *path, const char *type, int write_object, |
| 27 | const char *vpath) |
Daniel Barkalow | 024510c | 2005-12-10 17:25:24 -0500 | [diff] [blame] | 28 | { |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 29 | int fd; |
| 30 | fd = open(path, O_RDONLY); |
| 31 | if (fd < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 32 | die_errno("Cannot open '%s'", path); |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 33 | hash_fd(fd, type, write_object, vpath); |
Daniel Barkalow | 024510c | 2005-12-10 17:25:24 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 36 | static void hash_stdin_paths(const char *type, int write_objects) |
| 37 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 38 | struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 39 | |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 40 | while (strbuf_getline(&buf, stdin, '\n') != EOF) { |
| 41 | if (buf.buf[0] == '"') { |
| 42 | strbuf_reset(&nbuf); |
| 43 | if (unquote_c_style(&nbuf, buf.buf, NULL)) |
| 44 | die("line is badly quoted"); |
| 45 | strbuf_swap(&buf, &nbuf); |
| 46 | } |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 47 | hash_object(buf.buf, type, write_objects, buf.buf); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 48 | } |
| 49 | strbuf_release(&buf); |
| 50 | strbuf_release(&nbuf); |
| 51 | } |
| 52 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 53 | static const char * const hash_object_usage[] = { |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 54 | "git hash-object [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...", |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 55 | "git hash-object --stdin-paths < <list-of-paths>", |
| 56 | NULL |
| 57 | }; |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 58 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 59 | static const char *type; |
| 60 | static int write_object; |
| 61 | static int hashstdin; |
| 62 | static int stdin_paths; |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 63 | static int no_filters; |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 64 | static const char *vpath; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 65 | |
| 66 | static const struct option hash_object_options[] = { |
| 67 | OPT_STRING('t', NULL, &type, "type", "object type"), |
| 68 | OPT_BOOLEAN('w', NULL, &write_object, "write the object into the object database"), |
| 69 | OPT_BOOLEAN( 0 , "stdin", &hashstdin, "read the object from stdin"), |
| 70 | OPT_BOOLEAN( 0 , "stdin-paths", &stdin_paths, "read file names from stdin"), |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 71 | OPT_BOOLEAN( 0 , "no-filters", &no_filters, "store file as is without filters"), |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 72 | OPT_STRING( 0 , "path", &vpath, "file", "process file as it were from this path"), |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 73 | OPT_END() |
| 74 | }; |
| 75 | |
| 76 | int main(int argc, const char **argv) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 77 | { |
| 78 | int i; |
Junio C Hamano | 99e0169 | 2005-11-28 03:19:03 -0800 | [diff] [blame] | 79 | const char *prefix = NULL; |
Junio C Hamano | 706fe6a | 2005-11-26 00:30:07 -0800 | [diff] [blame] | 80 | int prefix_length = -1; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 81 | const char *errstr = NULL; |
| 82 | |
| 83 | type = blob_type; |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 84 | |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 85 | git_extract_argv0_path(argv[0]); |
| 86 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 87 | argc = parse_options(argc, argv, NULL, hash_object_options, |
| 88 | hash_object_usage, 0); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 89 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 90 | if (write_object) { |
| 91 | prefix = setup_git_directory(); |
| 92 | prefix_length = prefix ? strlen(prefix) : 0; |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 93 | if (vpath && prefix) |
| 94 | vpath = prefix_filename(prefix, prefix_length, vpath); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 95 | } |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 96 | |
Elijah Newren | 272459a | 2009-02-28 12:56:49 -0700 | [diff] [blame] | 97 | git_config(git_default_config, NULL); |
| 98 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 99 | if (stdin_paths) { |
| 100 | if (hashstdin) |
| 101 | errstr = "Can't use --stdin-paths with --stdin"; |
| 102 | else if (argc) |
| 103 | errstr = "Can't specify files with --stdin-paths"; |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 104 | else if (vpath) |
| 105 | errstr = "Can't use --stdin-paths with --path"; |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 106 | else if (no_filters) |
| 107 | errstr = "Can't use --stdin-paths with --no-filters"; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 108 | } |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 109 | else { |
| 110 | if (hashstdin > 1) |
| 111 | errstr = "Multiple --stdin arguments are not supported"; |
| 112 | if (vpath && no_filters) |
| 113 | errstr = "Can't use --path with --no-filters"; |
| 114 | } |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 115 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 116 | if (errstr) { |
Daniel Lowe | 42fc113 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 117 | error("%s", errstr); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 118 | usage_with_options(hash_object_usage, hash_object_options); |
| 119 | } |
| 120 | |
| 121 | if (hashstdin) |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 122 | hash_fd(0, type, write_object, vpath); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 123 | |
| 124 | for (i = 0 ; i < argc; i++) { |
| 125 | const char *arg = argv[i]; |
| 126 | |
| 127 | if (0 <= prefix_length) |
| 128 | arg = prefix_filename(prefix, prefix_length, arg); |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 129 | hash_object(arg, type, write_object, |
| 130 | no_filters ? NULL : vpath ? vpath : arg); |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 131 | } |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 132 | |
| 133 | if (stdin_paths) |
| 134 | hash_stdin_paths(type, write_object); |
| 135 | |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |