4 * Walk through each revision of a local Subversion repository and export it
5 * in a stream that git-fast-import can consume.
7 * Author: Chris Lee <clee@kde.org>
8 * License: MIT <http://www.opensource.org/licenses/mit-license.php>
19 #include <apr_general.h>
21 #include <apr_getopt.h>
23 #include <svn_types.h>
24 #include <svn_pools.h>
25 #include <svn_repos.h>
28 #define SVN_ERR(expr) SVN_INT_ERR(expr)
29 #define apr_sane_push(arr, contents) *(char **)apr_array_push(arr) = contents
31 #define TRUNK "/trunk/"
33 int dump_blob(svn_fs_root_t *root, char *full_path, apr_pool_t *pool)
35 svn_filesize_t stream_length;
40 SVN_ERR(svn_fs_file_length(&stream_length, root, full_path, pool));
41 SVN_ERR(svn_fs_file_contents(&stream, root, full_path, pool));
43 fprintf(stdout, "data %li\n", stream_length);
47 SVN_ERR(svn_stream_read(stream, buf, &len));
48 fprintf (stdout, "%s\0", buf);
51 fprintf(stdout, "\n");
56 int export_revision(svn_revnum_t rev, svn_repos_t *repo, svn_fs_t *fs, apr_pool_t *pool)
58 apr_array_header_t *file_changes;
59 apr_hash_t *changes, *props;
63 svn_fs_path_change_t *change;
64 svn_fs_root_t *root_obj;
67 char *path, *file_change;
72 fprintf(stderr, "Committing revision %li... ", rev);
74 SVN_ERR(svn_fs_revision_root(&root_obj, fs, rev, pool));
75 SVN_ERR(svn_fs_paths_changed(&changes, root_obj, pool));
76 SVN_ERR(svn_fs_revision_proplist(&props, fs, rev, pool));
78 revpool = svn_pool_create(pool);
80 file_changes = apr_array_make(pool, apr_hash_count(changes), sizeof(char *));
82 for (i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
83 svn_pool_clear(revpool);
84 apr_hash_this(i, &key, NULL, &val);
86 change = (svn_fs_path_change_t *)val;
88 SVN_ERR(svn_fs_is_dir(&is_dir, root_obj, path, revpool));
90 if (is_dir || strncmp(TRUNK, path, strlen(TRUNK))) {
94 if (change->change_kind == svn_fs_path_change_delete) {
95 *(char **)apr_array_push(file_changes) = ((char *)svn_string_createf(pool, "D %s", path + strlen(TRUNK))->data);
97 *(char **)apr_array_push(file_changes) = (char *)svn_string_createf(pool, "M 644 :%u %s", mark, path + strlen(TRUNK))->data;
98 fprintf(stdout, "blob\nmark :%u\n", mark++);
99 // dump_blob(root_obj, (char *)path, revpool);
103 if (file_changes->nelts == 0) {
104 fprintf(stderr, "skipping.\n");
105 svn_pool_destroy(revpool);
109 fprintf(stdout, "commit refs/heads/master\n");
110 fprintf(stdout, apr_array_pstrcat(pool, file_changes, '\n'));
111 fprintf(stdout, "\n\n");
113 svn_pool_destroy(revpool);
115 fprintf(stderr, "done!\n");
120 int crawl_revisions(char *repos_path)
122 apr_pool_t *pool, *subpool;
124 svn_revnum_t youngest_rev, min_rev, max_rev, rev;
127 pool = svn_pool_create(NULL);
129 SVN_ERR(svn_repos_open(&repos, repos_path, pool));
131 fs = svn_repos_fs(repos);
133 SVN_ERR(svn_fs_initialize(pool));
134 SVN_ERR(svn_fs_youngest_rev(&youngest_rev, fs, pool));
137 max_rev = youngest_rev;
139 subpool = svn_pool_create(pool);
140 for (rev = min_rev; rev <= max_rev; rev++) {
141 svn_pool_clear(subpool);
142 export_revision(rev, repos, fs, subpool);
145 svn_pool_destroy(pool);
150 int main(int argc, char *argv[])
153 fprintf(stderr, "usage: %s REPOS_PATH\n", argv[0]);
157 if (apr_initialize() != APR_SUCCESS) {
158 fprintf(stderr, "You lose at apr_initialize().\n");
162 crawl_revisions(argv[1]);