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
18 static uint32_t first_commit_done;
19 static struct line_buffer postimage = LINE_BUFFER_INIT;
20 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
22 /* NEEDSWORK: move to fast_export_init() */
23 static int init_postimage(void)
25 static int postimage_initialized;
26 if (postimage_initialized)
28 postimage_initialized = 1;
29 return buffer_tmpfile_init(&postimage);
32 void fast_export_init(int fd)
34 if (buffer_fdinit(&report_buffer, fd))
35 die_errno("cannot read from file descriptor %d", fd);
38 void fast_export_deinit(void)
40 if (buffer_deinit(&report_buffer))
41 die_errno("error closing fast-import feedback stream");
44 void fast_export_reset(void)
46 buffer_reset(&report_buffer);
49 void fast_export_delete(const char *path)
53 quote_c_style(path, NULL, stdout, 0);
57 static void fast_export_truncate(const char *path, uint32_t mode)
59 fast_export_modify(path, mode, "inline");
63 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
65 /* Mode must be 100644, 100755, 120000, or 160000. */
67 fast_export_truncate(path, mode);
70 printf("M %06"PRIo32" %s ", mode, dataref);
71 quote_c_style(path, NULL, stdout, 0);
75 static char gitsvnline[MAX_GITSVN_LINE_LEN];
76 void fast_export_begin_commit(uint32_t revision, const char *author,
77 const struct strbuf *log,
78 const char *uuid, const char *url,
79 unsigned long timestamp)
81 static const struct strbuf empty = STRBUF_INIT;
85 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
86 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
91 printf("commit refs/heads/master\n");
92 printf("mark :%"PRIu32"\n", revision);
93 printf("committer %s <%s@%s> %ld +0000\n",
94 *author ? author : "nobody",
95 *author ? author : "nobody",
96 *uuid ? uuid : "local", timestamp);
97 printf("data %"PRIuMAX"\n",
98 (uintmax_t) (log->len + strlen(gitsvnline)));
99 fwrite(log->buf, log->len, 1, stdout);
100 printf("%s\n", gitsvnline);
101 if (!first_commit_done) {
103 printf("from :%"PRIu32"\n", revision - 1);
104 first_commit_done = 1;
108 void fast_export_end_commit(uint32_t revision)
110 printf("progress Imported commit %"PRIu32".\n\n", revision);
113 static void ls_from_rev(uint32_t rev, const char *path)
115 /* ls :5 path/to/old/file */
116 printf("ls :%"PRIu32" ", rev);
117 quote_c_style(path, NULL, stdout, 0);
122 static void ls_from_active_commit(const char *path)
124 /* ls "path/to/file" */
126 quote_c_style(path, NULL, stdout, 1);
131 static const char *get_response_line(void)
133 const char *line = buffer_read_line(&report_buffer);
136 if (buffer_ferror(&report_buffer))
137 die_errno("error reading from fast-import");
138 die("unexpected end of fast-import feedback");
141 static void die_short_read(struct line_buffer *input)
143 if (buffer_ferror(input))
144 die_errno("error reading dump file");
145 die("invalid dump: unexpected end of file");
148 static int ends_with(const char *s, size_t len, const char *suffix)
150 const size_t suffixlen = strlen(suffix);
153 return !memcmp(s + len - suffixlen, suffix, suffixlen);
156 static int parse_cat_response_line(const char *header, off_t *len)
158 size_t headerlen = strlen(header);
163 if (ends_with(header, headerlen, " missing"))
164 return error("cat-blob reports missing blob: %s", header);
165 type = memmem(header, headerlen, " blob ", strlen(" blob "));
167 return error("cat-blob header has wrong object type: %s", header);
168 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
169 if (end == type + strlen(" blob "))
170 return error("cat-blob header does not contain length: %s", header);
171 if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
172 return error("cat-blob header contains negative length: %s", header);
173 if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
174 return error("blob too large for current definition of off_t");
177 return error("cat-blob header contains garbage after length: %s", header);
181 static void check_preimage_overflow(off_t a, off_t b)
183 if (signed_add_overflows(a, b))
184 die("blob too large for current definition of off_t");
187 static long apply_delta(off_t len, struct line_buffer *input,
188 const char *old_data, uint32_t old_mode)
191 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
194 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
195 die("cannot open temporary file for blob retrieval");
197 const char *response;
198 printf("cat-blob %s\n", old_data);
200 response = get_response_line();
201 if (parse_cat_response_line(response, &preimage.max_off))
202 die("invalid cat-blob response: %s", response);
203 check_preimage_overflow(preimage.max_off, 1);
205 if (old_mode == REPO_MODE_LNK) {
206 strbuf_addstr(&preimage.buf, "link ");
207 check_preimage_overflow(preimage.max_off, strlen("link "));
208 preimage.max_off += strlen("link ");
209 check_preimage_overflow(preimage.max_off, 1);
211 if (svndiff0_apply(input, len, &preimage, out))
212 die("cannot apply delta");
214 /* Read the remainder of preimage and trailing newline. */
215 assert(!signed_add_overflows(preimage.max_off, 1));
216 preimage.max_off++; /* room for newline */
217 if (move_window(&preimage, preimage.max_off - 1, 1))
218 die("cannot seek to end of input");
219 if (preimage.buf.buf[0] != '\n')
220 die("missing newline after cat-blob response");
222 ret = buffer_tmpfile_prepare_to_read(&postimage);
224 die("cannot read temporary file for blob retrieval");
225 strbuf_release(&preimage.buf);
229 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
231 if (mode == REPO_MODE_LNK) {
232 /* svn symlink blobs start with "link " */
234 if (buffer_skip_bytes(input, 5) != 5)
235 die_short_read(input);
237 printf("data %"PRIu32"\n", len);
238 if (buffer_copy_bytes(input, len) != len)
239 die_short_read(input);
243 static int parse_ls_response(const char *response, uint32_t *mode,
244 struct strbuf *dataref)
247 const char *response_end;
250 response_end = response + strlen(response);
252 if (*response == 'm') { /* Missing. */
258 if (response_end - response < strlen("100644") ||
259 response[strlen("100644")] != ' ')
260 die("invalid ls response: missing mode: %s", response);
262 for (; *response != ' '; response++) {
264 if (ch < '0' || ch > '7')
265 die("invalid ls response: mode is not octal: %s", response);
270 /* ' blob ' or ' tree ' */
271 if (response_end - response < strlen(" blob ") ||
272 (response[1] != 'b' && response[1] != 't'))
273 die("unexpected ls response: not a tree or blob: %s", response);
274 response += strlen(" blob ");
277 tab = memchr(response, '\t', response_end - response);
279 die("invalid ls response: missing tab: %s", response);
280 strbuf_add(dataref, response, tab - response);
284 int fast_export_ls_rev(uint32_t rev, const char *path,
285 uint32_t *mode, struct strbuf *dataref)
287 ls_from_rev(rev, path);
288 return parse_ls_response(get_response_line(), mode, dataref);
291 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
293 ls_from_active_commit(path);
294 return parse_ls_response(get_response_line(), mode, dataref);
297 void fast_export_blob_delta(uint32_t mode,
298 uint32_t old_mode, const char *old_data,
299 uint32_t len, struct line_buffer *input)
302 if (len > maximum_signed_value_of_type(off_t))
303 die("enormous delta");
304 postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
305 if (mode == REPO_MODE_LNK) {
306 buffer_skip_bytes(&postimage, strlen("link "));
307 postimage_len -= strlen("link ");
309 printf("data %ld\n", postimage_len);
310 buffer_copy_bytes(&postimage, postimage_len);