vcs-svn: remove custom mode constants
[git/git.git] / vcs-svn / fast_export.c
CommitLineData
c0e6c23d
DB
1/*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
5
a9a55613 6#include "cache.h"
03087971 7#include "quote.h"
c0e6c23d 8#include "fast_export.h"
c0e6c23d 9#include "repo_tree.h"
723b7a27 10#include "strbuf.h"
7a75e661
DB
11#include "svndiff.h"
12#include "sliding_window.h"
13#include "line_buffer.h"
c0e6c23d
DB
14
15#define MAX_GITSVN_LINE_LEN 4096
16
17static uint32_t first_commit_done;
7a75e661 18static struct line_buffer postimage = LINE_BUFFER_INIT;
41529bbc 19static struct line_buffer report_buffer = LINE_BUFFER_INIT;
c0e6c23d 20
7a75e661
DB
21/* 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
41529bbc 31void fast_export_init(int fd)
c0e6c23d 32{
c5bcbcdc 33 first_commit_done = 0;
41529bbc
DB
34 if (buffer_fdinit(&report_buffer, fd))
35 die_errno("cannot read from file descriptor %d", fd);
c0e6c23d
DB
36}
37
41529bbc
DB
38void fast_export_deinit(void)
39{
40 if (buffer_deinit(&report_buffer))
41 die_errno("error closing fast-import feedback stream");
42}
43
03087971 44void fast_export_delete(const char *path)
c0e6c23d 45{
03087971
DB
46 putchar('D');
47 putchar(' ');
48 quote_c_style(path, NULL, stdout, 0);
49 putchar('\n');
c0e6c23d
DB
50}
51
03087971 52static void fast_export_truncate(const char *path, uint32_t mode)
c0e6c23d 53{
03087971 54 fast_export_modify(path, mode, "inline");
723b7a27 55 printf("data 0\n\n");
c0e6c23d
DB
56}
57
03087971 58void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
c0e6c23d
DB
59{
60 /* Mode must be 100644, 100755, 120000, or 160000. */
723b7a27 61 if (!dataref) {
03087971 62 fast_export_truncate(path, mode);
723b7a27
JN
63 return;
64 }
03087971
DB
65 printf("M %06"PRIo32" %s ", mode, dataref);
66 quote_c_style(path, NULL, stdout, 0);
67 putchar('\n');
c0e6c23d
DB
68}
69
a9a55613 70void fast_export_begin_note(uint32_t revision, const char *author,
8e43a1d0 71 const char *log, unsigned long timestamp, const char *note_ref)
a9a55613 72{
8e43a1d0 73 static int firstnote = 1;
a9a55613 74 size_t loglen = strlen(log);
8e43a1d0 75 printf("commit %s\n", note_ref);
5efc60c1 76 printf("committer %s <%s@%s> %lu +0000\n", author, author, "local", timestamp);
a9a55613
FA
77 printf("data %"PRIuMAX"\n", (uintmax_t)loglen);
78 fwrite(log, loglen, 1, stdout);
8e43a1d0
FA
79 if (firstnote) {
80 if (revision > 1)
81 printf("from %s^0", note_ref);
82 firstnote = 0;
83 }
a9a55613
FA
84 fputc('\n', stdout);
85}
86
3c23953f
DI
87void fast_export_note(const char *committish, const char *dataref)
88{
89 printf("N %s %s\n", dataref, committish);
90}
91
c0e6c23d 92static char gitsvnline[MAX_GITSVN_LINE_LEN];
9ecfa8ae 93void fast_export_begin_commit(uint32_t revision, const char *author,
195b7ca6 94 const struct strbuf *log,
7c5817d3 95 const char *uuid, const char *url,
271fd1fc 96 unsigned long timestamp, const char *local_ref)
c0e6c23d 97{
195b7ca6 98 static const struct strbuf empty = STRBUF_INIT;
c0e6c23d 99 if (!log)
195b7ca6 100 log = &empty;
7c5817d3 101 if (*uuid && *url) {
5418d96d
RJ
102 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
103 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
7c5817d3 104 url, revision, uuid);
c0e6c23d
DB
105 } else {
106 *gitsvnline = '\0';
107 }
271fd1fc 108 printf("commit %s\n", local_ref);
78e1a3ff 109 printf("mark :%"PRIu32"\n", revision);
5efc60c1 110 printf("committer %s <%s@%s> %lu +0000\n",
7c5817d3
DB
111 *author ? author : "nobody",
112 *author ? author : "nobody",
113 *uuid ? uuid : "local", timestamp);
41e6b91f
JN
114 printf("data %"PRIuMAX"\n",
115 (uintmax_t) (log->len + strlen(gitsvnline)));
195b7ca6
JN
116 fwrite(log->buf, log->len, 1, stdout);
117 printf("%s\n", gitsvnline);
c0e6c23d
DB
118 if (!first_commit_done) {
119 if (revision > 1)
dd3f42ad 120 printf("from :%"PRIu32"\n", revision - 1);
c0e6c23d
DB
121 first_commit_done = 1;
122 }
723b7a27 123}
c0e6c23d 124
723b7a27
JN
125void fast_export_end_commit(uint32_t revision)
126{
5418d96d 127 printf("progress Imported commit %"PRIu32".\n\n", revision);
c0e6c23d
DB
128}
129
03087971 130static void ls_from_rev(uint32_t rev, const char *path)
723b7a27
JN
131{
132 /* ls :5 path/to/old/file */
03087971
DB
133 printf("ls :%"PRIu32" ", rev);
134 quote_c_style(path, NULL, stdout, 0);
135 putchar('\n');
723b7a27
JN
136 fflush(stdout);
137}
138
03087971 139static void ls_from_active_commit(const char *path)
723b7a27
JN
140{
141 /* ls "path/to/file" */
142 printf("ls \"");
03087971 143 quote_c_style(path, NULL, stdout, 1);
723b7a27
JN
144 printf("\"\n");
145 fflush(stdout);
146}
147
41529bbc
DB
148static 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
c9d1c8ba
JN
158static 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
7a75e661
DB
165static int parse_cat_response_line(const char *header, off_t *len)
166{
abe27c0c 167 uintmax_t n;
7a75e661
DB
168 const char *type;
169 const char *end;
170
95662315 171 if (ends_with(header, " missing"))
7a75e661 172 return error("cat-blob reports missing blob: %s", header);
53153e83 173 type = strstr(header, " blob ");
7a75e661
DB
174 if (!type)
175 return error("cat-blob header has wrong object type: %s", header);
abe27c0c 176 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
7a75e661
DB
177 if (end == type + strlen(" blob "))
178 return error("cat-blob header does not contain length: %s", header);
abe27c0c
JN
179 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;
7a75e661
DB
184 if (*end)
185 return error("cat-blob header contains garbage after length: %s", header);
186 return 0;
187}
188
abe27c0c
JN
189static 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
7a75e661
DB
195static long apply_delta(off_t len, struct line_buffer *input,
196 const char *old_data, uint32_t old_mode)
197{
198 long ret;
3ac10b2e 199 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
7a75e661
DB
200 FILE *out;
201
202 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
203 die("cannot open temporary file for blob retrieval");
7a75e661
DB
204 if (old_data) {
205 const char *response;
206 printf("cat-blob %s\n", old_data);
207 fflush(stdout);
208 response = get_response_line();
3ac10b2e 209 if (parse_cat_response_line(response, &preimage.max_off))
7a75e661 210 die("invalid cat-blob response: %s", response);
3ac10b2e 211 check_preimage_overflow(preimage.max_off, 1);
7a75e661 212 }
21c7c2d9 213 if (old_mode == S_IFLNK) {
7a75e661 214 strbuf_addstr(&preimage.buf, "link ");
3ac10b2e
JN
215 check_preimage_overflow(preimage.max_off, strlen("link "));
216 preimage.max_off += strlen("link ");
217 check_preimage_overflow(preimage.max_off, 1);
7a75e661
DB
218 }
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. */
3ac10b2e
JN
223 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))
7a75e661
DB
226 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
3c23953f
DI
237void 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
2d54b9ea 244void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input)
c0e6c23d 245{
2d54b9ea 246 assert(len >= 0);
21c7c2d9 247 if (mode == S_IFLNK) {
c0e6c23d 248 /* svn symlink blobs start with "link " */
2d54b9ea
JN
249 if (len < 5)
250 die("invalid dump: symlink too short for \"link\" prefix");
c0e6c23d 251 len -= 5;
c9d1c8ba
JN
252 if (buffer_skip_bytes(input, 5) != 5)
253 die_short_read(input);
c0e6c23d 254 }
2d54b9ea 255 printf("data %"PRIuMAX"\n", (uintmax_t) len);
c9d1c8ba
JN
256 if (buffer_copy_bytes(input, len) != len)
257 die_short_read(input);
c0e6c23d
DB
258 fputc('\n', stdout);
259}
723b7a27
JN
260
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. */
6a0b4438 276 if (response_end - response < (signed) strlen("100644") ||
723b7a27
JN
277 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 ' */
6a0b4438 289 if (response_end - response < (signed) strlen(" blob ") ||
723b7a27
JN
290 (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
03087971 302int fast_export_ls_rev(uint32_t rev, const char *path,
723b7a27
JN
303 uint32_t *mode, struct strbuf *dataref)
304{
03087971 305 ls_from_rev(rev, path);
723b7a27
JN
306 return parse_ls_response(get_response_line(), mode, dataref);
307}
308
03087971 309int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
723b7a27 310{
03087971 311 ls_from_active_commit(path);
723b7a27
JN
312 return parse_ls_response(get_response_line(), mode, dataref);
313}
7a75e661
DB
314
315void fast_export_blob_delta(uint32_t mode,
316 uint32_t old_mode, const char *old_data,
2d54b9ea 317 off_t len, struct line_buffer *input)
7a75e661
DB
318{
319 long postimage_len;
2d54b9ea
JN
320
321 assert(len >= 0);
322 postimage_len = apply_delta(len, input, old_data, old_mode);
21c7c2d9 323 if (mode == S_IFLNK) {
7a75e661
DB
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}