@matheusbsbs / matheus-bagliano / commits / 024510c8d9
Fork 0

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 <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Daniel Barkalow committed Dec 10, 2005 at 17:25 UTC 024510c8d947be6ae4765840e21a89d5a21271c4
4 files changed +52 -3
Documentation/git-hash-object.txt
+4 -1
@@ -8,7 +8,7 @@ git-hash-object - Computes object ID and optionally creates a blob from a file.
8
9 SYNOPSIS
10 --------
11 -'git-hash-object' [-t <type>] [-w] <file>
11 +'git-hash-object' [-t <type>] [-w] [--stdin] [--] <file>...
12
13 DESCRIPTION
14 -----------
@@ -29,6 +29,9 @@ OPTIONS
29 -w::
30 Actually write the object into the object database.
31
32 +--stdin::
33 + Read the object from standard input instead of from a file.
34 +
35 Author
36 ------
37 Written by Junio C Hamano <junkio@cox.net>
cache.h
+1
@@ -144,6 +144,7 @@ extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
144 extern int ce_modified(struct cache_entry *ce, struct stat *st);
145 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
146 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);
148 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
149 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
150
hash-object.c
+13 -2
@@ -21,8 +21,16 @@ static void hash_object(const char *path, const char *type, int write_object)
21 printf("%s\n", sha1_to_hex(sha1));
22 }
23
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 +
32 static const char hash_object_usage[] =
25 -"git-hash-object [-t <type>] [-w] <file>...";
33 +"git-hash-object [-t <type>] [-w] [--stdin] <file>...";
34
35 int main(int argc, char **argv)
36 {
@@ -53,9 +61,12 @@ int main(int argc, char **argv)
61 }
62 else if (!strcmp(argv[i], "--help"))
63 usage(hash_object_usage);
64 + else if (!strcmp(argv[i], "--stdin")) {
65 + hash_stdin(type, write_object);
66 + }
67 else
68 die(hash_object_usage);
58 - }
69 + }
70 else {
71 const char *arg = argv[i];
72 if (0 <= prefix_length)
sha1_file.c
+34
@@ -1528,6 +1528,40 @@ int has_sha1_file(const unsigned char *sha1)
1528 return find_sha1_file(sha1, &st) ? 1 : 0;
1529 }
1530
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 +
1565 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1566 {
1567 unsigned long size = st->st_size;