2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
6 #include "git-compat-util.h"
9 #include "fast_export.h"
10 #include "repo_tree.h"
13 #include "sliding_window.h"
14 #include "line_buffer.h"
16 #define MAX_GITSVN_LINE_LEN 4096
17 #define REPORT_FILENO 3
19 static uint32_t first_commit_done;
20 static struct line_buffer postimage = LINE_BUFFER_INIT;
21 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
23 /* NEEDSWORK: move to fast_export_init() */
24 static int init_postimage(void)
26 static int postimage_initialized;
27 if (postimage_initialized)
29 postimage_initialized = 1;
30 return buffer_tmpfile_init(&postimage);
33 static int init_report_buffer(int fd)
35 static int report_buffer_initialized;
36 if (report_buffer_initialized)
38 report_buffer_initialized = 1;
39 return buffer_fdinit(&report_buffer, fd);
42 void fast_export_init(int fd)
44 if (buffer_fdinit(&report_buffer, fd))
45 die_errno("cannot read from file descriptor %d", fd);
48 void fast_export_deinit(void)
50 if (buffer_deinit(&report_buffer))
51 die_errno("error closing fast-import feedback stream");
54 void fast_export_reset(void)
56 buffer_reset(&report_buffer);
59 void fast_export_delete(const char *path)
63 quote_c_style(path, NULL, stdout, 0);
67 static void fast_export_truncate(const char *path, uint32_t mode)
69 fast_export_modify(path, mode, "inline");
73 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
75 /* Mode must be 100644, 100755, 120000, or 160000. */
77 fast_export_truncate(path, mode);
80 printf("M %06"PRIo32" %s ", mode, dataref);
81 quote_c_style(path, NULL, stdout, 0);
85 static char gitsvnline[MAX_GITSVN_LINE_LEN];
86 void fast_export_begin_commit(uint32_t revision, const char *author,
87 const struct strbuf *log,
88 const char *uuid, const char *url,
89 unsigned long timestamp)
91 static const struct strbuf empty = STRBUF_INIT;
95 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
96 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
101 printf("commit refs/heads/master\n");
102 printf("mark :%"PRIu32"\n", revision);
103 printf("committer %s <%s@%s> %ld +0000\n",
104 *author ? author : "nobody",
105 *author ? author : "nobody",
106 *uuid ? uuid : "local", timestamp);
107 printf("data %"PRIuMAX"\n",
108 (uintmax_t) (log->len + strlen(gitsvnline)));
109 fwrite(log->buf, log->len, 1, stdout);
110 printf("%s\n", gitsvnline);
111 if (!first_commit_done) {
113 printf("from :%"PRIu32"\n", revision - 1);
114 first_commit_done = 1;
118 void fast_export_end_commit(uint32_t revision)
120 printf("progress Imported commit %"PRIu32".\n\n", revision);
123 static void ls_from_rev(uint32_t rev, const char *path)
125 /* ls :5 path/to/old/file */
126 printf("ls :%"PRIu32" ", rev);
127 quote_c_style(path, NULL, stdout, 0);
132 static void ls_from_active_commit(const char *path)
134 /* ls "path/to/file" */
136 quote_c_style(path, NULL, stdout, 1);
141 static const char *get_response_line(void)
143 const char *line = buffer_read_line(&report_buffer);
146 if (buffer_ferror(&report_buffer))
147 die_errno("error reading from fast-import");
148 die("unexpected end of fast-import feedback");
151 static void die_short_read(struct line_buffer *input)
153 if (buffer_ferror(input))
154 die_errno("error reading dump file");
155 die("invalid dump: unexpected end of file");
158 static int ends_with(const char *s, size_t len, const char *suffix)
160 const size_t suffixlen = strlen(suffix);
163 return !memcmp(s + len - suffixlen, suffix, suffixlen);
166 static int parse_cat_response_line(const char *header, off_t *len)
168 size_t headerlen = strlen(header);
172 if (ends_with(header, headerlen, " missing"))
173 return error("cat-blob reports missing blob: %s", header);
174 type = memmem(header, headerlen, " blob ", strlen(" blob "));
176 return error("cat-blob header has wrong object type: %s", header);
177 *len = strtoumax(type + strlen(" blob "), (char **) &end, 10);
178 if (end == type + strlen(" blob "))
179 return error("cat-blob header does not contain length: %s", header);
181 return error("cat-blob header contains garbage after length: %s", header);
185 static long apply_delta(off_t len, struct line_buffer *input,
186 const char *old_data, uint32_t old_mode)
189 off_t preimage_len = 0;
190 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, -1);
193 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
194 die("cannot open temporary file for blob retrieval");
195 if (init_report_buffer(REPORT_FILENO))
196 die("cannot open fd 3 for feedback from fast-import");
198 const char *response;
199 printf("cat-blob %s\n", old_data);
201 response = get_response_line();
202 if (parse_cat_response_line(response, &preimage_len))
203 die("invalid cat-blob response: %s", response);
205 if (old_mode == REPO_MODE_LNK) {
206 strbuf_addstr(&preimage.buf, "link ");
207 preimage_len += strlen("link ");
209 if (svndiff0_apply(input, len, &preimage, out))
210 die("cannot apply delta");
212 /* Read the remainder of preimage and trailing newline. */
213 if (move_window(&preimage, preimage_len, 1))
214 die("cannot seek to end of input");
215 if (preimage.buf.buf[0] != '\n')
216 die("missing newline after cat-blob response");
218 ret = buffer_tmpfile_prepare_to_read(&postimage);
220 die("cannot read temporary file for blob retrieval");
221 strbuf_release(&preimage.buf);
225 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
227 if (mode == REPO_MODE_LNK) {
228 /* svn symlink blobs start with "link " */
230 if (buffer_skip_bytes(input, 5) != 5)
231 die_short_read(input);
233 printf("data %"PRIu32"\n", len);
234 if (buffer_copy_bytes(input, len) != len)
235 die_short_read(input);
239 static int parse_ls_response(const char *response, uint32_t *mode,
240 struct strbuf *dataref)
243 const char *response_end;
246 response_end = response + strlen(response);
248 if (*response == 'm') { /* Missing. */
254 if (response_end - response < strlen("100644") ||
255 response[strlen("100644")] != ' ')
256 die("invalid ls response: missing mode: %s", response);
258 for (; *response != ' '; response++) {
260 if (ch < '0' || ch > '7')
261 die("invalid ls response: mode is not octal: %s", response);
266 /* ' blob ' or ' tree ' */
267 if (response_end - response < strlen(" blob ") ||
268 (response[1] != 'b' && response[1] != 't'))
269 die("unexpected ls response: not a tree or blob: %s", response);
270 response += strlen(" blob ");
273 tab = memchr(response, '\t', response_end - response);
275 die("invalid ls response: missing tab: %s", response);
276 strbuf_add(dataref, response, tab - response);
280 int fast_export_ls_rev(uint32_t rev, const char *path,
281 uint32_t *mode, struct strbuf *dataref)
283 ls_from_rev(rev, path);
284 return parse_ls_response(get_response_line(), mode, dataref);
287 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
289 ls_from_active_commit(path);
290 return parse_ls_response(get_response_line(), mode, dataref);
293 void fast_export_blob_delta(uint32_t mode,
294 uint32_t old_mode, const char *old_data,
295 uint32_t len, struct line_buffer *input)
298 if (len > maximum_signed_value_of_type(off_t))
299 die("enormous delta");
300 postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
301 if (mode == REPO_MODE_LNK) {
302 buffer_skip_bytes(&postimage, strlen("link "));
303 postimage_len -= strlen("link ");
305 printf("data %ld\n", postimage_len);
306 buffer_copy_bytes(&postimage, postimage_len);