sha1_file: refactor has_sha1_file_with_flags
[gitweb.git] / builtin / hash-object.c
index f7d3567dd0ce2d75778d6cf011961c0f203432b4..bbeaf20bcca1ae1e9bfa55b5a8e4adbed83c00e8 100644 (file)
@@ -87,6 +87,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 +102,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 +142,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)