@matheusbsbs / matheus-bagliano / commits / 181256442e
Fork 0

Move sha1_file_to_archive into libgit

When the specfile (export-subst) attribute was introduced, it added a dependency from archive-{tar|zip}.c to builtin-archive.c. This broke the support for archive-operations in libgit.a since builtin-archive.o doesn't belong in libgit.a. This patch moves the functions required by libgit.a from builtin-archive.c to the new file archive.c (which becomes part of libgit.a). Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Hjemli committed Jan 14, 2008 at 17:36 UTC 181256442ec1b09d0095e009922e109aa0a4af5e
3 files changed +85 -81
Makefile
+1 -1
@@ -318,7 +318,7 @@ LIB_OBJS = \
318 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
319 color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
320 convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
321 - transport.o bundle.o walker.o parse-options.o ws.o
321 + transport.o bundle.o walker.o parse-options.o ws.o archive.o
322
323 BUILTIN_OBJS = \
324 builtin-add.o \
archive.c new
+84
@@ -0,0 +1,84 @@
1 +#include "cache.h"
2 +#include "commit.h"
3 +#include "attr.h"
4 +
5 +static void format_subst(const struct commit *commit,
6 + const char *src, size_t len,
7 + struct strbuf *buf)
8 +{
9 + char *to_free = NULL;
10 + struct strbuf fmt;
11 +
12 + if (src == buf->buf)
13 + to_free = strbuf_detach(buf, NULL);
14 + strbuf_init(&fmt, 0);
15 + for (;;) {
16 + const char *b, *c;
17 +
18 + b = memmem(src, len, "$Format:", 8);
19 + if (!b || src + len < b + 9)
20 + break;
21 + c = memchr(b + 8, '$', len - 8);
22 + if (!c)
23 + break;
24 +
25 + strbuf_reset(&fmt);
26 + strbuf_add(&fmt, b + 8, c - b - 8);
27 +
28 + strbuf_add(buf, src, b - src);
29 + format_commit_message(commit, fmt.buf, buf);
30 + len -= c + 1 - src;
31 + src = c + 1;
32 + }
33 + strbuf_add(buf, src, len);
34 + strbuf_release(&fmt);
35 + free(to_free);
36 +}
37 +
38 +static int convert_to_archive(const char *path,
39 + const void *src, size_t len,
40 + struct strbuf *buf,
41 + const struct commit *commit)
42 +{
43 + static struct git_attr *attr_export_subst;
44 + struct git_attr_check check[1];
45 +
46 + if (!commit)
47 + return 0;
48 +
49 + if (!attr_export_subst)
50 + attr_export_subst = git_attr("export-subst", 12);
51 +
52 + check[0].attr = attr_export_subst;
53 + if (git_checkattr(path, ARRAY_SIZE(check), check))
54 + return 0;
55 + if (!ATTR_TRUE(check[0].value))
56 + return 0;
57 +
58 + format_subst(commit, src, len, buf);
59 + return 1;
60 +}
61 +
62 +void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
63 + unsigned int mode, enum object_type *type,
64 + unsigned long *sizep,
65 + const struct commit *commit)
66 +{
67 + void *buffer;
68 +
69 + buffer = read_sha1_file(sha1, type, sizep);
70 + if (buffer && S_ISREG(mode)) {
71 + struct strbuf buf;
72 + size_t size = 0;
73 +
74 + strbuf_init(&buf, 0);
75 + strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
76 + convert_to_working_tree(path, buf.buf, buf.len, &buf);
77 + convert_to_archive(path, buf.buf, buf.len, &buf, commit);
78 + buffer = strbuf_detach(&buf, &size);
79 + *sizep = size;
80 + }
81 +
82 + return buffer;
83 +}
84 +
builtin-archive.c
-80
@@ -79,86 +79,6 @@ static int run_remote_archiver(const char *remote, int argc,
79 return !!rv;
80 }
81
82 -static void format_subst(const struct commit *commit,
83 - const char *src, size_t len,
84 - struct strbuf *buf)
85 -{
86 - char *to_free = NULL;
87 - struct strbuf fmt;
88 -
89 - if (src == buf->buf)
90 - to_free = strbuf_detach(buf, NULL);
91 - strbuf_init(&fmt, 0);
92 - for (;;) {
93 - const char *b, *c;
94 -
95 - b = memmem(src, len, "$Format:", 8);
96 - if (!b || src + len < b + 9)
97 - break;
98 - c = memchr(b + 8, '$', len - 8);
99 - if (!c)
100 - break;
101 -
102 - strbuf_reset(&fmt);
103 - strbuf_add(&fmt, b + 8, c - b - 8);
104 -
105 - strbuf_add(buf, src, b - src);
106 - format_commit_message(commit, fmt.buf, buf);
107 - len -= c + 1 - src;
108 - src = c + 1;
109 - }
110 - strbuf_add(buf, src, len);
111 - strbuf_release(&fmt);
112 - free(to_free);
113 -}
114 -
115 -static int convert_to_archive(const char *path,
116 - const void *src, size_t len,
117 - struct strbuf *buf,
118 - const struct commit *commit)
119 -{
120 - static struct git_attr *attr_export_subst;
121 - struct git_attr_check check[1];
122 -
123 - if (!commit)
124 - return 0;
125 -
126 - if (!attr_export_subst)
127 - attr_export_subst = git_attr("export-subst", 12);
128 -
129 - check[0].attr = attr_export_subst;
130 - if (git_checkattr(path, ARRAY_SIZE(check), check))
131 - return 0;
132 - if (!ATTR_TRUE(check[0].value))
133 - return 0;
134 -
135 - format_subst(commit, src, len, buf);
136 - return 1;
137 -}
138 -
139 -void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
140 - unsigned int mode, enum object_type *type,
141 - unsigned long *sizep,
142 - const struct commit *commit)
143 -{
144 - void *buffer;
145 -
146 - buffer = read_sha1_file(sha1, type, sizep);
147 - if (buffer && S_ISREG(mode)) {
148 - struct strbuf buf;
149 - size_t size = 0;
150 -
151 - strbuf_init(&buf, 0);
152 - strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
153 - convert_to_working_tree(path, buf.buf, buf.len, &buf);
154 - convert_to_archive(path, buf.buf, buf.len, &buf, commit);
155 - buffer = strbuf_detach(&buf, &size);
156 - *sizep = size;
157 - }
158 -
159 - return buffer;
160 -}
161 -
82 static int init_archiver(const char *name, struct archiver *ar)
83 {
84 int rv = -1, i;