blob: bd0f2c2b86847708e5ebe668773f0fd08b9efc5d [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"
David Barrc0e6c232010-08-09 17:48:10 -05009#include "repo_tree.h"
Jonathan Nieder723b7a22010-12-10 04:00:55 -060010#include "strbuf.h"
David Barr7a75e662011-03-19 18:20:54 +110011#include "svndiff.h"
12#include "sliding_window.h"
13#include "line_buffer.h"
David Barrc0e6c232010-08-09 17:48:10 -050014
15#define MAX_GITSVN_LINE_LEN 4096
16
17static uint32_t first_commit_done;
David Barr7a75e662011-03-19 18:20:54 +110018static struct line_buffer postimage = LINE_BUFFER_INIT;
David Barr41529bb2011-03-05 13:30:23 +110019static struct line_buffer report_buffer = LINE_BUFFER_INIT;
David Barrc0e6c232010-08-09 17:48:10 -050020
David Barr7a75e662011-03-19 18:20:54 +110021/* NEEDSWORK: move to fast_export_init() */
22static int init_postimage(void)
23{
24 static int postimage_initialized;
25 if (postimage_initialized)
26 return 0;
27 postimage_initialized = 1;
28 return buffer_tmpfile_init(&postimage);
29}
30
David Barr41529bb2011-03-05 13:30:23 +110031void fast_export_init(int fd)
David Barrc0e6c232010-08-09 17:48:10 -050032{
Dmitry Ivankovc5bcbcd2011-06-23 17:33:58 +060033 first_commit_done = 0;
David Barr41529bb2011-03-05 13:30:23 +110034 if (buffer_fdinit(&report_buffer, fd))
35 die_errno("cannot read from file descriptor %d", fd);
David Barrc0e6c232010-08-09 17:48:10 -050036}
37
David Barr41529bb2011-03-05 13:30:23 +110038void fast_export_deinit(void)
39{
40 if (buffer_deinit(&report_buffer))
41 die_errno("error closing fast-import feedback stream");
42}
43
David Barr03087972010-12-13 16:41:12 +110044void fast_export_delete(const char *path)
David Barrc0e6c232010-08-09 17:48:10 -050045{
David Barr03087972010-12-13 16:41:12 +110046 putchar('D');
47 putchar(' ');
48 quote_c_style(path, NULL, stdout, 0);
49 putchar('\n');
David Barrc0e6c232010-08-09 17:48:10 -050050}
51
David Barr03087972010-12-13 16:41:12 +110052static void fast_export_truncate(const char *path, uint32_t mode)
Jonathan Nieder723b7a22010-12-10 04:00:55 -060053{
David Barr03087972010-12-13 16:41:12 +110054 fast_export_modify(path, mode, "inline");
Jonathan Nieder723b7a22010-12-10 04:00:55 -060055 printf("data 0\n\n");
56}
57
David Barr03087972010-12-13 16:41:12 +110058void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
David Barrc0e6c232010-08-09 17:48:10 -050059{
60 /* Mode must be 100644, 100755, 120000, or 160000. */
Jonathan Nieder723b7a22010-12-10 04:00:55 -060061 if (!dataref) {
David Barr03087972010-12-13 16:41:12 +110062 fast_export_truncate(path, mode);
Jonathan Nieder723b7a22010-12-10 04:00:55 -060063 return;
64 }
David Barr03087972010-12-13 16:41:12 +110065 printf("M %06"PRIo32" %s ", mode, dataref);
66 quote_c_style(path, NULL, stdout, 0);
67 putchar('\n');
David Barrc0e6c232010-08-09 17:48:10 -050068}
69
Florian Achleitnera9a55612012-09-19 17:21:25 +020070void fast_export_begin_note(uint32_t revision, const char *author,
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020071 const char *log, unsigned long timestamp, const char *note_ref)
Florian Achleitnera9a55612012-09-19 17:21:25 +020072{
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020073 static int firstnote = 1;
Florian Achleitnera9a55612012-09-19 17:21:25 +020074 size_t loglen = strlen(log);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020075 printf("commit %s\n", note_ref);
Florian Achleitnera9a55612012-09-19 17:21:25 +020076 printf("committer %s <%s@%s> %ld +0000\n", author, author, "local", timestamp);
77 printf("data %"PRIuMAX"\n", (uintmax_t)loglen);
78 fwrite(log, loglen, 1, stdout);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020079 if (firstnote) {
80 if (revision > 1)
81 printf("from %s^0", note_ref);
82 firstnote = 0;
83 }
Florian Achleitnera9a55612012-09-19 17:21:25 +020084 fputc('\n', stdout);
85}
86
Dmitry Ivankov3c239532012-09-19 17:21:24 +020087void fast_export_note(const char *committish, const char *dataref)
88{
89 printf("N %s %s\n", dataref, committish);
90}
91
David Barrc0e6c232010-08-09 17:48:10 -050092static char gitsvnline[MAX_GITSVN_LINE_LEN];
Jonathan Nieder9ecfa8a2011-05-26 01:51:38 -050093void fast_export_begin_commit(uint32_t revision, const char *author,
Jonathan Nieder195b7ca2011-03-26 00:49:37 -050094 const struct strbuf *log,
David Barr7c5817d2011-03-22 17:52:17 -050095 const char *uuid, const char *url,
Florian Achleitner271fd1f2012-09-19 17:21:22 +020096 unsigned long timestamp, const char *local_ref)
David Barrc0e6c232010-08-09 17:48:10 -050097{
Jonathan Nieder195b7ca2011-03-26 00:49:37 -050098 static const struct strbuf empty = STRBUF_INIT;
David Barrc0e6c232010-08-09 17:48:10 -050099 if (!log)
Jonathan Nieder195b7ca2011-03-26 00:49:37 -0500100 log = &empty;
David Barr7c5817d2011-03-22 17:52:17 -0500101 if (*uuid && *url) {
Ramsay Jones5418d962010-09-09 18:24:06 +0100102 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
103 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
David Barr7c5817d2011-03-22 17:52:17 -0500104 url, revision, uuid);
David Barrc0e6c232010-08-09 17:48:10 -0500105 } else {
106 *gitsvnline = '\0';
107 }
Florian Achleitner271fd1f2012-09-19 17:21:22 +0200108 printf("commit %s\n", local_ref);
Jonathan Nieder78e1a3f2010-12-09 18:57:13 -0600109 printf("mark :%"PRIu32"\n", revision);
David Barrc0e6c232010-08-09 17:48:10 -0500110 printf("committer %s <%s@%s> %ld +0000\n",
David Barr7c5817d2011-03-22 17:52:17 -0500111 *author ? author : "nobody",
112 *author ? author : "nobody",
113 *uuid ? uuid : "local", timestamp);
Jonathan Nieder41e6b912011-03-27 12:19:14 -0500114 printf("data %"PRIuMAX"\n",
115 (uintmax_t) (log->len + strlen(gitsvnline)));
Jonathan Nieder195b7ca2011-03-26 00:49:37 -0500116 fwrite(log->buf, log->len, 1, stdout);
117 printf("%s\n", gitsvnline);
David Barrc0e6c232010-08-09 17:48:10 -0500118 if (!first_commit_done) {
119 if (revision > 1)
David Barrdd3f42a2010-12-12 13:41:38 +1100120 printf("from :%"PRIu32"\n", revision - 1);
David Barrc0e6c232010-08-09 17:48:10 -0500121 first_commit_done = 1;
122 }
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600123}
David Barrc0e6c232010-08-09 17:48:10 -0500124
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600125void fast_export_end_commit(uint32_t revision)
126{
Ramsay Jones5418d962010-09-09 18:24:06 +0100127 printf("progress Imported commit %"PRIu32".\n\n", revision);
David Barrc0e6c232010-08-09 17:48:10 -0500128}
129
David Barr03087972010-12-13 16:41:12 +1100130static void ls_from_rev(uint32_t rev, const char *path)
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600131{
132 /* ls :5 path/to/old/file */
David Barr03087972010-12-13 16:41:12 +1100133 printf("ls :%"PRIu32" ", rev);
134 quote_c_style(path, NULL, stdout, 0);
135 putchar('\n');
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600136 fflush(stdout);
137}
138
David Barr03087972010-12-13 16:41:12 +1100139static void ls_from_active_commit(const char *path)
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600140{
141 /* ls "path/to/file" */
142 printf("ls \"");
David Barr03087972010-12-13 16:41:12 +1100143 quote_c_style(path, NULL, stdout, 1);
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600144 printf("\"\n");
145 fflush(stdout);
146}
147
David Barr41529bb2011-03-05 13:30:23 +1100148static const char *get_response_line(void)
149{
150 const char *line = buffer_read_line(&report_buffer);
151 if (line)
152 return line;
153 if (buffer_ferror(&report_buffer))
154 die_errno("error reading from fast-import");
155 die("unexpected end of fast-import feedback");
156}
157
Jonathan Niederc9d1c8b2010-12-28 04:30:54 -0600158static void die_short_read(struct line_buffer *input)
159{
160 if (buffer_ferror(input))
161 die_errno("error reading dump file");
162 die("invalid dump: unexpected end of file");
163}
164
David Barr7a75e662011-03-19 18:20:54 +1100165static int parse_cat_response_line(const char *header, off_t *len)
166{
Jonathan Niederabe27c02011-05-27 05:18:33 -0500167 uintmax_t n;
David Barr7a75e662011-03-19 18:20:54 +1100168 const char *type;
169 const char *end;
170
Christian Couder95662312013-12-01 08:49:16 +0100171 if (ends_with(header, " missing"))
David Barr7a75e662011-03-19 18:20:54 +1100172 return error("cat-blob reports missing blob: %s", header);
David Barr53153e82012-06-01 00:41:28 +1000173 type = strstr(header, " blob ");
David Barr7a75e662011-03-19 18:20:54 +1100174 if (!type)
175 return error("cat-blob header has wrong object type: %s", header);
Jonathan Niederabe27c02011-05-27 05:18:33 -0500176 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
David Barr7a75e662011-03-19 18:20:54 +1100177 if (end == type + strlen(" blob "))
178 return error("cat-blob header does not contain length: %s", header);
Jonathan Niederabe27c02011-05-27 05:18:33 -0500179 if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
180 return error("cat-blob header contains negative length: %s", header);
181 if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
182 return error("blob too large for current definition of off_t");
183 *len = n;
David Barr7a75e662011-03-19 18:20:54 +1100184 if (*end)
185 return error("cat-blob header contains garbage after length: %s", header);
186 return 0;
187}
188
Jonathan Niederabe27c02011-05-27 05:18:33 -0500189static void check_preimage_overflow(off_t a, off_t b)
190{
191 if (signed_add_overflows(a, b))
192 die("blob too large for current definition of off_t");
193}
194
David Barr7a75e662011-03-19 18:20:54 +1100195static long apply_delta(off_t len, struct line_buffer *input,
196 const char *old_data, uint32_t old_mode)
197{
198 long ret;
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500199 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
David Barr7a75e662011-03-19 18:20:54 +1100200 FILE *out;
201
202 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
203 die("cannot open temporary file for blob retrieval");
David Barr7a75e662011-03-19 18:20:54 +1100204 if (old_data) {
205 const char *response;
206 printf("cat-blob %s\n", old_data);
207 fflush(stdout);
208 response = get_response_line();
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500209 if (parse_cat_response_line(response, &preimage.max_off))
David Barr7a75e662011-03-19 18:20:54 +1100210 die("invalid cat-blob response: %s", response);
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500211 check_preimage_overflow(preimage.max_off, 1);
David Barr7a75e662011-03-19 18:20:54 +1100212 }
213 if (old_mode == REPO_MODE_LNK) {
214 strbuf_addstr(&preimage.buf, "link ");
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500215 check_preimage_overflow(preimage.max_off, strlen("link "));
216 preimage.max_off += strlen("link ");
217 check_preimage_overflow(preimage.max_off, 1);
David Barr7a75e662011-03-19 18:20:54 +1100218 }
219 if (svndiff0_apply(input, len, &preimage, out))
220 die("cannot apply delta");
221 if (old_data) {
222 /* Read the remainder of preimage and trailing newline. */
Jonathan Nieder3ac10b22011-05-27 05:44:27 -0500223 assert(!signed_add_overflows(preimage.max_off, 1));
224 preimage.max_off++; /* room for newline */
225 if (move_window(&preimage, preimage.max_off - 1, 1))
David Barr7a75e662011-03-19 18:20:54 +1100226 die("cannot seek to end of input");
227 if (preimage.buf.buf[0] != '\n')
228 die("missing newline after cat-blob response");
229 }
230 ret = buffer_tmpfile_prepare_to_read(&postimage);
231 if (ret < 0)
232 die("cannot read temporary file for blob retrieval");
233 strbuf_release(&preimage.buf);
234 return ret;
235}
236
Dmitry Ivankov3c239532012-09-19 17:21:24 +0200237void fast_export_buf_to_data(const struct strbuf *data)
238{
239 printf("data %"PRIuMAX"\n", (uintmax_t)data->len);
240 fwrite(data->buf, data->len, 1, stdout);
241 fputc('\n', stdout);
242}
243
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600244void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input)
David Barrc0e6c232010-08-09 17:48:10 -0500245{
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600246 assert(len >= 0);
David Barrc0e6c232010-08-09 17:48:10 -0500247 if (mode == REPO_MODE_LNK) {
248 /* svn symlink blobs start with "link " */
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600249 if (len < 5)
250 die("invalid dump: symlink too short for \"link\" prefix");
David Barrc0e6c232010-08-09 17:48:10 -0500251 len -= 5;
Jonathan Niederc9d1c8b2010-12-28 04:30:54 -0600252 if (buffer_skip_bytes(input, 5) != 5)
253 die_short_read(input);
David Barrc0e6c232010-08-09 17:48:10 -0500254 }
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600255 printf("data %"PRIuMAX"\n", (uintmax_t) len);
Jonathan Niederc9d1c8b2010-12-28 04:30:54 -0600256 if (buffer_copy_bytes(input, len) != len)
257 die_short_read(input);
David Barrc0e6c232010-08-09 17:48:10 -0500258 fputc('\n', stdout);
259}
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600260
261static int parse_ls_response(const char *response, uint32_t *mode,
262 struct strbuf *dataref)
263{
264 const char *tab;
265 const char *response_end;
266
267 assert(response);
268 response_end = response + strlen(response);
269
270 if (*response == 'm') { /* Missing. */
271 errno = ENOENT;
272 return -1;
273 }
274
275 /* Mode. */
David Barr6a0b4432012-06-01 00:41:29 +1000276 if (response_end - response < (signed) strlen("100644") ||
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600277 response[strlen("100644")] != ' ')
278 die("invalid ls response: missing mode: %s", response);
279 *mode = 0;
280 for (; *response != ' '; response++) {
281 char ch = *response;
282 if (ch < '0' || ch > '7')
283 die("invalid ls response: mode is not octal: %s", response);
284 *mode *= 8;
285 *mode += ch - '0';
286 }
287
288 /* ' blob ' or ' tree ' */
David Barr6a0b4432012-06-01 00:41:29 +1000289 if (response_end - response < (signed) strlen(" blob ") ||
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600290 (response[1] != 'b' && response[1] != 't'))
291 die("unexpected ls response: not a tree or blob: %s", response);
292 response += strlen(" blob ");
293
294 /* Dataref. */
295 tab = memchr(response, '\t', response_end - response);
296 if (!tab)
297 die("invalid ls response: missing tab: %s", response);
298 strbuf_add(dataref, response, tab - response);
299 return 0;
300}
301
David Barr03087972010-12-13 16:41:12 +1100302int fast_export_ls_rev(uint32_t rev, const char *path,
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600303 uint32_t *mode, struct strbuf *dataref)
304{
David Barr03087972010-12-13 16:41:12 +1100305 ls_from_rev(rev, path);
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600306 return parse_ls_response(get_response_line(), mode, dataref);
307}
308
David Barr03087972010-12-13 16:41:12 +1100309int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600310{
David Barr03087972010-12-13 16:41:12 +1100311 ls_from_active_commit(path);
Jonathan Nieder723b7a22010-12-10 04:00:55 -0600312 return parse_ls_response(get_response_line(), mode, dataref);
313}
David Barr7a75e662011-03-19 18:20:54 +1100314
315void fast_export_blob_delta(uint32_t mode,
316 uint32_t old_mode, const char *old_data,
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600317 off_t len, struct line_buffer *input)
David Barr7a75e662011-03-19 18:20:54 +1100318{
319 long postimage_len;
Jonathan Nieder2d54b9e2012-02-02 04:19:35 -0600320
321 assert(len >= 0);
322 postimage_len = apply_delta(len, input, old_data, old_mode);
David Barr7a75e662011-03-19 18:20:54 +1100323 if (mode == REPO_MODE_LNK) {
324 buffer_skip_bytes(&postimage, strlen("link "));
325 postimage_len -= strlen("link ");
326 }
327 printf("data %ld\n", postimage_len);
328 buffer_copy_bytes(&postimage, postimage_len);
329 fputc('\n', stdout);
330}