vcs-svn: pass paths through to fast-import
[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 "line_buffer.h"
11 #include "repo_tree.h"
12 #include "strbuf.h"
13
14 #define MAX_GITSVN_LINE_LEN 4096
15
16 static uint32_t first_commit_done;
17 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
18
19 void fast_export_init(int fd)
20 {
21         if (buffer_fdinit(&report_buffer, fd))
22                 die_errno("cannot read from file descriptor %d", fd);
23 }
24
25 void fast_export_deinit(void)
26 {
27         if (buffer_deinit(&report_buffer))
28                 die_errno("error closing fast-import feedback stream");
29 }
30
31 void fast_export_reset(void)
32 {
33         buffer_reset(&report_buffer);
34 }
35
36 void fast_export_delete(const char *path)
37 {
38         putchar('D');
39         putchar(' ');
40         quote_c_style(path, NULL, stdout, 0);
41         putchar('\n');
42 }
43
44 static void fast_export_truncate(const char *path, uint32_t mode)
45 {
46         fast_export_modify(path, mode, "inline");
47         printf("data 0\n\n");
48 }
49
50 void fast_export_modify(const char *path, uint32_t mode, const char *dataref)
51 {
52         /* Mode must be 100644, 100755, 120000, or 160000. */
53         if (!dataref) {
54                 fast_export_truncate(path, mode);
55                 return;
56         }
57         printf("M %06"PRIo32" %s ", mode, dataref);
58         quote_c_style(path, NULL, stdout, 0);
59         putchar('\n');
60 }
61
62 static char gitsvnline[MAX_GITSVN_LINE_LEN];
63 void fast_export_begin_commit(uint32_t revision, const char *author, char *log,
64                         const char *uuid, const char *url,
65                         unsigned long timestamp)
66 {
67         if (!log)
68                 log = "";
69         if (*uuid && *url) {
70                 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
71                                 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
72                                  url, revision, uuid);
73         } else {
74                 *gitsvnline = '\0';
75         }
76         printf("commit refs/heads/master\n");
77         printf("mark :%"PRIu32"\n", revision);
78         printf("committer %s <%s@%s> %ld +0000\n",
79                    *author ? author : "nobody",
80                    *author ? author : "nobody",
81                    *uuid ? uuid : "local", timestamp);
82         printf("data %"PRIu32"\n%s%s\n",
83                    (uint32_t) (strlen(log) + strlen(gitsvnline)),
84                    log, gitsvnline);
85         if (!first_commit_done) {
86                 if (revision > 1)
87                         printf("from :%"PRIu32"\n", revision - 1);
88                 first_commit_done = 1;
89         }
90 }
91
92 void fast_export_end_commit(uint32_t revision)
93 {
94         printf("progress Imported commit %"PRIu32".\n\n", revision);
95 }
96
97 static void ls_from_rev(uint32_t rev, const char *path)
98 {
99         /* ls :5 path/to/old/file */
100         printf("ls :%"PRIu32" ", rev);
101         quote_c_style(path, NULL, stdout, 0);
102         putchar('\n');
103         fflush(stdout);
104 }
105
106 static void ls_from_active_commit(const char *path)
107 {
108         /* ls "path/to/file" */
109         printf("ls \"");
110         quote_c_style(path, NULL, stdout, 1);
111         printf("\"\n");
112         fflush(stdout);
113 }
114
115 static const char *get_response_line(void)
116 {
117         const char *line = buffer_read_line(&report_buffer);
118         if (line)
119                 return line;
120         if (buffer_ferror(&report_buffer))
121                 die_errno("error reading from fast-import");
122         die("unexpected end of fast-import feedback");
123 }
124
125 static void die_short_read(struct line_buffer *input)
126 {
127         if (buffer_ferror(input))
128                 die_errno("error reading dump file");
129         die("invalid dump: unexpected end of file");
130 }
131
132 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
133 {
134         if (mode == REPO_MODE_LNK) {
135                 /* svn symlink blobs start with "link " */
136                 len -= 5;
137                 if (buffer_skip_bytes(input, 5) != 5)
138                         die_short_read(input);
139         }
140         printf("data %"PRIu32"\n", len);
141         if (buffer_copy_bytes(input, len) != len)
142                 die_short_read(input);
143         fputc('\n', stdout);
144 }
145
146 static int parse_ls_response(const char *response, uint32_t *mode,
147                                         struct strbuf *dataref)
148 {
149         const char *tab;
150         const char *response_end;
151
152         assert(response);
153         response_end = response + strlen(response);
154
155         if (*response == 'm') { /* Missing. */
156                 errno = ENOENT;
157                 return -1;
158         }
159
160         /* Mode. */
161         if (response_end - response < strlen("100644") ||
162             response[strlen("100644")] != ' ')
163                 die("invalid ls response: missing mode: %s", response);
164         *mode = 0;
165         for (; *response != ' '; response++) {
166                 char ch = *response;
167                 if (ch < '0' || ch > '7')
168                         die("invalid ls response: mode is not octal: %s", response);
169                 *mode *= 8;
170                 *mode += ch - '0';
171         }
172
173         /* ' blob ' or ' tree ' */
174         if (response_end - response < strlen(" blob ") ||
175             (response[1] != 'b' && response[1] != 't'))
176                 die("unexpected ls response: not a tree or blob: %s", response);
177         response += strlen(" blob ");
178
179         /* Dataref. */
180         tab = memchr(response, '\t', response_end - response);
181         if (!tab)
182                 die("invalid ls response: missing tab: %s", response);
183         strbuf_add(dataref, response, tab - response);
184         return 0;
185 }
186
187 int fast_export_ls_rev(uint32_t rev, const char *path,
188                                 uint32_t *mode, struct strbuf *dataref)
189 {
190         ls_from_rev(rev, path);
191         return parse_ls_response(get_response_line(), mode, dataref);
192 }
193
194 int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
195 {
196         ls_from_active_commit(path);
197         return parse_ls_response(get_response_line(), mode, dataref);
198 }