blob: b5b8913cb0f2ceae60c18123d979e2948c568151 [file] [log] [blame]
David Barrc0e6c232010-08-09 17:48:10 -05001/*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
5
Florian Achleitnera9a55612012-09-19 17:21:25 +02006#include "cache.h"
David Barr03087972010-12-13 16:41:12 +11007#include "quote.h"
David Barrc0e6c232010-08-09 17:48:10 -05008#include "fast_export.h"
Jonathan Nieder723b7a22010-12-10 04:00:55 -06009#include "strbuf.h"
David Barr7a75e662011-03-19 18:20:54 +110010#include "svndiff.h"
11#include "sliding_window.h"
12#include "line_buffer.h"
David Barrc0e6c232010-08-09 17:48:10 -050013
14#define MAX_GITSVN_LINE_LEN 4096
15
16static uint32_t first_commit_done;
David Barr7a75e662011-03-19 18:20:54 +110017static struct line_buffer postimage = LINE_BUFFER_INIT;
David Barr41529bb2011-03-05 13:30:23 +110018static struct line_buffer report_buffer = LINE_BUFFER_INIT;
David Barrc0e6c232010-08-09 17:48:10 -050019
David Barr7a75e662011-03-19 18:20:54 +110020/* NEEDSWORK: move to fast_export_init() */
21static 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
David Barr41529bb2011-03-05 13:30:23 +110030void fast_export_init(int fd)
David Barrc0e6c232010-08-09 17:48:10 -050031{
Dmitry Ivankovc5bcbcd2011-06-23 17:33:58 +060032 first_commit_done = 0;
David Barr41529bb2011-03-05 13:30:23 +110033 if (buffer_fdinit(&report_buffer, fd))
34 die_errno("cannot read from file descriptor %d", fd);
David Barrc0e6c232010-08-09 17:48:10 -050035}
36
David Barr41529bb2011-03-05 13:30:23 +110037void fast_export_deinit(void)
38{
39 if (buffer_deinit(&report_buffer))
40 die_errno("error closing fast-import feedback stream");
41}
42
David Barr03087972010-12-13 16:41:12 +110043void fast_export_delete(const char *path)
David Barrc0e6c232010-08-09 17:48:10 -050044{
David Barr03087972010-12-13 16:41:12 +110045 putchar('D');
46 putchar(' ');
47 quote_c_style(path, NULL, stdout, 0);
48 putchar('\n');
David Barrc0e6c232010-08-09 17:48:10 -050049}
50
David Barr03087972010-12-13 16:41:12 +110051static void fast_export_truncate(const char *path, uint32_t mode)
Jonathan Nieder723b7a22010-12-10 04:00:55 -060052{
David Barr03087972010-12-13 16:41:12 +110053 fast_export_modify(path, mode, "inline");
Jonathan Nieder723b7a22010-12-10 04:00:55 -060054 printf("data 0\n\n");
55}
56
David Barr03087972010-12-13 16:41:12 +110057void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
David Barrc0e6c232010-08-09 17:48:10 -050058{
59 /* Mode must be 100644, 100755, 120000, or 160000. */
Jonathan Nieder723b7a22010-12-10 04:00:55 -060060 if (!dataref) {
David Barr03087972010-12-13 16:41:12 +110061 fast_export_truncate(path, mode);
Jonathan Nieder723b7a22010-12-10 04:00:55 -060062 return;
63 }
David Barr03087972010-12-13 16:41:12 +110064 printf("M %06"PRIo32" %s ", mode, dataref);
65 quote_c_style(path, NULL, stdout, 0);
66 putchar('\n');
David Barrc0e6c232010-08-09 17:48:10 -050067}
68
Florian Achleitnera9a55612012-09-19 17:21:25 +020069void fast_export_begin_note(uint32_t revision, const char *author,
Johannes Schindelindddbad72017-04-26 21:29:31 +020070 const char *log, timestamp_t timestamp, const char *note_ref)
Florian Achleitnera9a55612012-09-19 17:21:25 +020071{
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020072 static int firstnote = 1;
Florian Achleitnera9a55612012-09-19 17:21:25 +020073 size_t loglen = strlen(log);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020074 printf("commit %s\n", note_ref);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +020075 printf("committer %s <%s@%s> %"PRItime" +0000\n", author, author, "local", timestamp);
Florian Achleitnera9a55612012-09-19 17:21:25 +020076 printf("data %"PRIuMAX"\n", (uintmax_t)loglen);
77 fwrite(log, loglen, 1, stdout);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020078 if (firstnote) {
79 if (revision > 1)
80 printf("from %s^0", note_ref);
81 firstnote = 0;
82 }
Florian Achleitnera9a55612012-09-19 17:21:25 +020083 fputc('\n', stdout);
84}
85
Dmitry Ivankov3c239532012-09-19 17:21:24 +020086void fast_export_note(const char *committish, const char *dataref)
87{
88 printf("N %s %s\n", dataref, committish);
89}
90
David Barrc0e6c232010-08-09 17:48:10 -050091static char gitsvnline[MAX_GITSVN_LINE_LEN];
Jonathan Nieder9ecfa8a2011-05-26 01:51:38 -050092void fast_export_begin_commit(uint32_t revision, const char *author,
Jonathan Nieder195b7ca2011-03-26 00:49:37 -050093 const struct strbuf *log,
David Barr7c5817d2011-03-22 17:52:17 -050094 const char *uuid, const char *url,
Johannes Schindelindddbad72017-04-26 21:29:31 +020095 timestamp_t timestamp, const char *local_ref)
David Barrc0e6c232010-08-09 17:48:10 -050096{
Jonathan Nieder195b7ca2011-03-26 00:49:37 -050097 static const struct strbuf empty = STRBUF_INIT;
David Barrc0e6c232010-08-09 17:48:10 -050098 if (!log)
Jonathan Nieder195b7ca2011-03-26 00:49:37 -050099 log = &empty;
David Barr7c5817d2011-03-22 17:52:17 -0500100 if (*uuid && *url) {
Ramsay Jones5418d962010-09-09 18:24:06 +0100101 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
102 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
David Barr7c5817d2011-03-22 17:52:17 -0500103 url, revision, uuid);
David Barrc0e6c232010-08-09 17:48:10 -0500104 } else {
105 *gitsvnline = '\0';
106 }
Florian Achleitner271fd1f2012-09-19 17:21:22 +0200107 printf("commit %s\n", local_ref);
Jonathan Nieder78e1a3f2010-12-09 18:57:13 -0600108 printf("mark :%"PRIu32"\n", revision);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200109 printf("committer %s <%s@%s> %"PRItime" +0000\n",
David Barr7c5817d2011-03-22 17:52:17 -0500110 *author ? author : "nobody",
111 *author ? author : "nobody",
112 *uuid ? uuid : "local", timestamp);
Jonathan Nieder41e6b912011-03-27 12:19:14 -0500113 printf("data %"PRIuMAX"\n",
114 (uintmax_t) (log->len + strlen(gitsvnline)));
Jonathan Nieder195b7ca2011-03-26 00:49:37 -0500115 fwrite(log->buf, log->len, 1, stdout);
116 printf("%s\n", gitsvnline);
David Barrc0e6c232010-08-09 17:48:10 -0500117 if (!first_commit_done) {
118 if (revision > 1)
David Barrdd3f42a2010-12-12 13:41:38 +1100119 printf("from :%"PRIu32"\n", revision - 1);
David Barrc0e6c232010-08-09 17:48:10 -0500120 first_commit_done = 1;
121 }
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600122}
David Barrc0e6c232010-08-09 17:48:10 -0500123
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600124void fast_export_end_commit(uint32_t revision)
125{
Ramsay Jones5418d962010-09-09 18:24:06 +0100126 printf("progress Imported commit %"PRIu32".\n\n", revision);
David Barrc0e6c232010-08-09 17:48:10 -0500127}
128
David Barr03087972010-12-13 16:41:12 +1100129static void ls_from_rev(uint32_t rev, const char *path)
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600130{
131 /* ls :5 path/to/old/file */
David Barr03087972010-12-13 16:41:12 +1100132 printf("ls :%"PRIu32" ", rev);
133 quote_c_style(path, NULL, stdout, 0);
134 putchar('\n');
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600135 fflush(stdout);
136}
137
David Barr03087972010-12-13 16:41:12 +1100138static void ls_from_active_commit(const char *path)
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600139{
140 /* ls "path/to/file" */
141 printf("ls \"");
David Barr03087972010-12-13 16:41:12 +1100142 quote_c_style(path, NULL, stdout, 1);
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600143 printf("\"\n");
144 fflush(stdout);
145}
146
David Barr41529bb2011-03-05 13:30:23 +1100147static 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
Jonathan Niederc9d1c8b2010-12-28 04:30:54 -0600157static 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
David Barr7a75e662011-03-19 18:20:54 +1100164static int parse_cat_response_line(const char *header, off_t *len)
165{
Jonathan Niederabe27c02011-05-27 05:18:33 -0500166 uintmax_t n;
David Barr7a75e662011-03-19 18:20:54 +1100167 const char *type;
168 const char *end;
169
Christian Couder95662312013-12-01 08:49:16 +0100170 if (ends_with(header, " missing"))
David Barr7a75e662011-03-19 18:20:54 +1100171 return error("cat-blob reports missing blob: %s", header);
David Barr53153e82012-06-01 00:41:28 +1000172 type = strstr(header, " blob ");
David Barr7a75e662011-03-19 18:20:54 +1100173 if (!type)
174 return error("cat-blob header has wrong object type: %s", header);
Jonathan Niederabe27c02011-05-27 05:18:33 -0500175 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
David Barr7a75e662011-03-19 18:20:54 +1100176 if (end == type + strlen(" blob "))
177 return error("cat-blob header does not contain length: %s", header);
Jonathan Niederabe27c02011-05-27 05:18:33 -0500178 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;
David Barr7a75e662011-03-19 18:20:54 +1100183 if (*end)
184 return error("cat-blob header contains garbage after length: %s", header);
185 return 0;
186}
187
Jonathan Niederabe27c02011-05-27 05:18:33 -0500188static 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
David Barr7a75e662011-03-19 18:20:54 +1100194static long apply_delta(off_t len, struct line_buffer *input,
195 const char *old_data, uint32_t old_mode)
196{
197 long ret;
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500198 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
David Barr7a75e662011-03-19 18:20:54 +1100199 FILE *out;
200
201 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
202 die("cannot open temporary file for blob retrieval");
David Barr7a75e662011-03-19 18:20:54 +1100203 if (old_data) {
204 const char *response;
205 printf("cat-blob %s\n", old_data);
206 fflush(stdout);
207 response = get_response_line();
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500208 if (parse_cat_response_line(response, &preimage.max_off))
David Barr7a75e662011-03-19 18:20:54 +1100209 die("invalid cat-blob response: %s", response);
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500210 check_preimage_overflow(preimage.max_off, 1);
David Barr7a75e662011-03-19 18:20:54 +1100211 }
Jonathan Nieder21c7c2d2017-08-22 17:01:34 -0700212 if (old_mode == S_IFLNK) {
David Barr7a75e662011-03-19 18:20:54 +1100213 strbuf_addstr(&preimage.buf, "link ");
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500214 check_preimage_overflow(preimage.max_off, strlen("link "));
215 preimage.max_off += strlen("link ");
216 check_preimage_overflow(preimage.max_off, 1);
David Barr7a75e662011-03-19 18:20:54 +1100217 }
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. */
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500222 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))
David Barr7a75e662011-03-19 18:20:54 +1100225 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
Dmitry Ivankov3c239532012-09-19 17:21:24 +0200236void 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
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600243void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input)
David Barrc0e6c232010-08-09 17:48:10 -0500244{
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600245 assert(len >= 0);
Jonathan Nieder21c7c2d2017-08-22 17:01:34 -0700246 if (mode == S_IFLNK) {
David Barrc0e6c232010-08-09 17:48:10 -0500247 /* svn symlink blobs start with "link " */
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600248 if (len < 5)
249 die("invalid dump: symlink too short for \"link\" prefix");
David Barrc0e6c232010-08-09 17:48:10 -0500250 len -= 5;
Jonathan Niederc9d1c8b2010-12-28 04:30:54 -0600251 if (buffer_skip_bytes(input, 5) != 5)
252 die_short_read(input);
David Barrc0e6c232010-08-09 17:48:10 -0500253 }
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600254 printf("data %"PRIuMAX"\n", (uintmax_t) len);
Jonathan Niederc9d1c8b2010-12-28 04:30:54 -0600255 if (buffer_copy_bytes(input, len) != len)
256 die_short_read(input);
David Barrc0e6c232010-08-09 17:48:10 -0500257 fputc('\n', stdout);
258}
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600259
260static 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. */
David Barr6a0b4432012-06-01 00:41:29 +1000275 if (response_end - response < (signed) strlen("100644") ||
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600276 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 ' */
David Barr6a0b4432012-06-01 00:41:29 +1000288 if (response_end - response < (signed) strlen(" blob ") ||
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600289 (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
David Barr03087972010-12-13 16:41:12 +1100301int fast_export_ls_rev(uint32_t rev, const char *path,
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600302 uint32_t *mode, struct strbuf *dataref)
303{
David Barr03087972010-12-13 16:41:12 +1100304 ls_from_rev(rev, path);
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600305 return parse_ls_response(get_response_line(), mode, dataref);
306}
307
David Barr03087972010-12-13 16:41:12 +1100308int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600309{
David Barr03087972010-12-13 16:41:12 +1100310 ls_from_active_commit(path);
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600311 return parse_ls_response(get_response_line(), mode, dataref);
312}
David Barr7a75e662011-03-19 18:20:54 +1100313
Jonathan Niederb8f43b12017-08-22 17:04:47 -0700314const 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)
Johannes Schindelinc3c34862018-05-02 11:38:41 +0200323 BUG("unexpected fast_export_ls error: %s",
324 strerror(errno));
Jonathan Niederb8f43b12017-08-22 17:04:47 -0700325 /* Treat missing paths as directories. */
326 *mode_out = S_IFDIR;
327 return NULL;
328 }
329 return buf.buf;
330}
331
332void 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)
Johannes Schindelinc3c34862018-05-02 11:38:41 +0200342 BUG("unexpected fast_export_ls_rev error: %s",
343 strerror(errno));
Jonathan Niederb8f43b12017-08-22 17:04:47 -0700344 fast_export_delete(dst);
345 return;
346 }
347 fast_export_modify(dst, mode, data.buf);
348}
349
David Barr7a75e662011-03-19 18:20:54 +1100350void fast_export_blob_delta(uint32_t mode,
351 uint32_t old_mode, const char *old_data,
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600352 off_t len, struct line_buffer *input)
David Barr7a75e662011-03-19 18:20:54 +1100353{
354 long postimage_len;
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600355
356 assert(len >= 0);
357 postimage_len = apply_delta(len, input, old_data, old_mode);
Jonathan Nieder21c7c2d2017-08-22 17:01:34 -0700358 if (mode == S_IFLNK) {
David Barr7a75e662011-03-19 18:20:54 +1100359 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}