hash-object.con commit Merge branch 'ew/maint-svn-cert-fileprovider' into maint (ca2c195)
   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
  10static void hash_object(const char *path, enum object_type type, int write_object)
  11{
  12        int fd;
  13        struct stat st;
  14        unsigned char sha1[20];
  15        fd = open(path, O_RDONLY);
  16        if (fd < 0 ||
  17            fstat(fd, &st) < 0 ||
  18            index_fd(sha1, fd, &st, write_object, type, path))
  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));
  23}
  24
  25static void hash_stdin(const char *type, int write_object)
  26{
  27        unsigned char sha1[20];
  28        if (index_pipe(sha1, 0, type, write_object))
  29                die("Unable to add stdin to database");
  30        printf("%s\n", sha1_to_hex(sha1));
  31}
  32
  33static const char hash_object_usage[] =
  34"git-hash-object [-t <type>] [-w] [--stdin] <file>...";
  35
  36int main(int argc, char **argv)
  37{
  38        int i;
  39        const char *type = blob_type;
  40        int write_object = 0;
  41        const char *prefix = NULL;
  42        int prefix_length = -1;
  43        int no_more_flags = 0;
  44
  45        git_config(git_default_config);
  46
  47        for (i = 1 ; i < argc; i++) {
  48                if (!no_more_flags && argv[i][0] == '-') {
  49                        if (!strcmp(argv[i], "-t")) {
  50                                if (argc <= ++i)
  51                                        usage(hash_object_usage);
  52                                type = argv[i];
  53                        }
  54                        else if (!strcmp(argv[i], "-w")) {
  55                                if (prefix_length < 0) {
  56                                        prefix = setup_git_directory();
  57                                        prefix_length =
  58                                                prefix ? strlen(prefix) : 0;
  59                                }
  60                                write_object = 1;
  61                        }
  62                        else if (!strcmp(argv[i], "--")) {
  63                                no_more_flags = 1;
  64                        }
  65                        else if (!strcmp(argv[i], "--help"))
  66                                usage(hash_object_usage);
  67                        else if (!strcmp(argv[i], "--stdin")) {
  68                                hash_stdin(type, write_object);
  69                        }
  70                        else
  71                                usage(hash_object_usage);
  72                }
  73                else {
  74                        const char *arg = argv[i];
  75                        if (0 <= prefix_length)
  76                                arg = prefix_filename(prefix, prefix_length,
  77                                                      arg);
  78                        hash_object(arg, type_from_string(type), write_object);
  79                        no_more_flags = 1;
  80                }
  81        }
  82        return 0;
  83}