Merge branch 'dp/hash-literally'
* dp/hash-literally: add --no-filters option to git hash-object add --path option to git hash-object use parse_options() in git hash-object correct usage help string for git-hash-object correct argument checking test for git hash-object teach index_fd to work with pipesmaint
commit
d8eec50468
|
@ -8,7 +8,9 @@ git-hash-object - Compute object ID and optionally creates a blob from a file
|
||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
--------
|
--------
|
||||||
'git hash-object' [-t <type>] [-w] [--stdin | --stdin-paths] [--] <file>...
|
[verse]
|
||||||
|
'git hash-object' [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...
|
||||||
|
'git hash-object' [-t <type>] [-w] --stdin-paths < <list-of-paths>
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
|
@ -35,6 +37,22 @@ OPTIONS
|
||||||
--stdin-paths::
|
--stdin-paths::
|
||||||
Read file names from stdin instead of from the command-line.
|
Read file names from stdin instead of from the command-line.
|
||||||
|
|
||||||
|
--path::
|
||||||
|
Hash object as it were located at the given path. The location of
|
||||||
|
file does not directly influence on the hash value, but path is
|
||||||
|
used to determine what git filters should be applied to the object
|
||||||
|
before it can be placed to the object database, and, as result of
|
||||||
|
applying filters, the actual blob put into the object database may
|
||||||
|
differ from the given file. This option is mainly useful for hashing
|
||||||
|
temporary files located outside of the working directory or files
|
||||||
|
read from stdin.
|
||||||
|
|
||||||
|
--no-filters::
|
||||||
|
Hash the contents as is, ignoring any input filter that would
|
||||||
|
have been chosen by the attributes mechanism, including crlf
|
||||||
|
conversion. If the file is read from standard input then this
|
||||||
|
is always implied, unless the --path option is given.
|
||||||
|
|
||||||
Author
|
Author
|
||||||
------
|
------
|
||||||
Written by Junio C Hamano <gitster@pobox.com>
|
Written by Junio C Hamano <gitster@pobox.com>
|
||||||
|
|
1
cache.h
1
cache.h
|
@ -391,7 +391,6 @@ extern int ie_modified(const struct index_state *, struct cache_entry *, struct
|
||||||
|
|
||||||
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
|
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
|
||||||
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
|
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
|
||||||
extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
|
|
||||||
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
|
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
|
||||||
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
|
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
|
||||||
|
|
||||||
|
|
153
hash-object.c
153
hash-object.c
|
@ -7,16 +7,14 @@
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
#include "blob.h"
|
#include "blob.h"
|
||||||
#include "quote.h"
|
#include "quote.h"
|
||||||
|
#include "parse-options.h"
|
||||||
|
|
||||||
static void hash_object(const char *path, enum object_type type, int write_object)
|
static void hash_fd(int fd, const char *type, int write_object, const char *path)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
fd = open(path, O_RDONLY);
|
if (fstat(fd, &st) < 0 ||
|
||||||
if (fd < 0 ||
|
index_fd(sha1, fd, &st, write_object, type_from_string(type), path))
|
||||||
fstat(fd, &st) < 0 ||
|
|
||||||
index_fd(sha1, fd, &st, write_object, type, path))
|
|
||||||
die(write_object
|
die(write_object
|
||||||
? "Unable to add %s to database"
|
? "Unable to add %s to database"
|
||||||
: "Unable to hash %s", path);
|
: "Unable to hash %s", path);
|
||||||
|
@ -24,12 +22,14 @@ static void hash_object(const char *path, enum object_type type, int write_objec
|
||||||
maybe_flush_or_die(stdout, "hash to stdout");
|
maybe_flush_or_die(stdout, "hash to stdout");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hash_stdin(const char *type, int write_object)
|
static void hash_object(const char *path, const char *type, int write_object,
|
||||||
|
const char *vpath)
|
||||||
{
|
{
|
||||||
unsigned char sha1[20];
|
int fd;
|
||||||
if (index_pipe(sha1, 0, type, write_object))
|
fd = open(path, O_RDONLY);
|
||||||
die("Unable to add stdin to database");
|
if (fd < 0)
|
||||||
printf("%s\n", sha1_to_hex(sha1));
|
die("Cannot open %s", path);
|
||||||
|
hash_fd(fd, type, write_object, vpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hash_stdin_paths(const char *type, int write_objects)
|
static void hash_stdin_paths(const char *type, int write_objects)
|
||||||
|
@ -45,92 +45,91 @@ static void hash_stdin_paths(const char *type, int write_objects)
|
||||||
die("line is badly quoted");
|
die("line is badly quoted");
|
||||||
strbuf_swap(&buf, &nbuf);
|
strbuf_swap(&buf, &nbuf);
|
||||||
}
|
}
|
||||||
hash_object(buf.buf, type_from_string(type), write_objects);
|
hash_object(buf.buf, type, write_objects, buf.buf);
|
||||||
}
|
}
|
||||||
strbuf_release(&buf);
|
strbuf_release(&buf);
|
||||||
strbuf_release(&nbuf);
|
strbuf_release(&nbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char hash_object_usage[] =
|
static const char * const hash_object_usage[] = {
|
||||||
"git hash-object [ [-t <type>] [-w] [--stdin] <file>... | --stdin-paths < <list-of-paths> ]";
|
"git hash-object [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...",
|
||||||
|
"git hash-object --stdin-paths < <list-of-paths>",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
static const char *type;
|
||||||
|
static int write_object;
|
||||||
|
static int hashstdin;
|
||||||
|
static int stdin_paths;
|
||||||
|
static int no_filters;
|
||||||
|
static const char *vpath;
|
||||||
|
|
||||||
|
static const struct option hash_object_options[] = {
|
||||||
|
OPT_STRING('t', NULL, &type, "type", "object type"),
|
||||||
|
OPT_BOOLEAN('w', NULL, &write_object, "write the object into the object database"),
|
||||||
|
OPT_BOOLEAN( 0 , "stdin", &hashstdin, "read the object from stdin"),
|
||||||
|
OPT_BOOLEAN( 0 , "stdin-paths", &stdin_paths, "read file names from stdin"),
|
||||||
|
OPT_BOOLEAN( 0 , "no-filters", &no_filters, "store file as is without filters"),
|
||||||
|
OPT_STRING( 0 , "path", &vpath, "file", "process file as it were from this path"),
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
const char *type = blob_type;
|
|
||||||
int write_object = 0;
|
|
||||||
const char *prefix = NULL;
|
const char *prefix = NULL;
|
||||||
int prefix_length = -1;
|
int prefix_length = -1;
|
||||||
int no_more_flags = 0;
|
const char *errstr = NULL;
|
||||||
int hashstdin = 0;
|
|
||||||
int stdin_paths = 0;
|
type = blob_type;
|
||||||
|
|
||||||
git_config(git_default_config, NULL);
|
git_config(git_default_config, NULL);
|
||||||
|
|
||||||
for (i = 1 ; i < argc; i++) {
|
argc = parse_options(argc, argv, hash_object_options, hash_object_usage, 0);
|
||||||
if (!no_more_flags && argv[i][0] == '-') {
|
|
||||||
if (!strcmp(argv[i], "-t")) {
|
|
||||||
if (argc <= ++i)
|
|
||||||
usage(hash_object_usage);
|
|
||||||
type = argv[i];
|
|
||||||
}
|
|
||||||
else if (!strcmp(argv[i], "-w")) {
|
|
||||||
if (prefix_length < 0) {
|
|
||||||
prefix = setup_git_directory();
|
|
||||||
prefix_length =
|
|
||||||
prefix ? strlen(prefix) : 0;
|
|
||||||
}
|
|
||||||
write_object = 1;
|
|
||||||
}
|
|
||||||
else if (!strcmp(argv[i], "--")) {
|
|
||||||
no_more_flags = 1;
|
|
||||||
}
|
|
||||||
else if (!strcmp(argv[i], "--help"))
|
|
||||||
usage(hash_object_usage);
|
|
||||||
else if (!strcmp(argv[i], "--stdin-paths")) {
|
|
||||||
if (hashstdin) {
|
|
||||||
error("Can't use --stdin-paths with --stdin");
|
|
||||||
usage(hash_object_usage);
|
|
||||||
}
|
|
||||||
stdin_paths = 1;
|
|
||||||
|
|
||||||
}
|
if (write_object) {
|
||||||
else if (!strcmp(argv[i], "--stdin")) {
|
prefix = setup_git_directory();
|
||||||
if (stdin_paths) {
|
prefix_length = prefix ? strlen(prefix) : 0;
|
||||||
error("Can't use %s with --stdin-paths", argv[i]);
|
if (vpath && prefix)
|
||||||
usage(hash_object_usage);
|
vpath = prefix_filename(prefix, prefix_length, vpath);
|
||||||
}
|
}
|
||||||
if (hashstdin)
|
|
||||||
die("Multiple --stdin arguments are not supported");
|
|
||||||
hashstdin = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
usage(hash_object_usage);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const char *arg = argv[i];
|
|
||||||
|
|
||||||
if (stdin_paths) {
|
if (stdin_paths) {
|
||||||
error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
|
if (hashstdin)
|
||||||
usage(hash_object_usage);
|
errstr = "Can't use --stdin-paths with --stdin";
|
||||||
}
|
else if (argc)
|
||||||
|
errstr = "Can't specify files with --stdin-paths";
|
||||||
|
else if (vpath)
|
||||||
|
errstr = "Can't use --stdin-paths with --path";
|
||||||
|
else if (no_filters)
|
||||||
|
errstr = "Can't use --stdin-paths with --no-filters";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (hashstdin > 1)
|
||||||
|
errstr = "Multiple --stdin arguments are not supported";
|
||||||
|
if (vpath && no_filters)
|
||||||
|
errstr = "Can't use --path with --no-filters";
|
||||||
|
}
|
||||||
|
|
||||||
if (hashstdin) {
|
if (errstr) {
|
||||||
hash_stdin(type, write_object);
|
error (errstr);
|
||||||
hashstdin = 0;
|
usage_with_options(hash_object_usage, hash_object_options);
|
||||||
}
|
}
|
||||||
if (0 <= prefix_length)
|
|
||||||
arg = prefix_filename(prefix, prefix_length,
|
if (hashstdin)
|
||||||
arg);
|
hash_fd(0, type, write_object, vpath);
|
||||||
hash_object(arg, type_from_string(type), write_object);
|
|
||||||
no_more_flags = 1;
|
for (i = 0 ; i < argc; i++) {
|
||||||
}
|
const char *arg = argv[i];
|
||||||
|
|
||||||
|
if (0 <= prefix_length)
|
||||||
|
arg = prefix_filename(prefix, prefix_length, arg);
|
||||||
|
hash_object(arg, type, write_object,
|
||||||
|
no_filters ? NULL : vpath ? vpath : arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stdin_paths)
|
if (stdin_paths)
|
||||||
hash_stdin_paths(type, write_object);
|
hash_stdin_paths(type, write_object);
|
||||||
|
|
||||||
if (hashstdin)
|
|
||||||
hash_stdin(type, write_object);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
64
sha1_file.c
64
sha1_file.c
|
@ -2360,51 +2360,22 @@ int has_sha1_file(const unsigned char *sha1)
|
||||||
return has_loose_object(sha1);
|
return has_loose_object(sha1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
|
static int index_mem(unsigned char *sha1, void *buf, size_t size,
|
||||||
|
int write_object, enum object_type type, const char *path)
|
||||||
{
|
{
|
||||||
struct strbuf buf;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
strbuf_init(&buf, 0);
|
|
||||||
if (strbuf_read(&buf, fd, 4096) < 0) {
|
|
||||||
strbuf_release(&buf);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!type)
|
|
||||||
type = blob_type;
|
|
||||||
if (write_object)
|
|
||||||
ret = write_sha1_file(buf.buf, buf.len, type, sha1);
|
|
||||||
else
|
|
||||||
ret = hash_sha1_file(buf.buf, buf.len, type, sha1);
|
|
||||||
strbuf_release(&buf);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
|
|
||||||
enum object_type type, const char *path)
|
|
||||||
{
|
|
||||||
size_t size = xsize_t(st->st_size);
|
|
||||||
void *buf = NULL;
|
|
||||||
int ret, re_allocated = 0;
|
int ret, re_allocated = 0;
|
||||||
|
|
||||||
if (size)
|
|
||||||
buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
if (!type)
|
if (!type)
|
||||||
type = OBJ_BLOB;
|
type = OBJ_BLOB;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convert blobs to git internal format
|
* Convert blobs to git internal format
|
||||||
*/
|
*/
|
||||||
if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {
|
if ((type == OBJ_BLOB) && path) {
|
||||||
struct strbuf nbuf;
|
struct strbuf nbuf;
|
||||||
strbuf_init(&nbuf, 0);
|
strbuf_init(&nbuf, 0);
|
||||||
if (convert_to_git(path, buf, size, &nbuf,
|
if (convert_to_git(path, buf, size, &nbuf,
|
||||||
write_object ? safe_crlf : 0)) {
|
write_object ? safe_crlf : 0)) {
|
||||||
munmap(buf, size);
|
|
||||||
buf = strbuf_detach(&nbuf, &size);
|
buf = strbuf_detach(&nbuf, &size);
|
||||||
re_allocated = 1;
|
re_allocated = 1;
|
||||||
}
|
}
|
||||||
|
@ -2414,12 +2385,33 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
|
||||||
ret = write_sha1_file(buf, size, typename(type), sha1);
|
ret = write_sha1_file(buf, size, typename(type), sha1);
|
||||||
else
|
else
|
||||||
ret = hash_sha1_file(buf, size, typename(type), sha1);
|
ret = hash_sha1_file(buf, size, typename(type), sha1);
|
||||||
if (re_allocated) {
|
if (re_allocated)
|
||||||
free(buf);
|
free(buf);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
if (size)
|
|
||||||
|
int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
|
||||||
|
enum object_type type, const char *path)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
size_t size = xsize_t(st->st_size);
|
||||||
|
|
||||||
|
if (!S_ISREG(st->st_mode)) {
|
||||||
|
struct strbuf sbuf;
|
||||||
|
strbuf_init(&sbuf, 0);
|
||||||
|
if (strbuf_read(&sbuf, fd, 4096) >= 0)
|
||||||
|
ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object,
|
||||||
|
type, path);
|
||||||
|
else
|
||||||
|
ret = -1;
|
||||||
|
strbuf_release(&sbuf);
|
||||||
|
} else if (size) {
|
||||||
|
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
|
ret = index_mem(sha1, buf, size, write_object, type, path);
|
||||||
munmap(buf, size);
|
munmap(buf, size);
|
||||||
|
} else
|
||||||
|
ret = index_mem(sha1, NULL, size, write_object, type, path);
|
||||||
|
close(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,16 +49,28 @@ setup_repo
|
||||||
# Argument checking
|
# Argument checking
|
||||||
|
|
||||||
test_expect_success "multiple '--stdin's are rejected" '
|
test_expect_success "multiple '--stdin's are rejected" '
|
||||||
test_must_fail git hash-object --stdin --stdin < example
|
echo example | test_must_fail git hash-object --stdin --stdin
|
||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success "Can't use --stdin and --stdin-paths together" '
|
test_expect_success "Can't use --stdin and --stdin-paths together" '
|
||||||
test_must_fail git hash-object --stdin --stdin-paths &&
|
echo example | test_must_fail git hash-object --stdin --stdin-paths &&
|
||||||
test_must_fail git hash-object --stdin-paths --stdin
|
echo example | test_must_fail git hash-object --stdin-paths --stdin
|
||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
|
test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
|
||||||
test_must_fail git hash-object --stdin-paths hello < example
|
echo example | test_must_fail git hash-object --stdin-paths hello
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "Can't use --path with --stdin-paths" '
|
||||||
|
echo example | test_must_fail git hash-object --stdin-paths --path=foo
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "Can't use --stdin-paths with --no-filters" '
|
||||||
|
echo example | test_must_fail git hash-object --stdin-paths --no-filters
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "Can't use --path with --no-filters" '
|
||||||
|
test_must_fail git hash-object --no-filters --path=foo
|
||||||
'
|
'
|
||||||
|
|
||||||
# Behavior
|
# Behavior
|
||||||
|
@ -93,6 +105,42 @@ test_expect_success 'git hash-object --stdin file1 <file0 first operates on file
|
||||||
test "$obname1" = "$obname1new"
|
test "$obname1" = "$obname1new"
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'check that appropriate filter is invoke when --path is used' '
|
||||||
|
echo fooQ | tr Q "\\015" >file0 &&
|
||||||
|
cp file0 file1 &&
|
||||||
|
echo "file0 -crlf" >.gitattributes &&
|
||||||
|
echo "file1 crlf" >>.gitattributes &&
|
||||||
|
git config core.autocrlf true &&
|
||||||
|
file0_sha=$(git hash-object file0) &&
|
||||||
|
file1_sha=$(git hash-object file1) &&
|
||||||
|
test "$file0_sha" != "$file1_sha" &&
|
||||||
|
path1_sha=$(git hash-object --path=file1 file0) &&
|
||||||
|
path0_sha=$(git hash-object --path=file0 file1) &&
|
||||||
|
test "$file0_sha" = "$path0_sha" &&
|
||||||
|
test "$file1_sha" = "$path1_sha" &&
|
||||||
|
path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
|
||||||
|
path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
|
||||||
|
test "$file0_sha" = "$path0_sha" &&
|
||||||
|
test "$file1_sha" = "$path1_sha" &&
|
||||||
|
git config --unset core.autocrlf
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'check that --no-filters option works' '
|
||||||
|
echo fooQ | tr Q "\\015" >file0 &&
|
||||||
|
cp file0 file1 &&
|
||||||
|
echo "file0 -crlf" >.gitattributes &&
|
||||||
|
echo "file1 crlf" >>.gitattributes &&
|
||||||
|
git config core.autocrlf true &&
|
||||||
|
file0_sha=$(git hash-object file0) &&
|
||||||
|
file1_sha=$(git hash-object file1) &&
|
||||||
|
test "$file0_sha" != "$file1_sha" &&
|
||||||
|
nofilters_file1=$(git hash-object --no-filters file1) &&
|
||||||
|
test "$file0_sha" = "$nofilters_file1" &&
|
||||||
|
nofilters_file1=$(cat file1 | git hash-object --stdin) &&
|
||||||
|
test "$file0_sha" = "$nofilters_file1" &&
|
||||||
|
git config --unset core.autocrlf
|
||||||
|
'
|
||||||
|
|
||||||
pop_repo
|
pop_repo
|
||||||
|
|
||||||
for args in "-w --stdin" "--stdin -w"; do
|
for args in "-w --stdin" "--stdin -w"; do
|
||||||
|
|
Loading…
Reference in New Issue