Merge branch 'rs/t4062-obsd' into next
[gitweb.git] / builtin / hash-object.c
index f7d3567dd0ce2d75778d6cf011961c0f203432b4..d04baf999a94cfa6a07e74861876d6a9f1c88a6d 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) Junio C Hamano, 2005
  */
 #include "builtin.h"
+#include "config.h"
 #include "blob.h"
 #include "quote.h"
 #include "parse-options.h"
@@ -87,6 +88,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
        int stdin_paths = 0;
        int no_filters = 0;
        int literally = 0;
+       int nongit = 0;
        unsigned flags = HASH_FORMAT_CHECK;
        const char *vpath = NULL;
        const struct option hash_object_options[] = {
@@ -101,18 +103,18 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
        int i;
-       int prefix_length = -1;
        const char *errstr = NULL;
 
        argc = parse_options(argc, argv, NULL, hash_object_options,
                             hash_object_usage, 0);
 
-       if (flags & HASH_WRITE_OBJECT) {
+       if (flags & HASH_WRITE_OBJECT)
                prefix = setup_git_directory();
-               prefix_length = prefix ? strlen(prefix) : 0;
-               if (vpath && prefix)
-                       vpath = prefix_filename(prefix, prefix_length, vpath);
-       }
+       else
+               prefix = setup_git_directory_gently(&nongit);
+
+       if (vpath && prefix)
+               vpath = xstrdup(prefix_filename(prefix, vpath));
 
        git_config(git_default_config, NULL);
 
@@ -141,11 +143,13 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
 
        for (i = 0 ; i < argc; i++) {
                const char *arg = argv[i];
+               char *to_free = NULL;
 
-               if (0 <= prefix_length)
-                       arg = prefix_filename(prefix, prefix_length, arg);
+               if (prefix)
+                       arg = to_free = prefix_filename(prefix, arg);
                hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
                            flags, literally);
+               free(to_free);
        }
 
        if (stdin_paths)