]> git.ipfire.org Git - thirdparty/git.git/blame - vcs-svn/fast_export.c
vcs-svn: add fast_export_note to create notes
[thirdparty/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
6#include "git-compat-util.h"
03087971
DB
7#include "strbuf.h"
8#include "quote.h"
c0e6c23d 9#include "fast_export.h"
c0e6c23d 10#include "repo_tree.h"
723b7a27 11#include "strbuf.h"
7a75e661
DB
12#include "svndiff.h"
13#include "sliding_window.h"
14#include "line_buffer.h"
c0e6c23d
DB
15
16#define MAX_GITSVN_LINE_LEN 4096
17
18static uint32_t first_commit_done;
7a75e661 19static struct line_buffer postimage = LINE_BUFFER_INIT;
41529bbc 20static struct line_buffer report_buffer = LINE_BUFFER_INIT;
c0e6c23d 21
7a75e661
DB
22/* NEEDSWORK: move to fast_export_init() */
23static int init_postimage(void)
24{
25 static int postimage_initialized;
26 if (postimage_initialized)
27 return 0;
28 postimage_initialized = 1;
29 return buffer_tmpfile_init(&postimage);
30}
31
41529bbc 32void fast_export_init(int fd)
c0e6c23d 33{
c5bcbcdc 34 first_commit_done = 0;
41529bbc
DB
35 if (buffer_fdinit(&report_buffer, fd))
36 die_errno("cannot read from file descriptor %d", fd);
c0e6c23d
DB
37}
38
41529bbc
DB
39void fast_export_deinit(void)
40{
41 if (buffer_deinit(&report_buffer))
42 die_errno("error closing fast-import feedback stream");
43}
44
03087971 45void fast_export_delete(const char *path)
c0e6c23d 46{
03087971
DB
47 putchar('D');
48 putchar(' ');
49 quote_c_style(path, NULL, stdout, 0);
50 putchar('\n');
c0e6c23d
DB
51}
52
03087971 53static void fast_export_truncate(const char *path, uint32_t mode)
c0e6c23d 54{
03087971 55 fast_export_modify(path, mode, "inline");
723b7a27 56 printf("data 0\n\n");
c0e6c23d
DB
57}
58
03087971 59void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
c0e6c23d
DB
60{
61 /* Mode must be 100644, 100755, 120000, or 160000. */
723b7a27 62 if (!dataref) {
03087971 63 fast_export_truncate(path, mode);
723b7a27
JN
64 return;
65 }
03087971
DB
66 printf("M %06"PRIo32" %s ", mode, dataref);
67 quote_c_style(path, NULL, stdout, 0);
68 putchar('\n');
c0e6c23d
DB
69}
70
3c23953f
DI
71void fast_export_note(const char *committish, const char *dataref)
72{
73 printf("N %s %s\n", dataref, committish);
74}
75
c0e6c23d 76static char gitsvnline[MAX_GITSVN_LINE_LEN];
9ecfa8ae 77void fast_export_begin_commit(uint32_t revision, const char *author,
195b7ca6 78 const struct strbuf *log,
7c5817d3 79 const char *uuid, const char *url,
271fd1fc 80 unsigned long timestamp, const char *local_ref)
c0e6c23d 81{
195b7ca6 82 static const struct strbuf empty = STRBUF_INIT;
c0e6c23d 83 if (!log)
195b7ca6 84 log = ∅
7c5817d3 85 if (*uuid && *url) {
5418d96d
RJ
86 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
87 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
7c5817d3 88 url, revision, uuid);
c0e6c23d
DB
89 } else {
90 *gitsvnline = '\0';
91 }
271fd1fc 92 printf("commit %s\n", local_ref);
78e1a3ff 93 printf("mark :%"PRIu32"\n", revision);
c0e6c23d 94 printf("committer %s <%s@%s> %ld +0000\n",
7c5817d3
DB
95 *author ? author : "nobody",
96 *author ? author : "nobody",
97 *uuid ? uuid : "local", timestamp);
41e6b91f
JN
98 printf("data %"PRIuMAX"\n",
99 (uintmax_t) (log->len + strlen(gitsvnline)));
195b7ca6
JN
100 fwrite(log->buf, log->len, 1, stdout);
101 printf("%s\n", gitsvnline);
c0e6c23d
DB
102 if (!first_commit_done) {
103 if (revision > 1)
dd3f42ad 104 printf("from :%"PRIu32"\n", revision - 1);
c0e6c23d
DB
105 first_commit_done = 1;
106 }
723b7a27 107}
c0e6c23d 108
723b7a27
JN
109void fast_export_end_commit(uint32_t revision)
110{
5418d96d 111 printf("progress Imported commit %"PRIu32".\n\n", revision);
c0e6c23d
DB
112}
113
03087971 114static void ls_from_rev(uint32_t rev, const char *path)
723b7a27
JN
115{
116 /* ls :5 path/to/old/file */
03087971
DB
117 printf("ls :%"PRIu32" ", rev);
118 quote_c_style(path, NULL, stdout, 0);
119 putchar('\n');
723b7a27
JN
120 fflush(stdout);
121}
122
03087971 123static void ls_from_active_commit(const char *path)
723b7a27
JN
124{
125 /* ls "path/to/file" */
126 printf("ls \"");
03087971 127 quote_c_style(path, NULL, stdout, 1);
723b7a27
JN
128 printf("\"\n");
129 fflush(stdout);
130}
131
41529bbc
DB
132static const char *get_response_line(void)
133{
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");
140}
141
c9d1c8ba
JN
142static void die_short_read(struct line_buffer *input)
143{
144 if (buffer_ferror(input))
145 die_errno("error reading dump file");
146 die("invalid dump: unexpected end of file");
147}
148
7a75e661
DB
149static int ends_with(const char *s, size_t len, const char *suffix)
150{
151 const size_t suffixlen = strlen(suffix);
152 if (len < suffixlen)
153 return 0;
154 return !memcmp(s + len - suffixlen, suffix, suffixlen);
155}
156
157static int parse_cat_response_line(const char *header, off_t *len)
158{
159 size_t headerlen = strlen(header);
abe27c0c 160 uintmax_t n;
7a75e661
DB
161 const char *type;
162 const char *end;
163
164 if (ends_with(header, headerlen, " missing"))
165 return error("cat-blob reports missing blob: %s", header);
53153e83 166 type = strstr(header, " blob ");
7a75e661
DB
167 if (!type)
168 return error("cat-blob header has wrong object type: %s", header);
abe27c0c 169 n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
7a75e661
DB
170 if (end == type + strlen(" blob "))
171 return error("cat-blob header does not contain length: %s", header);
abe27c0c
JN
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;
7a75e661
DB
177 if (*end)
178 return error("cat-blob header contains garbage after length: %s", header);
179 return 0;
180}
181
abe27c0c
JN
182static void check_preimage_overflow(off_t a, off_t b)
183{
184 if (signed_add_overflows(a, b))
185 die("blob too large for current definition of off_t");
186}
187
7a75e661
DB
188static long apply_delta(off_t len, struct line_buffer *input,
189 const char *old_data, uint32_t old_mode)
190{
191 long ret;
3ac10b2e 192 struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
7a75e661
DB
193 FILE *out;
194
195 if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
196 die("cannot open temporary file for blob retrieval");
7a75e661
DB
197 if (old_data) {
198 const char *response;
199 printf("cat-blob %s\n", old_data);
200 fflush(stdout);
201 response = get_response_line();
3ac10b2e 202 if (parse_cat_response_line(response, &preimage.max_off))
7a75e661 203 die("invalid cat-blob response: %s", response);
3ac10b2e 204 check_preimage_overflow(preimage.max_off, 1);
7a75e661
DB
205 }
206 if (old_mode == REPO_MODE_LNK) {
207 strbuf_addstr(&preimage.buf, "link ");
3ac10b2e
JN
208 check_preimage_overflow(preimage.max_off, strlen("link "));
209 preimage.max_off += strlen("link ");
210 check_preimage_overflow(preimage.max_off, 1);
7a75e661
DB
211 }
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. */
3ac10b2e
JN
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))
7a75e661
DB
219 die("cannot seek to end of input");
220 if (preimage.buf.buf[0] != '\n')
221 die("missing newline after cat-blob response");
222 }
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;
228}
229
3c23953f
DI
230void fast_export_buf_to_data(const struct strbuf *data)
231{
232 printf("data %"PRIuMAX"\n", (uintmax_t)data->len);
233 fwrite(data->buf, data->len, 1, stdout);
234 fputc('\n', stdout);
235}
236
2d54b9ea 237void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input)
c0e6c23d 238{
2d54b9ea 239 assert(len >= 0);
c0e6c23d
DB
240 if (mode == REPO_MODE_LNK) {
241 /* svn symlink blobs start with "link " */
2d54b9ea
JN
242 if (len < 5)
243 die("invalid dump: symlink too short for \"link\" prefix");
c0e6c23d 244 len -= 5;
c9d1c8ba
JN
245 if (buffer_skip_bytes(input, 5) != 5)
246 die_short_read(input);
c0e6c23d 247 }
2d54b9ea 248 printf("data %"PRIuMAX"\n", (uintmax_t) len);
c9d1c8ba
JN
249 if (buffer_copy_bytes(input, len) != len)
250 die_short_read(input);
c0e6c23d
DB
251 fputc('\n', stdout);
252}
723b7a27
JN
253
254static int parse_ls_response(const char *response, uint32_t *mode,
255 struct strbuf *dataref)
256{
257 const char *tab;
258 const char *response_end;
259
260 assert(response);
261 response_end = response + strlen(response);
262
263 if (*response == 'm') { /* Missing. */
264 errno = ENOENT;
265 return -1;
266 }
267
268 /* Mode. */
6a0b4438 269 if (response_end - response < (signed) strlen("100644") ||
723b7a27
JN
270 response[strlen("100644")] != ' ')
271 die("invalid ls response: missing mode: %s", response);
272 *mode = 0;
273 for (; *response != ' '; response++) {
274 char ch = *response;
275 if (ch < '0' || ch > '7')
276 die("invalid ls response: mode is not octal: %s", response);
277 *mode *= 8;
278 *mode += ch - '0';
279 }
280
281 /* ' blob ' or ' tree ' */
6a0b4438 282 if (response_end - response < (signed) strlen(" blob ") ||
723b7a27
JN
283 (response[1] != 'b' && response[1] != 't'))
284 die("unexpected ls response: not a tree or blob: %s", response);
285 response += strlen(" blob ");
286
287 /* Dataref. */
288 tab = memchr(response, '\t', response_end - response);
289 if (!tab)
290 die("invalid ls response: missing tab: %s", response);
291 strbuf_add(dataref, response, tab - response);
292 return 0;
293}
294
03087971 295int fast_export_ls_rev(uint32_t rev, const char *path,
723b7a27
JN
296 uint32_t *mode, struct strbuf *dataref)
297{
03087971 298 ls_from_rev(rev, path);
723b7a27
JN
299 return parse_ls_response(get_response_line(), mode, dataref);
300}
301
03087971 302int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
723b7a27 303{
03087971 304 ls_from_active_commit(path);
723b7a27
JN
305 return parse_ls_response(get_response_line(), mode, dataref);
306}
7a75e661
DB
307
308void fast_export_blob_delta(uint32_t mode,
309 uint32_t old_mode, const char *old_data,
2d54b9ea 310 off_t len, struct line_buffer *input)
7a75e661
DB
311{
312 long postimage_len;
2d54b9ea
JN
313
314 assert(len >= 0);
315 postimage_len = apply_delta(len, input, old_data, old_mode);
7a75e661
DB
316 if (mode == REPO_MODE_LNK) {
317 buffer_skip_bytes(&postimage, strlen("link "));
318 postimage_len -= strlen("link ");
319 }
320 printf("data %ld\n", postimage_len);
321 buffer_copy_bytes(&postimage, postimage_len);
322 fputc('\n', stdout);
323}