Skip to content

Commit 024510c

Browse files
iabervonJunio C Hamano
authored andcommitted
Allow saving an object from a pipe
In order to support getting data into git with scripts, this adds a --stdin option to git-hash-object, which will make it read from stdin. Signed-off-by: Daniel Barkalow <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10945e0 commit 024510c

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

Documentation/git-hash-object.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-hash-object - Computes object ID and optionally creates a blob from a file.
88

99
SYNOPSIS
1010
--------
11-
'git-hash-object' [-t <type>] [-w] <file>
11+
'git-hash-object' [-t <type>] [-w] [--stdin] [--] <file>...
1212

1313
DESCRIPTION
1414
-----------
@@ -29,6 +29,9 @@ OPTIONS
2929
-w::
3030
Actually write the object into the object database.
3131

32+
--stdin::
33+
Read the object from standard input instead of from a file.
34+
3235
Author
3336
------
3437
Written by Junio C Hamano <[email protected]>

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
144144
extern int ce_modified(struct cache_entry *ce, struct stat *st);
145145
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
146146
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
147+
extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
147148
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
148149
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
149150

hash-object.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ static void hash_object(const char *path, const char *type, int write_object)
2121
printf("%s\n", sha1_to_hex(sha1));
2222
}
2323

24+
static void hash_stdin(const char *type, int write_object)
25+
{
26+
unsigned char sha1[20];
27+
if (index_pipe(sha1, 0, type, write_object))
28+
die("Unable to add stdin to database");
29+
printf("%s\n", sha1_to_hex(sha1));
30+
}
31+
2432
static const char hash_object_usage[] =
25-
"git-hash-object [-t <type>] [-w] <file>...";
33+
"git-hash-object [-t <type>] [-w] [--stdin] <file>...";
2634

2735
int main(int argc, char **argv)
2836
{
@@ -53,9 +61,12 @@ int main(int argc, char **argv)
5361
}
5462
else if (!strcmp(argv[i], "--help"))
5563
usage(hash_object_usage);
64+
else if (!strcmp(argv[i], "--stdin")) {
65+
hash_stdin(type, write_object);
66+
}
5667
else
5768
die(hash_object_usage);
58-
}
69+
}
5970
else {
6071
const char *arg = argv[i];
6172
if (0 <= prefix_length)

sha1_file.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,40 @@ int has_sha1_file(const unsigned char *sha1)
15281528
return find_sha1_file(sha1, &st) ? 1 : 0;
15291529
}
15301530

1531+
int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
1532+
{
1533+
unsigned long size = 4096;
1534+
char *buf = malloc(size);
1535+
int iret, ret;
1536+
unsigned long off = 0;
1537+
unsigned char hdr[50];
1538+
int hdrlen;
1539+
do {
1540+
iret = read(fd, buf + off, size - off);
1541+
if (iret > 0) {
1542+
off += iret;
1543+
if (off == size) {
1544+
size *= 2;
1545+
buf = realloc(buf, size);
1546+
}
1547+
}
1548+
} while (iret > 0);
1549+
if (iret < 0) {
1550+
free(buf);
1551+
return -1;
1552+
}
1553+
if (!type)
1554+
type = "blob";
1555+
if (write_object)
1556+
ret = write_sha1_file(buf, off, type, sha1);
1557+
else {
1558+
write_sha1_file_prepare(buf, off, type, sha1, hdr, &hdrlen);
1559+
ret = 0;
1560+
}
1561+
free(buf);
1562+
return ret;
1563+
}
1564+
15311565
int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
15321566
{
15331567
unsigned long size = st->st_size;

0 commit comments

Comments
 (0)