{"thread":{"id":"11619","subject":"[PATCH] Move sha1_file_to_archive into libgit","startedAt":"2008-01-14T16:36:34Z","lastAt":"2008-01-14T16:36:34Z","messageCount":1,"participants":["Lars Hjemli"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"65322","messageId":"1200328594-17810-1-git-send-email-hjemli@gmail.com","threadId":"11619","inReplyTo":null,"subject":"[PATCH] Move sha1_file_to_archive into libgit","fromName":"Lars Hjemli","fromEmail":"hjemli@gmail.com","sentAt":"2008-01-14T16:36:34Z","receivedAt":"2008-01-14T16:36:34Z","isPatch":true,"sender":{"key":"hjemli@gmail.com","avatar":null},"body":"When the specfile (export-subst) attribute was introduced, it added a\ndependency from archive-{tar|zip}.c to builtin-archive.c. This broke the\nsupport for archive-operations in libgit.a since builtin-archive.o doesn't\nbelong in libgit.a.\n\nThis patch moves the functions required by libgit.a from builtin-archive.c\nto the new file archive.c (which becomes part of libgit.a).\n\nSigned-off-by: Lars Hjemli <hjemli@gmail.com>\n---\n Makefile          |    2 +-\n archive.c         |   84 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n builtin-archive.c |   80 --------------------------------------------------\n 3 files changed, 85 insertions(+), 81 deletions(-)\n create mode 100644 archive.c\n\ndiff --git a/Makefile b/Makefile\nindex 21c80e6..c9b482a 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -316,7 +316,7 @@ LIB_OBJS = \\\n \talloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \\\n \tcolor.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \\\n \tconvert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \\\n-\ttransport.o bundle.o walker.o parse-options.o ws.o\n+\ttransport.o bundle.o walker.o parse-options.o ws.o archive.o\n \n BUILTIN_OBJS = \\\n \tbuiltin-add.o \\\ndiff --git a/archive.c b/archive.c\nnew file mode 100644\nindex 0000000..fb159fe\n--- /dev/null\n+++ b/archive.c\n@@ -0,0 +1,84 @@\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"attr.h\"\n+\n+static void format_subst(const struct commit *commit,\n+                         const char *src, size_t len,\n+                         struct strbuf *buf)\n+{\n+\tchar *to_free = NULL;\n+\tstruct strbuf fmt;\n+\n+\tif (src == buf->buf)\n+\t\tto_free = strbuf_detach(buf, NULL);\n+\tstrbuf_init(&fmt, 0);\n+\tfor (;;) {\n+\t\tconst char *b, *c;\n+\n+\t\tb = memmem(src, len, \"$Format:\", 8);\n+\t\tif (!b || src + len < b + 9)\n+\t\t\tbreak;\n+\t\tc = memchr(b + 8, '$', len - 8);\n+\t\tif (!c)\n+\t\t\tbreak;\n+\n+\t\tstrbuf_reset(&fmt);\n+\t\tstrbuf_add(&fmt, b + 8, c - b - 8);\n+\n+\t\tstrbuf_add(buf, src, b - src);\n+\t\tformat_commit_message(commit, fmt.buf, buf);\n+\t\tlen -= c + 1 - src;\n+\t\tsrc  = c + 1;\n+\t}\n+\tstrbuf_add(buf, src, len);\n+\tstrbuf_release(&fmt);\n+\tfree(to_free);\n+}\n+\n+static int convert_to_archive(const char *path,\n+                              const void *src, size_t len,\n+                              struct strbuf *buf,\n+                              const struct commit *commit)\n+{\n+\tstatic struct git_attr *attr_export_subst;\n+\tstruct git_attr_check check[1];\n+\n+\tif (!commit)\n+\t\treturn 0;\n+\n+\tif (!attr_export_subst)\n+\t\tattr_export_subst = git_attr(\"export-subst\", 12);\n+\n+\tcheck[0].attr = attr_export_subst;\n+\tif (git_checkattr(path, ARRAY_SIZE(check), check))\n+\t\treturn 0;\n+\tif (!ATTR_TRUE(check[0].value))\n+\t\treturn 0;\n+\n+\tformat_subst(commit, src, len, buf);\n+\treturn 1;\n+}\n+\n+void *sha1_file_to_archive(const char *path, const unsigned char *sha1,\n+                           unsigned int mode, enum object_type *type,\n+                           unsigned long *sizep,\n+                           const struct commit *commit)\n+{\n+\tvoid *buffer;\n+\n+\tbuffer = read_sha1_file(sha1, type, sizep);\n+\tif (buffer && S_ISREG(mode)) {\n+\t\tstruct strbuf buf;\n+\t\tsize_t size = 0;\n+\n+\t\tstrbuf_init(&buf, 0);\n+\t\tstrbuf_attach(&buf, buffer, *sizep, *sizep + 1);\n+\t\tconvert_to_working_tree(path, buf.buf, buf.len, &buf);\n+\t\tconvert_to_archive(path, buf.buf, buf.len, &buf, commit);\n+\t\tbuffer = strbuf_detach(&buf, &size);\n+\t\t*sizep = size;\n+\t}\n+\n+\treturn buffer;\n+}\n+\ndiff --git a/builtin-archive.c b/builtin-archive.c\nindex 14a1b30..c2e0c1e 100644\n--- a/builtin-archive.c\n+++ b/builtin-archive.c\n@@ -79,86 +79,6 @@ static int run_remote_archiver(const char *remote, int argc,\n \treturn !!rv;\n }\n \n-static void format_subst(const struct commit *commit,\n-                         const char *src, size_t len,\n-                         struct strbuf *buf)\n-{\n-\tchar *to_free = NULL;\n-\tstruct strbuf fmt;\n-\n-\tif (src == buf->buf)\n-\t\tto_free = strbuf_detach(buf, NULL);\n-\tstrbuf_init(&fmt, 0);\n-\tfor (;;) {\n-\t\tconst char *b, *c;\n-\n-\t\tb = memmem(src, len, \"$Format:\", 8);\n-\t\tif (!b || src + len < b + 9)\n-\t\t\tbreak;\n-\t\tc = memchr(b + 8, '$', len - 8);\n-\t\tif (!c)\n-\t\t\tbreak;\n-\n-\t\tstrbuf_reset(&fmt);\n-\t\tstrbuf_add(&fmt, b + 8, c - b - 8);\n-\n-\t\tstrbuf_add(buf, src, b - src);\n-\t\tformat_commit_message(commit, fmt.buf, buf);\n-\t\tlen -= c + 1 - src;\n-\t\tsrc  = c + 1;\n-\t}\n-\tstrbuf_add(buf, src, len);\n-\tstrbuf_release(&fmt);\n-\tfree(to_free);\n-}\n-\n-static int convert_to_archive(const char *path,\n-                              const void *src, size_t len,\n-                              struct strbuf *buf,\n-                              const struct commit *commit)\n-{\n-\tstatic struct git_attr *attr_export_subst;\n-\tstruct git_attr_check check[1];\n-\n-\tif (!commit)\n-\t\treturn 0;\n-\n-\tif (!attr_export_subst)\n-\t\tattr_export_subst = git_attr(\"export-subst\", 12);\n-\n-\tcheck[0].attr = attr_export_subst;\n-\tif (git_checkattr(path, ARRAY_SIZE(check), check))\n-\t\treturn 0;\n-\tif (!ATTR_TRUE(check[0].value))\n-\t\treturn 0;\n-\n-\tformat_subst(commit, src, len, buf);\n-\treturn 1;\n-}\n-\n-void *sha1_file_to_archive(const char *path, const unsigned char *sha1,\n-                           unsigned int mode, enum object_type *type,\n-                           unsigned long *sizep,\n-                           const struct commit *commit)\n-{\n-\tvoid *buffer;\n-\n-\tbuffer = read_sha1_file(sha1, type, sizep);\n-\tif (buffer && S_ISREG(mode)) {\n-\t\tstruct strbuf buf;\n-\t\tsize_t size = 0;\n-\n-\t\tstrbuf_init(&buf, 0);\n-\t\tstrbuf_attach(&buf, buffer, *sizep, *sizep + 1);\n-\t\tconvert_to_working_tree(path, buf.buf, buf.len, &buf);\n-\t\tconvert_to_archive(path, buf.buf, buf.len, &buf, commit);\n-\t\tbuffer = strbuf_detach(&buf, &size);\n-\t\t*sizep = size;\n-\t}\n-\n-\treturn buffer;\n-}\n-\n static int init_archiver(const char *name, struct archiver *ar)\n {\n \tint rv = -1, i;\n-- \n1.5.4.rc2.69.g047fe-dirty\n"}]}