builtin / hash-object.con commit Merge branch 'jk/shortlog-ident-cleanup' (17cb5f8)
   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 "blob.h"
  10#include "quote.h"
  11#include "parse-options.h"
  12#include "exec_cmd.h"
  13
  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 */
  19static 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
  24        if (strbuf_read(&buf, fd, 4096) < 0)
  25                ret = -1;
  26        else
  27                ret = hash_sha1_file_literally(buf.buf, buf.len, type, oid, flags);
  28        strbuf_release(&buf);
  29        return ret;
  30}
  31
  32static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
  33                    int literally)
  34{
  35        struct stat st;
  36        struct object_id oid;
  37
  38        if (fstat(fd, &st) < 0 ||
  39            (literally
  40             ? hash_literally(&oid, fd, type, flags)
  41             : index_fd(&oid, fd, &st, type_from_string(type), path, flags)))
  42                die((flags & HASH_WRITE_OBJECT)
  43                    ? "Unable to add %s to database"
  44                    : "Unable to hash %s", path);
  45        printf("%s\n", oid_to_hex(&oid));
  46        maybe_flush_or_die(stdout, "hash to stdout");
  47}
  48
  49static void hash_object(const char *path, const char *type, const char *vpath,
  50                        unsigned flags, int literally)
  51{
  52        int fd;
  53        fd = open(path, O_RDONLY);
  54        if (fd < 0)
  55                die_errno("Cannot open '%s'", path);
  56        hash_fd(fd, type, vpath, flags, literally);
  57}
  58
  59static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
  60                             int literally)
  61{
  62        struct strbuf buf = STRBUF_INIT;
  63        struct strbuf unquoted = STRBUF_INIT;
  64
  65        while (strbuf_getline(&buf, stdin) != EOF) {
  66                if (buf.buf[0] == '"') {
  67                        strbuf_reset(&unquoted);
  68                        if (unquote_c_style(&unquoted, buf.buf, NULL))
  69                                die("line is badly quoted");
  70                        strbuf_swap(&buf, &unquoted);
  71                }
  72                hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
  73                            literally);
  74        }
  75        strbuf_release(&buf);
  76        strbuf_release(&unquoted);
  77}
  78
  79int cmd_hash_object(int argc, const char **argv, const char *prefix)
  80{
  81        static const char * const hash_object_usage[] = {
  82                N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
  83                N_("git hash-object  --stdin-paths"),
  84                NULL
  85        };
  86        const char *type = blob_type;
  87        int hashstdin = 0;
  88        int stdin_paths = 0;
  89        int no_filters = 0;
  90        int literally = 0;
  91        int nongit = 0;
  92        unsigned flags = HASH_FORMAT_CHECK;
  93        const char *vpath = NULL;
  94        const struct option hash_object_options[] = {
  95                OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
  96                OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
  97                        HASH_WRITE_OBJECT),
  98                OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
  99                OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
 100                OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
 101                OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
 102                OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
 103                OPT_END()
 104        };
 105        int i;
 106        const char *errstr = NULL;
 107
 108        argc = parse_options(argc, argv, NULL, hash_object_options,
 109                             hash_object_usage, 0);
 110
 111        if (flags & HASH_WRITE_OBJECT)
 112                prefix = setup_git_directory();
 113        else
 114                prefix = setup_git_directory_gently(&nongit);
 115
 116        if (vpath && prefix)
 117                vpath = xstrdup(prefix_filename(prefix, vpath));
 118
 119        git_config(git_default_config, NULL);
 120
 121        if (stdin_paths) {
 122                if (hashstdin)
 123                        errstr = "Can't use --stdin-paths with --stdin";
 124                else if (argc)
 125                        errstr = "Can't specify files with --stdin-paths";
 126                else if (vpath)
 127                        errstr = "Can't use --stdin-paths with --path";
 128        }
 129        else {
 130                if (hashstdin > 1)
 131                        errstr = "Multiple --stdin arguments are not supported";
 132                if (vpath && no_filters)
 133                        errstr = "Can't use --path with --no-filters";
 134        }
 135
 136        if (errstr) {
 137                error("%s", errstr);
 138                usage_with_options(hash_object_usage, hash_object_options);
 139        }
 140
 141        if (hashstdin)
 142                hash_fd(0, type, vpath, flags, literally);
 143
 144        for (i = 0 ; i < argc; i++) {
 145                const char *arg = argv[i];
 146                char *to_free = NULL;
 147
 148                if (prefix)
 149                        arg = to_free = prefix_filename(prefix, arg);
 150                hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
 151                            flags, literally);
 152                free(to_free);
 153        }
 154
 155        if (stdin_paths)
 156                hash_stdin_paths(type, no_filters, flags, literally);
 157
 158        return 0;
 159}