vcs-svn: reset first_commit_done in fast_export_init
[git/jrn.git] / vcs-svn / fast_export.c
blob19d7c34c25d7b41e5920ad252784b23771321392
1 /*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
6 #include "git-compat-util.h"
7 #include "strbuf.h"
8 #include "quote.h"
9 #include "fast_export.h"
10 #include "repo_tree.h"
11 #include "strbuf.h"
12 #include "svndiff.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)
27 return 0;
28 postimage_initialized = 1;
29 return buffer_tmpfile_init(&postimage);
32 void fast_export_init(int fd)
34 first_commit_done = 0;
35 if (buffer_fdinit(&report_buffer, fd))
36 die_errno("cannot read from file descriptor %d", fd);
39 void fast_export_deinit(void)
41 if (buffer_deinit(&report_buffer))
42 die_errno("error closing fast-import feedback stream");
45 void fast_export_reset(void)
47 buffer_reset(&report_buffer);
50 void fast_export_delete(const char *path)
52 putchar('D');
53 putchar(' ');
54 quote_c_style(path, NULL, stdout, 0);
55 putchar('\n');
58 static void fast_export_truncate(const char *path, uint32_t mode)
60 fast_export_modify(path, mode, "inline");
61 printf("data 0\n\n");
64 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
66 /* Mode must be 100644, 100755, 120000, or 160000. */
67 if (!dataref) {
68 fast_export_truncate(path, mode);
69 return;
71 printf("M %06"PRIo32" %s ", mode, dataref);
72 quote_c_style(path, NULL, stdout, 0);
73 putchar('\n');
76 static char gitsvnline[MAX_GITSVN_LINE_LEN];
77 void fast_export_begin_commit(uint32_t revision, const char *author,
78 const struct strbuf *log,
79 const char *uuid, const char *url,
80 unsigned long timestamp)
82 static const struct strbuf empty = STRBUF_INIT;
83 if (!log)
84 log = ∅
85 if (*uuid && *url) {
86 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
87 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
88 url, revision, uuid);
89 } else {
90 *gitsvnline = '\0';
92 printf("commit refs/heads/master\n");
93 printf("mark :%"PRIu32"\n", revision);
94 printf("committer %s <%s@%s> %ld +0000\n",
95 *author ? author : "nobody",
96 *author ? author : "nobody",
97 *uuid ? uuid : "local", timestamp);
98 printf("data %"PRIuMAX"\n",
99 (uintmax_t) (log->len + strlen(gitsvnline)));
100 fwrite(log->buf, log->len, 1, stdout);
101 printf("%s\n", gitsvnline);
102 if (!first_commit_done) {
103 if (revision > 1)
104 printf("from :%"PRIu32"\n", revision - 1);
105 first_commit_done = 1;
109 void fast_export_end_commit(uint32_t revision)
111 printf("progress Imported commit %"PRIu32".\n\n", revision);
114 static void ls_from_rev(uint32_t rev, const char *path)
116 /* ls :5 path/to/old/file */
117 printf("ls :%"PRIu32" ", rev);
118 quote_c_style(path, NULL, stdout, 0);
119 putchar('\n');
120 fflush(stdout);
123 static void ls_from_active_commit(const char *path)
125 /* ls "path/to/file" */
126 printf("ls \"");
127 quote_c_style(path, NULL, stdout, 1);
128 printf("\"\n");
129 fflush(stdout);
132 static const char *get_response_line(void)
134 const char *line = buffer_read_line(&report_buffer);
135 if (line)
136 return line;
137 if (buffer_ferror(&report_buffer))
138 die_errno("error reading from fast-import");
139 die("unexpected end of fast-import feedback");
142 static void die_short_read(struct line_buffer *input)
144 if (buffer_ferror(input))
145 die_errno("error reading dump file");
146 die("invalid dump: unexpected end of file");
149 static int ends_with(const char *s, size_t len, const char *suffix)
151 const size_t suffixlen = strlen(suffix);
152 if (len < suffixlen)
153 return 0;
154 return !memcmp(s + len - suffixlen, suffix, suffixlen);
157 static int parse_cat_response_line(const char *header, off_t *len)
159 size_t headerlen = strlen(header);
160 uintmax_t n;
161 const char *type;
162 const char *end;
164 if (ends_with(header, headerlen, " missing"))
165 return error("cat-blob reports missing blob: %s", header);
166 type = memmem(header, headerlen, " blob ", strlen(" blob "));
167 if (!type)
168 return error("cat-blob header has wrong object type: %s", header);
169 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
170 if (end == type + strlen(" blob "))
171 return error("cat-blob header does not contain length: %s", header);
172 if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
173 return error("cat-blob header contains negative length: %s", header);
174 if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
175 return error("blob too large for current definition of off_t");
176 *len = n;
177 if (*end)
178 return error("cat-blob header contains garbage after length: %s", header);
179 return 0;
182 static void check_preimage_overflow(off_t a, off_t b)
184 if (signed_add_overflows(a, b))
185 die("blob too large for current definition of off_t");
188 static long apply_delta(off_t len, struct line_buffer *input,
189 const char *old_data, uint32_t old_mode)
191 long ret;
192 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
193 FILE *out;
195 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
196 die("cannot open temporary file for blob retrieval");
197 if (old_data) {
198 const char *response;
199 printf("cat-blob %s\n", old_data);
200 fflush(stdout);
201 response = get_response_line();
202 if (parse_cat_response_line(response, &preimage.max_off))
203 die("invalid cat-blob response: %s", response);
204 check_preimage_overflow(preimage.max_off, 1);
206 if (old_mode == REPO_MODE_LNK) {
207 strbuf_addstr(&preimage.buf, "link ");
208 check_preimage_overflow(preimage.max_off, strlen("link "));
209 preimage.max_off += strlen("link ");
210 check_preimage_overflow(preimage.max_off, 1);
212 if (svndiff0_apply(input, len, &preimage, out))
213 die("cannot apply delta");
214 if (old_data) {
215 /* Read the remainder of preimage and trailing newline. */
216 assert(!signed_add_overflows(preimage.max_off, 1));
217 preimage.max_off++; /* room for newline */
218 if (move_window(&preimage, preimage.max_off - 1, 1))
219 die("cannot seek to end of input");
220 if (preimage.buf.buf[0] != '\n')
221 die("missing newline after cat-blob response");
223 ret = buffer_tmpfile_prepare_to_read(&postimage);
224 if (ret < 0)
225 die("cannot read temporary file for blob retrieval");
226 strbuf_release(&preimage.buf);
227 return ret;
230 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
232 if (mode == REPO_MODE_LNK) {
233 /* svn symlink blobs start with "link " */
234 len -= 5;
235 if (buffer_skip_bytes(input, 5) != 5)
236 die_short_read(input);
238 printf("data %"PRIu32"\n", len);
239 if (buffer_copy_bytes(input, len) != len)
240 die_short_read(input);
241 fputc('\n', stdout);
244 static int parse_ls_response(const char *response, uint32_t *mode,
245 struct strbuf *dataref)
247 const char *tab;
248 const char *response_end;
250 assert(response);
251 response_end = response + strlen(response);
253 if (*response == 'm') { /* Missing. */
254 errno = ENOENT;
255 return -1;
258 /* Mode. */
259 if (response_end - response < strlen("100644") ||
260 response[strlen("100644")] != ' ')
261 die("invalid ls response: missing mode: %s", response);
262 *mode = 0;
263 for (; *response != ' '; response++) {
264 char ch = *response;
265 if (ch < '0' || ch > '7')
266 die("invalid ls response: mode is not octal: %s", response);
267 *mode *= 8;
268 *mode += ch - '0';
271 /* ' blob ' or ' tree ' */
272 if (response_end - response < strlen(" blob ") ||
273 (response[1] != 'b' && response[1] != 't'))
274 die("unexpected ls response: not a tree or blob: %s", response);
275 response += strlen(" blob ");
277 /* Dataref. */
278 tab = memchr(response, '\t', response_end - response);
279 if (!tab)
280 die("invalid ls response: missing tab: %s", response);
281 strbuf_add(dataref, response, tab - response);
282 return 0;
285 int fast_export_ls_rev(uint32_t rev, const char *path,
286 uint32_t *mode, struct strbuf *dataref)
288 ls_from_rev(rev, path);
289 return parse_ls_response(get_response_line(), mode, dataref);
292 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
294 ls_from_active_commit(path);
295 return parse_ls_response(get_response_line(), mode, dataref);
298 void fast_export_blob_delta(uint32_t mode,
299 uint32_t old_mode, const char *old_data,
300 uint32_t len, struct line_buffer *input)
302 long postimage_len;
303 if (len > maximum_signed_value_of_type(off_t))
304 die("enormous delta");
305 postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
306 if (mode == REPO_MODE_LNK) {
307 buffer_skip_bytes(&postimage, strlen("link "));
308 postimage_len -= strlen("link ");
310 printf("data %ld\n", postimage_len);
311 buffer_copy_bytes(&postimage, postimage_len);
312 fputc('\n', stdout);