| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * svn-fast-export.c | 
					
						
							|  |  |  |  * ---------- | 
					
						
							|  |  |  |  *  Walk through each revision of a local Subversion repository and export it | 
					
						
							|  |  |  |  *  in a stream that git-fast-import can consume. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Author: Chris Lee <clee@kde.org> | 
					
						
							|  |  |  |  * License: MIT <http://www.opensource.org/licenses/mit-license.php>
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  | #define _XOPEN_SOURCE
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef PATH_MAX
 | 
					
						
							|  |  |  | #define PATH_MAX 4096
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <apr_lib.h>
 | 
					
						
							|  |  |  | #include <apr_getopt.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  | #include <apr_general.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  | #include <svn_fs.h>
 | 
					
						
							| 
									
										
										
										
											2007-03-14 15:35:33 +00:00
										 |  |  | #include <svn_repos.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | #include <svn_pools.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  | #include <svn_types.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #undef SVN_ERR
 | 
					
						
							|  |  |  | #define SVN_ERR(expr) SVN_INT_ERR(expr)
 | 
					
						
							|  |  |  | #define apr_sane_push(arr, contents) *(char **)apr_array_push(arr) = contents
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TRUNK "/trunk/"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  | time_t get_epoch(char *svn_date) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 08:49:29 -08:00
										 |  |  |     struct tm tm = {0}; | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  |     char *date = malloc(strlen(svn_date) * sizeof(char *)); | 
					
						
							|  |  |  |     strncpy(date, svn_date, strlen(svn_date) - 8); | 
					
						
							|  |  |  |     strptime(date, "%Y-%m-%dT%H:%M:%S", &tm); | 
					
						
							| 
									
										
										
										
											2007-01-14 08:49:29 -08:00
										 |  |  |     free(date); | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  |     return mktime(&tm); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | int dump_blob(svn_fs_root_t *root, char *full_path, apr_pool_t *pool) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |     apr_size_t     len; | 
					
						
							|  |  |  |     svn_stream_t   *stream, *outstream; | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     svn_filesize_t stream_length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SVN_ERR(svn_fs_file_length(&stream_length, root, full_path, pool)); | 
					
						
							|  |  |  |     SVN_ERR(svn_fs_file_contents(&stream, root, full_path, pool)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 07:05:30 -08:00
										 |  |  |     fprintf(stdout, "data %lu\n", stream_length); | 
					
						
							|  |  |  |     fflush(stdout); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 07:05:30 -08:00
										 |  |  |     SVN_ERR(svn_stream_for_stdout(&outstream, pool)); | 
					
						
							|  |  |  |     SVN_ERR(svn_stream_copy(stream, outstream, pool)); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fprintf(stdout, "\n"); | 
					
						
							| 
									
										
										
										
											2007-01-14 07:48:24 -08:00
										 |  |  |     fflush(stdout); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  | int export_revision(svn_revnum_t rev, svn_fs_t *fs, apr_pool_t *pool) | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |     unsigned int         mark; | 
					
						
							|  |  |  |     const void           *key; | 
					
						
							|  |  |  |     void                 *val; | 
					
						
							|  |  |  |     char                 *path, *file_change; | 
					
						
							|  |  |  |     apr_pool_t           *revpool; | 
					
						
							|  |  |  |     apr_hash_t           *changes, *props; | 
					
						
							|  |  |  |     apr_hash_index_t     *i; | 
					
						
							|  |  |  |     apr_array_header_t   *file_changes; | 
					
						
							|  |  |  |     svn_string_t         *author, *committer, *svndate, *svnlog; | 
					
						
							|  |  |  |     svn_boolean_t        is_dir; | 
					
						
							|  |  |  |     svn_fs_root_t        *fs_root; | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     svn_fs_path_change_t *change; | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 07:05:30 -08:00
										 |  |  |     fprintf(stderr, "Exporting revision %ld... ", rev); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |     SVN_ERR(svn_fs_revision_root(&fs_root, fs, rev, pool)); | 
					
						
							|  |  |  |     SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool)); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     SVN_ERR(svn_fs_revision_proplist(&props, fs, rev, pool)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     revpool = svn_pool_create(pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     file_changes = apr_array_make(pool, apr_hash_count(changes), sizeof(char *)); | 
					
						
							|  |  |  |     mark = 1; | 
					
						
							|  |  |  |     for (i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) { | 
					
						
							|  |  |  |         svn_pool_clear(revpool); | 
					
						
							|  |  |  |         apr_hash_this(i, &key, NULL, &val); | 
					
						
							|  |  |  |         path = (char *)key; | 
					
						
							|  |  |  |         change = (svn_fs_path_change_t *)val; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |         SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, path, revpool)); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (is_dir || strncmp(TRUNK, path, strlen(TRUNK))) { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (change->change_kind == svn_fs_path_change_delete) { | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |             apr_sane_push(file_changes, (char *)svn_string_createf(pool, "D %s", path + strlen(TRUNK))->data); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |             apr_sane_push(file_changes, (char *)svn_string_createf(pool, "M 644 :%u %s", mark, path + strlen(TRUNK))->data); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |             fprintf(stdout, "blob\nmark :%u\n", mark++); | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |             dump_blob(fs_root, (char *)path, revpool); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (file_changes->nelts == 0) { | 
					
						
							|  |  |  |         fprintf(stderr, "skipping.\n"); | 
					
						
							|  |  |  |         svn_pool_destroy(revpool); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  |     author = apr_hash_get(props, "svn:author", APR_HASH_KEY_STRING); | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |     if (svn_string_isempty(author)) | 
					
						
							|  |  |  |         author = svn_string_create("nobody", pool); | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  |     svndate = apr_hash_get(props, "svn:date", APR_HASH_KEY_STRING); | 
					
						
							|  |  |  |     svnlog = apr_hash_get(props, "svn:log", APR_HASH_KEY_STRING); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     fprintf(stdout, "commit refs/heads/master\n"); | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  |     fprintf(stdout, "committer %s <%s@localhost> %ld -0000\n", author->data, author->data, get_epoch((char *)svndate->data)); | 
					
						
							|  |  |  |     fprintf(stdout, "data %d\n", svnlog->len); | 
					
						
							| 
									
										
										
										
											2007-01-14 07:48:24 -08:00
										 |  |  |     fputs(svnlog->data, stdout); | 
					
						
							| 
									
										
										
										
											2007-01-14 06:11:31 -08:00
										 |  |  |     fprintf(stdout, "\n"); | 
					
						
							| 
									
										
										
										
											2007-01-14 17:07:42 -08:00
										 |  |  |     fputs(apr_array_pstrcat(pool, file_changes, '\n'), stdout); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     fprintf(stdout, "\n\n"); | 
					
						
							| 
									
										
										
										
											2007-01-14 07:48:24 -08:00
										 |  |  |     fflush(stdout); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     svn_pool_destroy(revpool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fprintf(stderr, "done!\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int crawl_revisions(char *repos_path) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |     apr_pool_t   *pool, *subpool; | 
					
						
							|  |  |  |     svn_fs_t     *fs; | 
					
						
							| 
									
										
										
										
											2007-03-14 15:35:33 +00:00
										 |  |  |     svn_repos_t  *repos; | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     svn_revnum_t youngest_rev, min_rev, max_rev, rev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pool = svn_pool_create(NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SVN_ERR(svn_fs_initialize(pool)); | 
					
						
							| 
									
										
										
										
											2007-03-14 15:35:33 +00:00
										 |  |  |     SVN_ERR(svn_repos_open(&repos, repos_path, pool)); | 
					
						
							|  |  |  |     if ((fs = svn_repos_fs(repos)) == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     SVN_ERR(svn_fs_youngest_rev(&youngest_rev, fs, pool)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     min_rev = 1; | 
					
						
							| 
									
										
										
										
											2007-01-14 08:51:30 -08:00
										 |  |  |     max_rev = youngest_rev; | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     subpool = svn_pool_create(pool); | 
					
						
							|  |  |  |     for (rev = min_rev; rev <= max_rev; rev++) { | 
					
						
							|  |  |  |         svn_pool_clear(subpool); | 
					
						
							| 
									
										
										
										
											2007-01-16 19:21:01 -08:00
										 |  |  |         export_revision(rev, fs, subpool); | 
					
						
							| 
									
										
										
										
											2007-01-14 05:16:29 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     svn_pool_destroy(pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (argc != 2) { | 
					
						
							|  |  |  |         fprintf(stderr, "usage: %s REPOS_PATH\n", argv[0]); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (apr_initialize() != APR_SUCCESS) { | 
					
						
							|  |  |  |         fprintf(stderr, "You lose at apr_initialize().\n"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     crawl_revisions(argv[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     apr_terminate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |