Commit | Line | Data |
---|---|---|
c0e6c23d DB |
1 | /* |
2 | * Licensed under a two-clause BSD-style license. | |
3 | * See LICENSE for details. | |
4 | */ | |
5 | ||
a9a55613 | 6 | #include "cache.h" |
03087971 | 7 | #include "quote.h" |
c0e6c23d | 8 | #include "fast_export.h" |
723b7a27 | 9 | #include "strbuf.h" |
7a75e661 DB |
10 | #include "svndiff.h" |
11 | #include "sliding_window.h" | |
12 | #include "line_buffer.h" | |
c0e6c23d DB |
13 | |
14 | #define MAX_GITSVN_LINE_LEN 4096 | |
15 | ||
16 | static uint32_t first_commit_done; | |
7a75e661 | 17 | static struct line_buffer postimage = LINE_BUFFER_INIT; |
41529bbc | 18 | static struct line_buffer report_buffer = LINE_BUFFER_INIT; |
c0e6c23d | 19 | |
7a75e661 DB |
20 | /* NEEDSWORK: move to fast_export_init() */ |
21 | static int init_postimage(void) | |
22 | { | |
23 | static int postimage_initialized; | |
24 | if (postimage_initialized) | |
25 | return 0; | |
26 | postimage_initialized = 1; | |
27 | return buffer_tmpfile_init(&postimage); | |
28 | } | |
29 | ||
41529bbc | 30 | void fast_export_init(int fd) |
c0e6c23d | 31 | { |
c5bcbcdc | 32 | first_commit_done = 0; |
41529bbc DB |
33 | if (buffer_fdinit(&report_buffer, fd)) |
34 | die_errno("cannot read from file descriptor %d", fd); | |
c0e6c23d DB |
35 | } |
36 | ||
41529bbc DB |
37 | void fast_export_deinit(void) |
38 | { | |
39 | if (buffer_deinit(&report_buffer)) | |
40 | die_errno("error closing fast-import feedback stream"); | |
41 | } | |
42 | ||
03087971 | 43 | void fast_export_delete(const char *path) |
c0e6c23d | 44 | { |
03087971 DB |
45 | putchar('D'); |
46 | putchar(' '); | |
47 | quote_c_style(path, NULL, stdout, 0); | |
48 | putchar('\n'); | |
c0e6c23d DB |
49 | } |
50 | ||
03087971 | 51 | static void fast_export_truncate(const char *path, uint32_t mode) |
c0e6c23d | 52 | { |
03087971 | 53 | fast_export_modify(path, mode, "inline"); |
723b7a27 | 54 | printf("data 0\n\n"); |
c0e6c23d DB |
55 | } |
56 | ||
03087971 | 57 | void fast_export_modify(const char *path, uint32_t mode, const char *dataref) |
c0e6c23d DB |
58 | { |
59 | /* Mode must be 100644, 100755, 120000, or 160000. */ | |
723b7a27 | 60 | if (!dataref) { |
03087971 | 61 | fast_export_truncate(path, mode); |
723b7a27 JN |
62 | return; |
63 | } | |
03087971 DB |
64 | printf("M %06"PRIo32" %s ", mode, dataref); |
65 | quote_c_style(path, NULL, stdout, 0); | |
66 | putchar('\n'); | |
c0e6c23d DB |
67 | } |
68 | ||
a9a55613 | 69 | void fast_export_begin_note(uint32_t revision, const char *author, |
dddbad72 | 70 | const char *log, timestamp_t timestamp, const char *note_ref) |
a9a55613 | 71 | { |
8e43a1d0 | 72 | static int firstnote = 1; |
a9a55613 | 73 | size_t loglen = strlen(log); |
8e43a1d0 | 74 | printf("commit %s\n", note_ref); |
cb71f8bd | 75 | printf("committer %s <%s@%s> %"PRItime" +0000\n", author, author, "local", timestamp); |
a9a55613 FA |
76 | printf("data %"PRIuMAX"\n", (uintmax_t)loglen); |
77 | fwrite(log, loglen, 1, stdout); | |
8e43a1d0 FA |
78 | if (firstnote) { |
79 | if (revision > 1) | |
80 | printf("from %s^0", note_ref); | |
81 | firstnote = 0; | |
82 | } | |
a9a55613 FA |
83 | fputc('\n', stdout); |
84 | } | |
85 | ||
3c23953f DI |
86 | void fast_export_note(const char *committish, const char *dataref) |
87 | { | |
88 | printf("N %s %s\n", dataref, committish); | |
89 | } | |
90 | ||
c0e6c23d | 91 | static char gitsvnline[MAX_GITSVN_LINE_LEN]; |
9ecfa8ae | 92 | void fast_export_begin_commit(uint32_t revision, const char *author, |
195b7ca6 | 93 | const struct strbuf *log, |
7c5817d3 | 94 | const char *uuid, const char *url, |
dddbad72 | 95 | timestamp_t timestamp, const char *local_ref) |
c0e6c23d | 96 | { |
195b7ca6 | 97 | static const struct strbuf empty = STRBUF_INIT; |
c0e6c23d | 98 | if (!log) |
195b7ca6 | 99 | log = ∅ |
7c5817d3 | 100 | if (*uuid && *url) { |
5418d96d RJ |
101 | snprintf(gitsvnline, MAX_GITSVN_LINE_LEN, |
102 | "\n\ngit-svn-id: %s@%"PRIu32" %s\n", | |
7c5817d3 | 103 | url, revision, uuid); |
c0e6c23d DB |
104 | } else { |
105 | *gitsvnline = '\0'; | |
106 | } | |
271fd1fc | 107 | printf("commit %s\n", local_ref); |
78e1a3ff | 108 | printf("mark :%"PRIu32"\n", revision); |
cb71f8bd | 109 | printf("committer %s <%s@%s> %"PRItime" +0000\n", |
7c5817d3 DB |
110 | *author ? author : "nobody", |
111 | *author ? author : "nobody", | |
112 | *uuid ? uuid : "local", timestamp); | |
41e6b91f JN |
113 | printf("data %"PRIuMAX"\n", |
114 | (uintmax_t) (log->len + strlen(gitsvnline))); | |
195b7ca6 JN |
115 | fwrite(log->buf, log->len, 1, stdout); |
116 | printf("%s\n", gitsvnline); | |
c0e6c23d DB |
117 | if (!first_commit_done) { |
118 | if (revision > 1) | |
dd3f42ad | 119 | printf("from :%"PRIu32"\n", revision - 1); |
c0e6c23d DB |
120 | first_commit_done = 1; |
121 | } | |
723b7a27 | 122 | } |
c0e6c23d | 123 | |
723b7a27 JN |
124 | void fast_export_end_commit(uint32_t revision) |
125 | { | |
5418d96d | 126 | printf("progress Imported commit %"PRIu32".\n\n", revision); |
c0e6c23d DB |
127 | } |
128 | ||
03087971 | 129 | static void ls_from_rev(uint32_t rev, const char *path) |
723b7a27 JN |
130 | { |
131 | /* ls :5 path/to/old/file */ | |
03087971 DB |
132 | printf("ls :%"PRIu32" ", rev); |
133 | quote_c_style(path, NULL, stdout, 0); | |
134 | putchar('\n'); | |
723b7a27 JN |
135 | fflush(stdout); |
136 | } | |
137 | ||
03087971 | 138 | static void ls_from_active_commit(const char *path) |
723b7a27 JN |
139 | { |
140 | /* ls "path/to/file" */ | |
141 | printf("ls \""); | |
03087971 | 142 | quote_c_style(path, NULL, stdout, 1); |
723b7a27 JN |
143 | printf("\"\n"); |
144 | fflush(stdout); | |
145 | } | |
146 | ||
41529bbc DB |
147 | static const char *get_response_line(void) |
148 | { | |
149 | const char *line = buffer_read_line(&report_buffer); | |
150 | if (line) | |
151 | return line; | |
152 | if (buffer_ferror(&report_buffer)) | |
153 | die_errno("error reading from fast-import"); | |
154 | die("unexpected end of fast-import feedback"); | |
155 | } | |
156 | ||
c9d1c8ba JN |
157 | static void die_short_read(struct line_buffer *input) |
158 | { | |
159 | if (buffer_ferror(input)) | |
160 | die_errno("error reading dump file"); | |
161 | die("invalid dump: unexpected end of file"); | |
162 | } | |
163 | ||
7a75e661 DB |
164 | static int parse_cat_response_line(const char *header, off_t *len) |
165 | { | |
abe27c0c | 166 | uintmax_t n; |
7a75e661 DB |
167 | const char *type; |
168 | const char *end; | |
169 | ||
95662315 | 170 | if (ends_with(header, " missing")) |
7a75e661 | 171 | return error("cat-blob reports missing blob: %s", header); |
53153e83 | 172 | type = strstr(header, " blob "); |
7a75e661 DB |
173 | if (!type) |
174 | return error("cat-blob header has wrong object type: %s", header); | |
abe27c0c | 175 | n = strtoumax(type + strlen(" blob "), (char **) &end, 10); |
7a75e661 DB |
176 | if (end == type + strlen(" blob ")) |
177 | return error("cat-blob header does not contain length: %s", header); | |
abe27c0c JN |
178 | if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob "))) |
179 | return error("cat-blob header contains negative length: %s", header); | |
180 | if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t)) | |
181 | return error("blob too large for current definition of off_t"); | |
182 | *len = n; | |
7a75e661 DB |
183 | if (*end) |
184 | return error("cat-blob header contains garbage after length: %s", header); | |
185 | return 0; | |
186 | } | |
187 | ||
abe27c0c JN |
188 | static void check_preimage_overflow(off_t a, off_t b) |
189 | { | |
190 | if (signed_add_overflows(a, b)) | |
191 | die("blob too large for current definition of off_t"); | |
192 | } | |
193 | ||
7a75e661 DB |
194 | static long apply_delta(off_t len, struct line_buffer *input, |
195 | const char *old_data, uint32_t old_mode) | |
196 | { | |
197 | long ret; | |
3ac10b2e | 198 | struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0); |
7a75e661 DB |
199 | FILE *out; |
200 | ||
201 | if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage))) | |
202 | die("cannot open temporary file for blob retrieval"); | |
7a75e661 DB |
203 | if (old_data) { |
204 | const char *response; | |
205 | printf("cat-blob %s\n", old_data); | |
206 | fflush(stdout); | |
207 | response = get_response_line(); | |
3ac10b2e | 208 | if (parse_cat_response_line(response, &preimage.max_off)) |
7a75e661 | 209 | die("invalid cat-blob response: %s", response); |
3ac10b2e | 210 | check_preimage_overflow(preimage.max_off, 1); |
7a75e661 | 211 | } |
21c7c2d9 | 212 | if (old_mode == S_IFLNK) { |
7a75e661 | 213 | strbuf_addstr(&preimage.buf, "link "); |
3ac10b2e JN |
214 | check_preimage_overflow(preimage.max_off, strlen("link ")); |
215 | preimage.max_off += strlen("link "); | |
216 | check_preimage_overflow(preimage.max_off, 1); | |
7a75e661 DB |
217 | } |
218 | if (svndiff0_apply(input, len, &preimage, out)) | |
219 | die("cannot apply delta"); | |
220 | if (old_data) { | |
221 | /* Read the remainder of preimage and trailing newline. */ | |
3ac10b2e JN |
222 | assert(!signed_add_overflows(preimage.max_off, 1)); |
223 | preimage.max_off++; /* room for newline */ | |
224 | if (move_window(&preimage, preimage.max_off - 1, 1)) | |
7a75e661 DB |
225 | die("cannot seek to end of input"); |
226 | if (preimage.buf.buf[0] != '\n') | |
227 | die("missing newline after cat-blob response"); | |
228 | } | |
229 | ret = buffer_tmpfile_prepare_to_read(&postimage); | |
230 | if (ret < 0) | |
231 | die("cannot read temporary file for blob retrieval"); | |
232 | strbuf_release(&preimage.buf); | |
233 | return ret; | |
234 | } | |
235 | ||
3c23953f DI |
236 | void fast_export_buf_to_data(const struct strbuf *data) |
237 | { | |
238 | printf("data %"PRIuMAX"\n", (uintmax_t)data->len); | |
239 | fwrite(data->buf, data->len, 1, stdout); | |
240 | fputc('\n', stdout); | |
241 | } | |
242 | ||
2d54b9ea | 243 | void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input) |
c0e6c23d | 244 | { |
2d54b9ea | 245 | assert(len >= 0); |
21c7c2d9 | 246 | if (mode == S_IFLNK) { |
c0e6c23d | 247 | /* svn symlink blobs start with "link " */ |
2d54b9ea JN |
248 | if (len < 5) |
249 | die("invalid dump: symlink too short for \"link\" prefix"); | |
c0e6c23d | 250 | len -= 5; |
c9d1c8ba JN |
251 | if (buffer_skip_bytes(input, 5) != 5) |
252 | die_short_read(input); | |
c0e6c23d | 253 | } |
2d54b9ea | 254 | printf("data %"PRIuMAX"\n", (uintmax_t) len); |
c9d1c8ba JN |
255 | if (buffer_copy_bytes(input, len) != len) |
256 | die_short_read(input); | |
c0e6c23d DB |
257 | fputc('\n', stdout); |
258 | } | |
723b7a27 JN |
259 | |
260 | static int parse_ls_response(const char *response, uint32_t *mode, | |
261 | struct strbuf *dataref) | |
262 | { | |
263 | const char *tab; | |
264 | const char *response_end; | |
265 | ||
266 | assert(response); | |
267 | response_end = response + strlen(response); | |
268 | ||
269 | if (*response == 'm') { /* Missing. */ | |
270 | errno = ENOENT; | |
271 | return -1; | |
272 | } | |
273 | ||
274 | /* Mode. */ | |
6a0b4438 | 275 | if (response_end - response < (signed) strlen("100644") || |
723b7a27 JN |
276 | response[strlen("100644")] != ' ') |
277 | die("invalid ls response: missing mode: %s", response); | |
278 | *mode = 0; | |
279 | for (; *response != ' '; response++) { | |
280 | char ch = *response; | |
281 | if (ch < '0' || ch > '7') | |
282 | die("invalid ls response: mode is not octal: %s", response); | |
283 | *mode *= 8; | |
284 | *mode += ch - '0'; | |
285 | } | |
286 | ||
287 | /* ' blob ' or ' tree ' */ | |
6a0b4438 | 288 | if (response_end - response < (signed) strlen(" blob ") || |
723b7a27 JN |
289 | (response[1] != 'b' && response[1] != 't')) |
290 | die("unexpected ls response: not a tree or blob: %s", response); | |
291 | response += strlen(" blob "); | |
292 | ||
293 | /* Dataref. */ | |
294 | tab = memchr(response, '\t', response_end - response); | |
295 | if (!tab) | |
296 | die("invalid ls response: missing tab: %s", response); | |
297 | strbuf_add(dataref, response, tab - response); | |
298 | return 0; | |
299 | } | |
300 | ||
03087971 | 301 | int fast_export_ls_rev(uint32_t rev, const char *path, |
723b7a27 JN |
302 | uint32_t *mode, struct strbuf *dataref) |
303 | { | |
03087971 | 304 | ls_from_rev(rev, path); |
723b7a27 JN |
305 | return parse_ls_response(get_response_line(), mode, dataref); |
306 | } | |
307 | ||
03087971 | 308 | int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref) |
723b7a27 | 309 | { |
03087971 | 310 | ls_from_active_commit(path); |
723b7a27 JN |
311 | return parse_ls_response(get_response_line(), mode, dataref); |
312 | } | |
7a75e661 | 313 | |
b8f43b12 JN |
314 | const char *fast_export_read_path(const char *path, uint32_t *mode_out) |
315 | { | |
316 | int err; | |
317 | static struct strbuf buf = STRBUF_INIT; | |
318 | ||
319 | strbuf_reset(&buf); | |
320 | err = fast_export_ls(path, mode_out, &buf); | |
321 | if (err) { | |
322 | if (errno != ENOENT) | |
c3c3486b JS |
323 | BUG("unexpected fast_export_ls error: %s", |
324 | strerror(errno)); | |
b8f43b12 JN |
325 | /* Treat missing paths as directories. */ |
326 | *mode_out = S_IFDIR; | |
327 | return NULL; | |
328 | } | |
329 | return buf.buf; | |
330 | } | |
331 | ||
332 | void fast_export_copy(uint32_t revision, const char *src, const char *dst) | |
333 | { | |
334 | int err; | |
335 | uint32_t mode; | |
336 | static struct strbuf data = STRBUF_INIT; | |
337 | ||
338 | strbuf_reset(&data); | |
339 | err = fast_export_ls_rev(revision, src, &mode, &data); | |
340 | if (err) { | |
341 | if (errno != ENOENT) | |
c3c3486b JS |
342 | BUG("unexpected fast_export_ls_rev error: %s", |
343 | strerror(errno)); | |
b8f43b12 JN |
344 | fast_export_delete(dst); |
345 | return; | |
346 | } | |
347 | fast_export_modify(dst, mode, data.buf); | |
348 | } | |
349 | ||
7a75e661 DB |
350 | void fast_export_blob_delta(uint32_t mode, |
351 | uint32_t old_mode, const char *old_data, | |
2d54b9ea | 352 | off_t len, struct line_buffer *input) |
7a75e661 DB |
353 | { |
354 | long postimage_len; | |
2d54b9ea JN |
355 | |
356 | assert(len >= 0); | |
357 | postimage_len = apply_delta(len, input, old_data, old_mode); | |
21c7c2d9 | 358 | if (mode == S_IFLNK) { |
7a75e661 DB |
359 | buffer_skip_bytes(&postimage, strlen("link ")); |
360 | postimage_len -= strlen("link "); | |
361 | } | |
362 | printf("data %ld\n", postimage_len); | |
363 | buffer_copy_bytes(&postimage, postimage_len); | |
364 | fputc('\n', stdout); | |
365 | } |