2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
6 #include "git-compat-util.h"
7 #include "fast_export.h"
8 #include "line_buffer.h"
10 #include "string_pool.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
)
38 pool_print_seq_q(depth
, path
, '/', stdout
);
42 static void fast_export_truncate(uint32_t depth
, const uint32_t *path
, uint32_t mode
)
44 fast_export_modify(depth
, path
, mode
, "inline");
48 void fast_export_modify(uint32_t depth
, const uint32_t *path
, uint32_t mode
,
51 /* Mode must be 100644, 100755, 120000, or 160000. */
53 fast_export_truncate(depth
, path
, mode
);
56 printf("M %06"PRIo32
" %s \"", mode
, dataref
);
57 pool_print_seq_q(depth
, path
, '/', stdout
);
61 static char gitsvnline
[MAX_GITSVN_LINE_LEN
];
62 void fast_export_begin_commit(uint32_t revision
, uint32_t author
, char *log
,
63 uint32_t uuid
, uint32_t url
,
64 unsigned long timestamp
)
69 snprintf(gitsvnline
, MAX_GITSVN_LINE_LEN
,
70 "\n\ngit-svn-id: %s@%"PRIu32
" %s\n",
71 pool_fetch(url
), revision
, pool_fetch(uuid
));
75 printf("commit refs/heads/master\n");
76 printf("mark :%"PRIu32
"\n", revision
);
77 printf("committer %s <%s@%s> %ld +0000\n",
78 ~author
? pool_fetch(author
) : "nobody",
79 ~author
? pool_fetch(author
) : "nobody",
80 ~uuid
? pool_fetch(uuid
) : "local", timestamp
);
81 printf("data %"PRIu32
"\n%s%s\n",
82 (uint32_t) (strlen(log
) + strlen(gitsvnline
)),
84 if (!first_commit_done
) {
86 printf("from :%"PRIu32
"\n", revision
- 1);
87 first_commit_done
= 1;
91 void fast_export_end_commit(uint32_t revision
)
93 printf("progress Imported commit %"PRIu32
".\n\n", revision
);
96 static void ls_from_rev(uint32_t rev
, uint32_t depth
, const uint32_t *path
)
98 /* ls :5 path/to/old/file */
99 printf("ls :%"PRIu32
" \"", rev
);
100 pool_print_seq_q(depth
, path
, '/', stdout
);
105 static void ls_from_active_commit(uint32_t depth
, const uint32_t *path
)
107 /* ls "path/to/file" */
109 pool_print_seq_q(depth
, path
, '/', stdout
);
114 static const char *get_response_line(void)
116 const char *line
= buffer_read_line(&report_buffer
);
119 if (buffer_ferror(&report_buffer
))
120 die_errno("error reading from fast-import");
121 die("unexpected end of fast-import feedback");
124 void fast_export_data(uint32_t mode
, uint32_t len
, struct line_buffer
*input
)
126 if (mode
== REPO_MODE_LNK
) {
127 /* svn symlink blobs start with "link " */
128 buffer_skip_bytes(input
, 5);
131 printf("data %"PRIu32
"\n", len
);
132 buffer_copy_bytes(input
, len
);
136 static int parse_ls_response(const char *response
, uint32_t *mode
,
137 struct strbuf
*dataref
)
140 const char *response_end
;
143 response_end
= response
+ strlen(response
);
145 if (*response
== 'm') { /* Missing. */
151 if (response_end
- response
< strlen("100644") ||
152 response
[strlen("100644")] != ' ')
153 die("invalid ls response: missing mode: %s", response
);
155 for (; *response
!= ' '; response
++) {
157 if (ch
< '0' || ch
> '7')
158 die("invalid ls response: mode is not octal: %s", response
);
163 /* ' blob ' or ' tree ' */
164 if (response_end
- response
< strlen(" blob ") ||
165 (response
[1] != 'b' && response
[1] != 't'))
166 die("unexpected ls response: not a tree or blob: %s", response
);
167 response
+= strlen(" blob ");
170 tab
= memchr(response
, '\t', response_end
- response
);
172 die("invalid ls response: missing tab: %s", response
);
173 strbuf_add(dataref
, response
, tab
- response
);
177 int fast_export_ls_rev(uint32_t rev
, uint32_t depth
, const uint32_t *path
,
178 uint32_t *mode
, struct strbuf
*dataref
)
180 ls_from_rev(rev
, depth
, path
);
181 return parse_ls_response(get_response_line(), mode
, dataref
);
184 int fast_export_ls(uint32_t depth
, const uint32_t *path
,
185 uint32_t *mode
, struct strbuf
*dataref
)
187 ls_from_active_commit(depth
, path
);
188 return parse_ls_response(get_response_line(), mode
, dataref
);