Code

edc658d4fe9567f3fdf5ebc5d7c7fd7ef174d5c3
[git.git] / vcs-svn / fast_export.c
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
17 #define REPORT_FILENO 3
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;
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 }
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 }
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 }
48 void fast_export_deinit(void)
49 {
50         if (buffer_deinit(&report_buffer))
51                 die_errno("error closing fast-import feedback stream");
52 }
54 void fast_export_reset(void)
55 {
56         buffer_reset(&report_buffer);
57 }
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 }
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 }
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 }
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         }
118 void fast_export_end_commit(uint32_t revision)
120         printf("progress Imported commit %"PRIu32".\n\n", revision);
123 static void ls_from_rev(uint32_t rev, const char *path)
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);
132 static void ls_from_active_commit(const char *path)
134         /* ls "path/to/file" */
135         printf("ls \"");
136         quote_c_style(path, NULL, stdout, 1);
137         printf("\"\n");
138         fflush(stdout);
141 static const char *get_response_line(void)
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");
151 static void die_short_read(struct line_buffer *input)
153         if (buffer_ferror(input))
154                 die_errno("error reading dump file");
155         die("invalid dump: unexpected end of file");
158 static int ends_with(const char *s, size_t len, const char *suffix)
160         const size_t suffixlen = strlen(suffix);
161         if (len < suffixlen)
162                 return 0;
163         return !memcmp(s + len - suffixlen, suffix, suffixlen);
166 static int parse_cat_response_line(const char *header, off_t *len)
168         size_t headerlen = strlen(header);
169         const char *type;
170         const char *end;
172         if (ends_with(header, headerlen, " missing"))
173                 return error("cat-blob reports missing blob: %s", header);
174         type = memmem(header, headerlen, " blob ", strlen(" blob "));
175         if (!type)
176                 return error("cat-blob header has wrong object type: %s", header);
177         *len = strtoumax(type + strlen(" blob "), (char **) &end, 10);
178         if (end == type + strlen(" blob "))
179                 return error("cat-blob header does not contain length: %s", header);
180         if (*end)
181                 return error("cat-blob header contains garbage after length: %s", header);
182         return 0;
185 static long apply_delta(off_t len, struct line_buffer *input,
186                         const char *old_data, uint32_t old_mode)
188         long ret;
189         off_t preimage_len = 0;
190         struct sliding_view preimage = SLIDING_VIEW_INIT(&report_buffer, -1);
191         FILE *out;
193         if (init_postimage() || !(out = buffer_tmpfile_rewind(&postimage)))
194                 die("cannot open temporary file for blob retrieval");
195         if (init_report_buffer(REPORT_FILENO))
196                 die("cannot open fd 3 for feedback from fast-import");
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_len))
203                         die("invalid cat-blob response: %s", response);
204         }
205         if (old_mode == REPO_MODE_LNK) {
206                 strbuf_addstr(&preimage.buf, "link ");
207                 preimage_len += strlen("link ");
208         }
209         if (svndiff0_apply(input, len, &preimage, out))
210                 die("cannot apply delta");
211         if (old_data) {
212                 /* Read the remainder of preimage and trailing newline. */
213                 if (move_window(&preimage, preimage_len, 1))
214                         die("cannot seek to end of input");
215                 if (preimage.buf.buf[0] != '\n')
216                         die("missing newline after cat-blob response");
217         }
218         ret = buffer_tmpfile_prepare_to_read(&postimage);
219         if (ret < 0)
220                 die("cannot read temporary file for blob retrieval");
221         strbuf_release(&preimage.buf);
222         return ret;
225 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
227         if (mode == REPO_MODE_LNK) {
228                 /* svn symlink blobs start with "link " */
229                 len -= 5;
230                 if (buffer_skip_bytes(input, 5) != 5)
231                         die_short_read(input);
232         }
233         printf("data %"PRIu32"\n", len);
234         if (buffer_copy_bytes(input, len) != len)
235                 die_short_read(input);
236         fputc('\n', stdout);
239 static int parse_ls_response(const char *response, uint32_t *mode,
240                                         struct strbuf *dataref)
242         const char *tab;
243         const char *response_end;
245         assert(response);
246         response_end = response + strlen(response);
248         if (*response == 'm') { /* Missing. */
249                 errno = ENOENT;
250                 return -1;
251         }
253         /* Mode. */
254         if (response_end - response < strlen("100644") ||
255             response[strlen("100644")] != ' ')
256                 die("invalid ls response: missing mode: %s", response);
257         *mode = 0;
258         for (; *response != ' '; response++) {
259                 char ch = *response;
260                 if (ch < '0' || ch > '7')
261                         die("invalid ls response: mode is not octal: %s", response);
262                 *mode *= 8;
263                 *mode += ch - '0';
264         }
266         /* ' blob ' or ' tree ' */
267         if (response_end - response < strlen(" blob ") ||
268             (response[1] != 'b' && response[1] != 't'))
269                 die("unexpected ls response: not a tree or blob: %s", response);
270         response += strlen(" blob ");
272         /* Dataref. */
273         tab = memchr(response, '\t', response_end - response);
274         if (!tab)
275                 die("invalid ls response: missing tab: %s", response);
276         strbuf_add(dataref, response, tab - response);
277         return 0;
280 int fast_export_ls_rev(uint32_t rev, const char *path,
281                                 uint32_t *mode, struct strbuf *dataref)
283         ls_from_rev(rev, path);
284         return parse_ls_response(get_response_line(), mode, dataref);
287 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
289         ls_from_active_commit(path);
290         return parse_ls_response(get_response_line(), mode, dataref);
293 void fast_export_blob_delta(uint32_t mode,
294                                 uint32_t old_mode, const char *old_data,
295                                 uint32_t len, struct line_buffer *input)
297         long postimage_len;
298         if (len > maximum_signed_value_of_type(off_t))
299                 die("enormous delta");
300         postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
301         if (mode == REPO_MODE_LNK) {
302                 buffer_skip_bytes(&postimage, strlen("link "));
303                 postimage_len -= strlen("link ");
304         }
305         printf("data %ld\n", postimage_len);
306         buffer_copy_bytes(&postimage, postimage_len);
307         fputc('\n', stdout);