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 "fast_export.h"
8#include "line_buffer.h"
9#include "repo_tree.h"
10#include "string_pool.h"
11#include "strbuf.h"
12
13#define MAX_GITSVN_LINE_LEN 4096
14
15static uint32_t first_commit_done;
16static struct line_buffer report_buffer = LINE_BUFFER_INIT;
17
18void fast_export_init(int fd)
19{
20 if (buffer_fdinit(&report_buffer, fd))
21 die_errno("cannot read from file descriptor %d", fd);
22}
23
24void fast_export_deinit(void)
25{
26 if (buffer_deinit(&report_buffer))
27 die_errno("error closing fast-import feedback stream");
28}
29
30void fast_export_reset(void)
31{
32 buffer_reset(&report_buffer);
33}
34
35void fast_export_delete(uint32_t depth, const uint32_t *path)
36{
37 printf("D \"");
38 pool_print_seq_q(depth, path, '/', stdout);
39 printf("\"\n");
40}
41
42static void fast_export_truncate(uint32_t depth, const uint32_t *path, uint32_t mode)
43{
44 fast_export_modify(depth, path, mode, "inline");
45 printf("data 0\n\n");
46}
47
48void fast_export_modify(uint32_t depth, const uint32_t *path, uint32_t mode,
49 const char *dataref)
50{
51 /* Mode must be 100644, 100755, 120000, or 160000. */
52 if (!dataref) {
53 fast_export_truncate(depth, path, mode);
54 return;
55 }
56 printf("M %06"PRIo32" %s \"", mode, dataref);
57 pool_print_seq_q(depth, path, '/', stdout);
58 printf("\"\n");
59}
60
61static char gitsvnline[MAX_GITSVN_LINE_LEN];
62void 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)
66{
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';
76 }
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;
91 }
92}
93
94void fast_export_end_commit(uint32_t revision)
95{
96 printf("progress Imported commit %"PRIu32".\n\n", revision);
97}
98
99static void ls_from_rev(uint32_t rev, uint32_t depth, const uint32_t *path)
100{
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);
106}
107
108static void ls_from_active_commit(uint32_t depth, const uint32_t *path)
109{
110 /* ls "path/to/file" */
111 printf("ls \"");
112 pool_print_seq_q(depth, path, '/', stdout);
113 printf("\"\n");
114 fflush(stdout);
115}
116
117static const char *get_response_line(void)
118{
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");
125}
126
127static void die_short_read(struct line_buffer *input)
128{
129 if (buffer_ferror(input))
130 die_errno("error reading dump file");
131 die("invalid dump: unexpected end of file");
132}
133
134void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
135{
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);
141 }
142 printf("data %"PRIu32"\n", len);
143 if (buffer_copy_bytes(input, len) != len)
144 die_short_read(input);
145 fputc('\n', stdout);
146}
147
148static int parse_ls_response(const char *response, uint32_t *mode,
149 struct strbuf *dataref)
150{
151 const char *tab;
152 const char *response_end;
153
154 assert(response);
155 response_end = response + strlen(response);
156
157 if (*response == 'm') { /* Missing. */
158 errno = ENOENT;
159 return -1;
160 }
161
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';
173 }
174
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 ");
180
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;
187}
188
189int fast_export_ls_rev(uint32_t rev, uint32_t depth, const uint32_t *path,
190 uint32_t *mode, struct strbuf *dataref)
191{
192 ls_from_rev(rev, depth, path);
193 return parse_ls_response(get_response_line(), mode, dataref);
194}
195
196int fast_export_ls(uint32_t depth, const uint32_t *path,
197 uint32_t *mode, struct strbuf *dataref)
198{
199 ls_from_active_commit(depth, path);
200 return parse_ls_response(get_response_line(), mode, dataref);
201}