Merge branch 'db/vcs-svn-incremental' into svn-fe
[git/gitweb.git] / vcs-svn / fast_export.c
blobff980b3a2aad56c13d129bdd8b24091de8981865
1 /*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
6 #include "git-compat-util.h"
7 #include "fast_export.h"
8 #include "line_buffer.h"
9 #include "repo_tree.h"
10 #include "string_pool.h"
11 #include "strbuf.h"
13 #define MAX_GITSVN_LINE_LEN 4096
15 static uint32_t first_commit_done;
16 static struct line_buffer report_buffer = LINE_BUFFER_INIT;
18 void fast_export_init(int fd)
20 if (buffer_fdinit(&report_buffer, fd))
21 die_errno("cannot read from file descriptor %d", fd);
24 void fast_export_deinit(void)
26 if (buffer_deinit(&report_buffer))
27 die_errno("error closing fast-import feedback stream");
30 void fast_export_reset(void)
32 buffer_reset(&report_buffer);
35 void fast_export_delete(uint32_t depth, const uint32_t *path)
37 printf("D \"");
38 pool_print_seq_q(depth, path, '/', stdout);
39 printf("\"\n");
42 static void fast_export_truncate(uint32_t depth, const uint32_t *path, uint32_t mode)
44 fast_export_modify(depth, path, mode, "inline");
45 printf("data 0\n\n");
48 void fast_export_modify(uint32_t depth, const uint32_t *path, uint32_t mode,
49 const char *dataref)
51 /* Mode must be 100644, 100755, 120000, or 160000. */
52 if (!dataref) {
53 fast_export_truncate(depth, path, mode);
54 return;
56 printf("M %06"PRIo32" %s \"", mode, dataref);
57 pool_print_seq_q(depth, path, '/', stdout);
58 printf("\"\n");
61 static char gitsvnline[MAX_GITSVN_LINE_LEN];
62 void fast_export_begin_commit(uint32_t revision, const char *author,
63 const struct strbuf *log,
64 const char *uuid, const char *url,
65 unsigned long timestamp)
67 static const struct strbuf empty = STRBUF_INIT;
68 if (!log)
69 log = ∅
70 if (*uuid && *url) {
71 snprintf(gitsvnline, MAX_GITSVN_LINE_LEN,
72 "\n\ngit-svn-id: %s@%"PRIu32" %s\n",
73 url, revision, uuid);
74 } else {
75 *gitsvnline = '\0';
77 printf("commit refs/heads/master\n");
78 printf("mark :%"PRIu32"\n", revision);
79 printf("committer %s <%s@%s> %ld +0000\n",
80 *author ? author : "nobody",
81 *author ? author : "nobody",
82 *uuid ? uuid : "local", timestamp);
83 printf("data %"PRIuMAX"\n",
84 (uintmax_t) (log->len + strlen(gitsvnline)));
85 fwrite(log->buf, log->len, 1, stdout);
86 printf("%s\n", gitsvnline);
87 if (!first_commit_done) {
88 if (revision > 1)
89 printf("from :%"PRIu32"\n", revision - 1);
90 first_commit_done = 1;
94 void fast_export_end_commit(uint32_t revision)
96 printf("progress Imported commit %"PRIu32".\n\n", revision);
99 static void ls_from_rev(uint32_t rev, uint32_t depth, const uint32_t *path)
101 /* ls :5 path/to/old/file */
102 printf("ls :%"PRIu32" \"", rev);
103 pool_print_seq_q(depth, path, '/', stdout);
104 printf("\"\n");
105 fflush(stdout);
108 static void ls_from_active_commit(uint32_t depth, const uint32_t *path)
110 /* ls "path/to/file" */
111 printf("ls \"");
112 pool_print_seq_q(depth, path, '/', stdout);
113 printf("\"\n");
114 fflush(stdout);
117 static const char *get_response_line(void)
119 const char *line = buffer_read_line(&report_buffer);
120 if (line)
121 return line;
122 if (buffer_ferror(&report_buffer))
123 die_errno("error reading from fast-import");
124 die("unexpected end of fast-import feedback");
127 static void die_short_read(struct line_buffer *input)
129 if (buffer_ferror(input))
130 die_errno("error reading dump file");
131 die("invalid dump: unexpected end of file");
134 void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
136 if (mode == REPO_MODE_LNK) {
137 /* svn symlink blobs start with "link " */
138 len -= 5;
139 if (buffer_skip_bytes(input, 5) != 5)
140 die_short_read(input);
142 printf("data %"PRIu32"\n", len);
143 if (buffer_copy_bytes(input, len) != len)
144 die_short_read(input);
145 fputc('\n', stdout);
148 static int parse_ls_response(const char *response, uint32_t *mode,
149 struct strbuf *dataref)
151 const char *tab;
152 const char *response_end;
154 assert(response);
155 response_end = response + strlen(response);
157 if (*response == 'm') { /* Missing. */
158 errno = ENOENT;
159 return -1;
162 /* Mode. */
163 if (response_end - response < strlen("100644") ||
164 response[strlen("100644")] != ' ')
165 die("invalid ls response: missing mode: %s", response);
166 *mode = 0;
167 for (; *response != ' '; response++) {
168 char ch = *response;
169 if (ch < '0' || ch > '7')
170 die("invalid ls response: mode is not octal: %s", response);
171 *mode *= 8;
172 *mode += ch - '0';
175 /* ' blob ' or ' tree ' */
176 if (response_end - response < strlen(" blob ") ||
177 (response[1] != 'b' && response[1] != 't'))
178 die("unexpected ls response: not a tree or blob: %s", response);
179 response += strlen(" blob ");
181 /* Dataref. */
182 tab = memchr(response, '\t', response_end - response);
183 if (!tab)
184 die("invalid ls response: missing tab: %s", response);
185 strbuf_add(dataref, response, tab - response);
186 return 0;
189 int fast_export_ls_rev(uint32_t rev, uint32_t depth, const uint32_t *path,
190 uint32_t *mode, struct strbuf *dataref)
192 ls_from_rev(rev, depth, path);
193 return parse_ls_response(get_response_line(), mode, dataref);
196 int fast_export_ls(uint32_t depth, const uint32_t *path,
197 uint32_t *mode, struct strbuf *dataref)
199 ls_from_active_commit(depth, path);
200 return parse_ls_response(get_response_line(), mode, dataref);