Merge branch 'db/text-delta' into svn-fe
[git] / vcs-svn / fast_export.c
1 /*
2  * Licensed under a two-clause BSD-style license.
3  * See LICENSE for details.
4  */
5
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"
15
16 #define MAX_GITSVN_LINE_LEN 4096
17 #define REPORT_FILENO 3
18
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;
22
23 /* NEEDSWORK: move to fast_export_init() */
24 static int init_postimage(void)
25 {
26         static int postimage_initialized;
27         if (postimage_initialized)
28                 return 0;
29         postimage_initialized = 1;
30         return buffer_tmpfile_init(&postimage);
31 }
32
33 static int init_report_buffer(int fd)
34 {
35         static int report_buffer_initialized;
36         if (report_buffer_initialized)
37                 return 0;
38         report_buffer_initialized = 1;
39         return buffer_fdinit(&report_buffer, fd);
40 }
41
42 void fast_export_init(int fd)
43 {
44         if (buffer_fdinit(&report_buffer, fd))
45                 die_errno("cannot read from file descriptor %d", fd);
46 }
47
48 void fast_export_deinit(void)
49 {
50         if (buffer_deinit(&report_buffer))
51                 die_errno("error closing fast-import feedback stream");
52 }
53
54 void fast_export_reset(void)
55 {
56         buffer_reset(&report_buffer);
57 }
58
59 void fast_export_delete(const char *path)
60 {
61         putchar('D');
62         putchar(' ');
63         quote_c_style(path, NULL, stdout, 0);
64         putchar('\n');
65 }
66
67 static void fast_export_truncate(const char *path, uint32_t mode)
68 {
69         fast_export_modify(path, mode, "inline");
70         printf("data 0\n\n");
71 }
72
73 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
74 {
75         /* Mode must be 100644, 100755, 120000, or 160000. */
76         if (!dataref) {
77                 fast_export_truncate(path, mode);
78                 return;
79         }
80         printf("M %06"PRIo32" %s ", mode, dataref);
81         quote_c_style(path, NULL, stdout, 0);
82         putchar('\n');
83 }
84
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)
90 {
91         static const struct strbuf empty = STRBUF_INIT;
92         if (!log)
93                 log = ∅
94         if (*uuid && *url) {
95                 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
96                                 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
97                                  url, revision, uuid);
98         } else {
99                 *gitsvnline = '\0';
100         }
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) {
112                 if (revision > 1)
113                         printf("from :%"PRIu32"\n", revision - 1);
114                 first_commit_done = 1;
115         }
116 }
117
118 void fast_export_end_commit(uint32_t revision)
119 {
120         printf("progress Imported commit %"PRIu32".\n\n", revision);
121 }
122
123 static void ls_from_rev(uint32_t rev, const char *path)
124 {
125         /* ls :5 path/to/old/file */
126         printf("ls :%"PRIu32" ", rev);
127         quote_c_style(path, NULL, stdout, 0);
128         putchar('\n');
129         fflush(stdout);
130 }
131
132 static void ls_from_active_commit(const char *path)
133 {
134         /* ls "path/to/file" */
135         printf("ls \"");
136         quote_c_style(path, NULL, stdout, 1);
137         printf("\"\n");
138         fflush(stdout);
139 }
140
141 static const char *get_response_line(void)
142 {
143         const char *line = buffer_read_line(&report_buffer);
144         if (line)
145                 return line;
146         if (buffer_ferror(&report_buffer))
147                 die_errno("error reading from fast-import");
148         die("unexpected end of fast-import feedback");
149 }
150
151 static void die_short_read(struct line_buffer *input)
152 {
153         if (buffer_ferror(input))
154                 die_errno("error reading dump file");
155         die("invalid dump: unexpected end of file");
156 }
157
158 static int ends_with(const char *s, size_t len, const char *suffix)
159 {
160         const size_t suffixlen = strlen(suffix);
161         if (len < suffixlen)
162                 return 0;
163         return !memcmp(s + len - suffixlen, suffix, suffixlen);
164 }
165
166 static int parse_cat_response_line(const char *header, off_t *len)
167 {
168         size_t headerlen = strlen(header);
169         uintmax_t n;
170         const char *type;
171         const char *end;
172
173         if (ends_with(header, headerlen, " missing"))
174                 return error("cat-blob reports missing blob: %s", header);
175         type = memmem(header, headerlen, " blob ", strlen(" blob "));
176         if (!type)
177                 return error("cat-blob header has wrong object type: %s", header);
178         n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
179         if (end == type + strlen(" blob "))
180                 return error("cat-blob header does not contain length: %s", header);
181         if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
182                 return error("cat-blob header contains negative length: %s", header);
183         if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
184                 return error("blob too large for current definition of off_t");
185         *len = n;
186         if (*end)
187                 return error("cat-blob header contains garbage after length: %s", header);
188         return 0;
189 }
190
191 static void check_preimage_overflow(off_t a, off_t b)
192 {
193         if (signed_add_overflows(a, b))
194                 die("blob too large for current definition of off_t");
195 }
196
197 static long apply_delta(off_t len, struct line_buffer *input,
198                         const char *old_data, uint32_t old_mode)
199 {
200         long ret;
201         struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, 0);
202         FILE *out;
203
204         if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
205                 die("cannot open temporary file for blob retrieval");
206         if (init_report_buffer(REPORT_FILENO))
207                 die("cannot open fd 3 for feedback from fast-import");
208         if (old_data) {
209                 const char *response;
210                 printf("cat-blob %s\n", old_data);
211                 fflush(stdout);
212                 response = get_response_line();
213                 if (parse_cat_response_line(response, &preimage.max_off))
214                         die("invalid cat-blob response: %s", response);
215                 check_preimage_overflow(preimage.max_off, 1);
216         }
217         if (old_mode == REPO_MODE_LNK) {
218                 strbuf_addstr(&preimage.buf, "link ");
219                 check_preimage_overflow(preimage.max_off, strlen("link "));
220                 preimage.max_off += strlen("link ");
221                 check_preimage_overflow(preimage.max_off, 1);
222         }
223         if (svndiff0_apply(input, len, &preimage, out))
224                 die("cannot apply delta");
225         if (old_data) {
226                 /* Read the remainder of preimage and trailing newline. */
227                 assert(!signed_add_overflows(preimage.max_off, 1));
228                 preimage.max_off++;     /* room for newline */
229                 if (move_window(&preimage, preimage.max_off - 1, 1))
230                         die("cannot seek to end of input");
231                 if (preimage.buf.buf[0] != '\n')
232                         die("missing newline after cat-blob response");
233         }
234         ret = buffer_tmpfile_prepare_to_read(&postimage);
235         if (ret < 0)
236                 die("cannot read temporary file for blob retrieval");
237         strbuf_release(&preimage.buf);
238         return ret;
239 }
240
241 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
242 {
243         if (mode == REPO_MODE_LNK) {
244                 /* svn symlink blobs start with "link " */
245                 len -= 5;
246                 if (buffer_skip_bytes(input, 5) != 5)
247                         die_short_read(input);
248         }
249         printf("data %"PRIu32"\n", len);
250         if (buffer_copy_bytes(input, len) != len)
251                 die_short_read(input);
252         fputc('\n', stdout);
253 }
254
255 static int parse_ls_response(const char *response, uint32_t *mode,
256                                         struct strbuf *dataref)
257 {
258         const char *tab;
259         const char *response_end;
260
261         assert(response);
262         response_end = response + strlen(response);
263
264         if (*response == 'm') { /* Missing. */
265                 errno = ENOENT;
266                 return -1;
267         }
268
269         /* Mode. */
270         if (response_end - response < strlen("100644") ||
271             response[strlen("100644")] != ' ')
272                 die("invalid ls response: missing mode: %s", response);
273         *mode = 0;
274         for (; *response != ' '; response++) {
275                 char ch = *response;
276                 if (ch < '0' || ch > '7')
277                         die("invalid ls response: mode is not octal: %s", response);
278                 *mode *= 8;
279                 *mode += ch - '0';
280         }
281
282         /* ' blob ' or ' tree ' */
283         if (response_end - response < strlen(" blob ") ||
284             (response[1] != 'b' && response[1] != 't'))
285                 die("unexpected ls response: not a tree or blob: %s", response);
286         response += strlen(" blob ");
287
288         /* Dataref. */
289         tab = memchr(response, '\t', response_end - response);
290         if (!tab)
291                 die("invalid ls response: missing tab: %s", response);
292         strbuf_add(dataref, response, tab - response);
293         return 0;
294 }
295
296 int fast_export_ls_rev(uint32_t rev, const char *path,
297                                 uint32_t *mode, struct strbuf *dataref)
298 {
299         ls_from_rev(rev, path);
300         return parse_ls_response(get_response_line(), mode, dataref);
301 }
302
303 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
304 {
305         ls_from_active_commit(path);
306         return parse_ls_response(get_response_line(), mode, dataref);
307 }
308
309 void fast_export_blob_delta(uint32_t mode,
310                                 uint32_t old_mode, const char *old_data,
311                                 uint32_t len, struct line_buffer *input)
312 {
313         long postimage_len;
314         if (len > maximum_signed_value_of_type(off_t))
315                 die("enormous delta");
316         postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
317         if (mode == REPO_MODE_LNK) {
318                 buffer_skip_bytes(&postimage, strlen("link "));
319                 postimage_len -= strlen("link ");
320         }
321         printf("data %ld\n", postimage_len);
322         buffer_copy_bytes(&postimage, postimage_len);
323         fputc('\n', stdout);
324 }