hash-object.con commit correct usage help string for git-hash-object (9ae8e00)
   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 "cache.h"
   8#include "blob.h"
   9#include "quote.h"
  10
  11static void hash_fd(int fd, const char *type, int write_object, const char *path)
  12{
  13        struct stat st;
  14        unsigned char sha1[20];
  15        if (fstat(fd, &st) < 0 ||
  16            index_fd(sha1, fd, &st, write_object, type_from_string(type), path))
  17                die(write_object
  18                    ? "Unable to add %s to database"
  19                    : "Unable to hash %s", path);
  20        printf("%s\n", sha1_to_hex(sha1));
  21        maybe_flush_or_die(stdout, "hash to stdout");
  22}
  23
  24static void hash_object(const char *path, const char *type, int write_object)
  25{
  26        int fd;
  27        fd = open(path, O_RDONLY);
  28        if (fd < 0)
  29                die("Cannot open %s", path);
  30        hash_fd(fd, type, write_object, path);
  31}
  32
  33static void hash_stdin_paths(const char *type, int write_objects)
  34{
  35        struct strbuf buf, nbuf;
  36
  37        strbuf_init(&buf, 0);
  38        strbuf_init(&nbuf, 0);
  39        while (strbuf_getline(&buf, stdin, '\n') != EOF) {
  40                if (buf.buf[0] == '"') {
  41                        strbuf_reset(&nbuf);
  42                        if (unquote_c_style(&nbuf, buf.buf, NULL))
  43                                die("line is badly quoted");
  44                        strbuf_swap(&buf, &nbuf);
  45                }
  46                hash_object(buf.buf, type, write_objects);
  47        }
  48        strbuf_release(&buf);
  49        strbuf_release(&nbuf);
  50}
  51
  52static const char hash_object_usage[] =
  53"git hash-object [-t <type>] [-w] [--stdin] [--] <file>...\n"
  54"   or: git hash-object  --stdin-paths < <list-of-paths>";
  55
  56int main(int argc, char **argv)
  57{
  58        int i;
  59        const char *type = blob_type;
  60        int write_object = 0;
  61        const char *prefix = NULL;
  62        int prefix_length = -1;
  63        int no_more_flags = 0;
  64        int hashstdin = 0;
  65        int stdin_paths = 0;
  66
  67        git_config(git_default_config, NULL);
  68
  69        for (i = 1 ; i < argc; i++) {
  70                if (!no_more_flags && argv[i][0] == '-') {
  71                        if (!strcmp(argv[i], "-t")) {
  72                                if (argc <= ++i)
  73                                        usage(hash_object_usage);
  74                                type = argv[i];
  75                        }
  76                        else if (!strcmp(argv[i], "-w")) {
  77                                if (prefix_length < 0) {
  78                                        prefix = setup_git_directory();
  79                                        prefix_length =
  80                                                prefix ? strlen(prefix) : 0;
  81                                }
  82                                write_object = 1;
  83                        }
  84                        else if (!strcmp(argv[i], "--")) {
  85                                no_more_flags = 1;
  86                        }
  87                        else if (!strcmp(argv[i], "--help"))
  88                                usage(hash_object_usage);
  89                        else if (!strcmp(argv[i], "--stdin-paths")) {
  90                                if (hashstdin) {
  91                                        error("Can't use --stdin-paths with --stdin");
  92                                        usage(hash_object_usage);
  93                                }
  94                                stdin_paths = 1;
  95
  96                        }
  97                        else if (!strcmp(argv[i], "--stdin")) {
  98                                if (stdin_paths) {
  99                                        error("Can't use %s with --stdin-paths", argv[i]);
 100                                        usage(hash_object_usage);
 101                                }
 102                                if (hashstdin)
 103                                        die("Multiple --stdin arguments are not supported");
 104                                hashstdin = 1;
 105                        }
 106                        else
 107                                usage(hash_object_usage);
 108                }
 109                else {
 110                        const char *arg = argv[i];
 111
 112                        if (stdin_paths) {
 113                                error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
 114                                usage(hash_object_usage);
 115                        }
 116
 117                        if (hashstdin) {
 118                                hash_fd(0, type, write_object, NULL);
 119                                hashstdin = 0;
 120                        }
 121                        if (0 <= prefix_length)
 122                                arg = prefix_filename(prefix, prefix_length,
 123                                                      arg);
 124                        hash_object(arg, type, write_object);
 125                        no_more_flags = 1;
 126                }
 127        }
 128
 129        if (stdin_paths)
 130                hash_stdin_paths(type, write_object);
 131
 132        if (hashstdin)
 133                hash_fd(0, type, write_object, NULL);
 134        return 0;
 135}