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