{"thread":{"id":"2795","subject":"[PATCH] Allow saving an object from a pipe","startedAt":"2005-12-10T22:25:24Z","lastAt":"2005-12-12T17:12:12Z","messageCount":3,"participants":["Daniel Barkalow","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"13463","messageId":"Pine.LNX.4.64.0512101724290.25300@iabervon.org","threadId":"2795","inReplyTo":null,"subject":"[PATCH] Allow saving an object from a pipe","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2005-12-10T22:25:24Z","receivedAt":"2005-12-10T22:25:24Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"In order to support getting data into git with scripts, this adds a\n--stdin option to git-hash-object, which will make it read from stdin.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n\n---\n\n Documentation/git-hash-object.txt |    3 +++\n cache.h                           |    1 +\n hash-object.c                     |   15 +++++++++++++--\n sha1_file.c                       |   34 ++++++++++++++++++++++++++++++++++\n 4 files changed, 51 insertions(+), 2 deletions(-)\n\nfac4c3a5c27839006e5d086ef282b252bd5f390e\ndiff --git a/Documentation/git-hash-object.txt b/Documentation/git-hash-object.txt\nindex db12e92..eaac4c9 100644\n--- a/Documentation/git-hash-object.txt\n+++ b/Documentation/git-hash-object.txt\n@@ -29,6 +29,9 @@ OPTIONS\n -w::\n \tActually write the object into the object database.\n \n+--stdin::\n+\tRead the object from standard input instead of from a file.\n+\n Author\n ------\n Written by Junio C Hamano <junkio@cox.net>\ndiff --git a/cache.h b/cache.h\nindex 86fc250..c78d8ae 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -144,6 +144,7 @@ extern int ce_match_stat(struct cache_en\n extern int ce_modified(struct cache_entry *ce, struct stat *st);\n extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);\n extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);\n+extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);\n extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);\n extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);\n \ndiff --git a/hash-object.c b/hash-object.c\nindex 6227936..6502b5b 100644\n--- a/hash-object.c\n+++ b/hash-object.c\n@@ -21,8 +21,16 @@ static void hash_object(const char *path\n \tprintf(\"%s\\n\", sha1_to_hex(sha1));\n }\n \n+static void hash_stdin(const char *type, int write_object)\n+{\n+\tunsigned char sha1[20];\n+\tif (index_pipe(sha1, 0, type, write_object))\n+\t\tdie(\"Unable to add stdin to database\");\n+\tprintf(\"%s\\n\", sha1_to_hex(sha1));\n+}\n+\n static const char hash_object_usage[] =\n-\"git-hash-object [-t <type>] [-w] <file>...\";\n+\"git-hash-object [-t <type>] [-w] [--stdin] <file>...\";\n \n int main(int argc, char **argv)\n {\n@@ -53,9 +61,12 @@ int main(int argc, char **argv)\n \t\t\t}\n \t\t\telse if (!strcmp(argv[i], \"--help\"))\n \t\t\t\tusage(hash_object_usage);\n+\t\t\telse if (!strcmp(argv[i], \"--stdin\")) {\n+\t\t\t\thash_stdin(type, write_object);\n+\t\t\t}\n \t\t\telse\n \t\t\t\tdie(hash_object_usage);\n-\t\t}\n+\t\t} \n \t\telse {\n \t\t\tconst char *arg = argv[i];\n \t\t\tif (0 <= prefix_length)\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 111a71d..fa22e9c 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1528,6 +1528,40 @@ int has_sha1_file(const unsigned char *s\n \treturn find_sha1_file(sha1, &st) ? 1 : 0;\n }\n \n+int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)\n+{\n+\tunsigned long size = 4096;\n+\tchar *buf = malloc(size);\n+\tint iret, ret;\n+\tunsigned long off = 0;\n+\tunsigned char hdr[50];\n+\tint hdrlen;\n+\tdo {\n+\t\tiret = read(fd, buf + off, size - off);\n+\t\tif (iret > 0) {\n+\t\t\toff += iret;\n+\t\t\tif (off == size) {\n+\t\t\t\tsize *= 2;\n+\t\t\t\tbuf = realloc(buf, size);\n+\t\t\t}\n+\t\t}\n+\t} while (iret > 0);\n+\tif (iret < 0) {\n+\t\tfree(buf);\n+\t\treturn -1;\n+\t}\n+\tif (!type)\n+\t\ttype = \"blob\";\n+\tif (write_object)\n+\t\tret = write_sha1_file(buf, off, type, sha1);\n+\telse {\n+\t\twrite_sha1_file_prepare(buf, off, type, sha1, hdr, &hdrlen);\n+\t\tret = 0;\n+\t}\n+\tfree(buf);\n+\treturn ret;\n+}\n+\n int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)\n {\n \tunsigned long size = st->st_size;\n-- \n0.99.9.GIT\n"},{"id":"13470","messageId":"7v64pwuyny.fsf@assigned-by-dhcp.cox.net","threadId":"2795","inReplyTo":"Pine.LNX.4.64.0512101724290.25300@iabervon.org","subject":"Re: [PATCH] Allow saving an object from a pipe","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-12-11T02:59:13Z","receivedAt":"2005-12-11T02:59:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Daniel Barkalow <barkalow@iabervon.org> writes:\n\n> In order to support getting data into git with scripts, this adds a\n> --stdin option to git-hash-object, which will make it read from stdin.\n\nThanks, will apply.\n\nTo be honest, though, I am still debating myself about the merit\nof not having to have a temporary file.  Because we need the\nsize of the blob available before starting to hash (i.e. we\ncannot say \"atend\"), index_pipe ends up keeping the whole blob\ndata in memory, which is not much better than the caller storing\nit in a temporary file and driving the normal hash-object from\nthe command line anyway.\n"},{"id":"13515","messageId":"Pine.LNX.4.64.0512121210010.25300@iabervon.org","threadId":"2795","inReplyTo":"7v64pwuyny.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] Allow saving an object from a pipe","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2005-12-12T17:12:12Z","receivedAt":"2005-12-12T17:12:12Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Sat, 10 Dec 2005, Junio C Hamano wrote:\n\n> Daniel Barkalow <barkalow@iabervon.org> writes:\n> \n> > In order to support getting data into git with scripts, this adds a\n> > --stdin option to git-hash-object, which will make it read from stdin.\n> \n> Thanks, will apply.\n> \n> To be honest, though, I am still debating myself about the merit\n> of not having to have a temporary file.  Because we need the\n> size of the blob available before starting to hash (i.e. we\n> cannot say \"atend\"), index_pipe ends up keeping the whole blob\n> data in memory, which is not much better than the caller storing\n> it in a temporary file and driving the normal hash-object from\n> the command line anyway.\n\nIt's not much better for efficiency, but it's a whole lot easier to write \nthe script without having to worry about a temporary file, what could \nhappen to it while you're trying to use it, and making sure it gets \ncleaned up afterwards. It can also save on disk writes, depending on where \nyou put it and how the system is configured.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"}]}