1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
/*! # Mercurial to Git converter using git fast-import with multi repository import support ```bash hg-git-fast-import single hg_repo git_repo ``` ## Features 1. Import of single and multiple Mercurial repositories to Git repository. 1. Import of new revisions from previously imported Mercurial repositories to Git repository. 1. Tags. 1. Closed branches. 1. Verification of the end result with diff. ## Installation With `cargo`: ```bash cargo install hg-git-fast-import ``` From source: ```bash git clone https://github.com/kilork/hg-git-fast-import.git cd hg-git-fast-import cargo install --path . ``` ## Usage **hg-git-fast-import** is a command-line utility, usage info can be access with --help argument: ```bash $ hg-git-fast-import --help hg-git-fast-import 1.1.0 Alexander Korolev <kilork@yandex.ru> A utility to import single and multiple Mercurial repositories to Git. USAGE: hg-git-fast-import <SUBCOMMAND> FLAGS: -h, --help Prints help information -V, --version Prints version information SUBCOMMANDS: help Prints this message or the help of the given subcommand(s) multi Exports multiple Mercurial repositories to single Git repo in fast-import compatible format single Exports single Mercurial repository to Git fast-import compatible format ``` Import of single repository: ```bash $ hg-git-fast-import single --help hg-git-fast-import-single 1.1.0 Alexander Korolev <kilork@yandex.ru> Exports single Mercurial repository to Git fast-import compatible format USAGE: hg-git-fast-import single [FLAGS] [OPTIONS] <hg_repo> [git_repo] FLAGS: --clean Recreate Git repo before import if it exists. --cron Produce minimal output only if new revisions loaded or error happened. --fix-wrong-branch-names Fix wrong Mercurial branch names (not compatible with git ref format). -h, --help Prints help information --no-clean-closed-branches Do not clean closed Mercurial branches. --source-pull Pull source Mercurial repository before import. --target-pull Pull target Git repository before push. --target-push Push target Git repository after successful import. -V, --version Prints version information --verify Compares resulting Git repo with Mercurial. OPTIONS: -a, --authors <authors> Authors remapping in toml format. -c, --config <config> Repository configuration in toml format. --git-active-branches <git-active-branches> Git maximum number of branches to maintain active at once. --limit-high <limit-high> Limit high revision to import. --log <log> Log file. If present - additional log info would be printed to this file. ARGS: <hg_repo> The Mercurial repo for import to git <git_repo> The Git repo to import to. Creates repo if it does not exist. Otherwise saved state must exist. ``` Import of multiple repositories: ```bash $ hg-git-fast-import multi --help hg-git-fast-import-multi 1.1.0 Alexander Korolev <kilork@yandex.ru> Exports multiple Mercurial repositories to single Git repo in fast-import compatible format USAGE: hg-git-fast-import multi [FLAGS] [OPTIONS] --config <config> FLAGS: --clean Recreate Git repo before import if it exists. --cron Produce minimal output only if new revisions loaded or error happened. --fix-wrong-branch-names Fix wrong Mercurial branch names (not compatible with git ref format). -h, --help Prints help information --no-clean-closed-branches Do not clean closed Mercurial branches. --source-pull Pull source Mercurial repository before import. --target-pull Pull target Git repository before push. --target-push Push target Git repository after successful import. -V, --version Prints version information --verify Compares resulting Git repo with Mercurial. OPTIONS: -a, --authors <authors> Authors remapping in toml format. -c, --config <config> Repositories configuration in toml format. --git-active-branches <git-active-branches> Git maximum number of branches to maintain active at once. --log <log> Log file. If present - additional log info would be printed to this file. ``` ## Configuration syntax For more advanced cases one may supply configuration in `toml` format. ### Single mode configuration example ```toml # Allows to start import in of hanged heads in repository # (currently has no effect, default value is true). Optional. allow_unnamed_heads = true # Offset for git fast-import marks in Git repository. Optional, default is 0. offset = 1000 # Path prefix in target repository. If path_prefix = 'test', # all files will be under test folder in target Git repository. # Optional. path_prefix = 'prefix1' # Tag prefix in target repository. Optional. tag_prefix = 'prefix2-' # Branch prefix in target repository. Optional. branch_prefix = 'prefix3-' # By default master branch is not prefixed by branch_prefix. # This behavior can be changed by specifying this as true. # Optional. prefix_default_branch = false # Mapping between authors in Mercurial and authors in Git. # Required mainly because of Git asks for particular format "Somename <email@address>". # But also can be used to fix typos and etc. [authors] 'aaa 1' = 'Bbb <bbb@company.xyz>' aaa = 'Bbb <bbb@company.xyz>' ccc = 'Qqq <qqq@another.dom>' 'My <my_typo@wrong.xyz>' = 'My <my@normal.xyz>' # Mapping between branches in Mercurial and branches in Git. # Required mainly because Git does not allow some characters, # which allowed in Mercurial, for example - spaces. # Branches taken from mapping will not have branch_prefix, # so it must be added to mapped values. [branches] 'branch in hg' = 'branch-in-git' 'anotherhg' = 'othergit' ``` With `authors` and `branches` subsections one can rename authors and branches during import. Offset creates marks in Git repository. Can be useful if all marks files from imported repositories planned to be analyzed together. `allow_unnamed_heads` allows to start import in case of hanged heads in repository, currently this feature has no effect. ### Multi mode configuration example ```toml # Path to target git repository. path_git = "000_git" # This is subsection with list of repositories to be aggregated into single repo. # Each subsection start like this (see toml format for arrays). [[repositories]] # Mercurial repository path. path_hg = "001_hg" # Child Git repository path. path_git = "001_git" # Child repository configuration for 001_hg/001_git. # Fields are the same as on root level in single mode configuration. [repositories.config] allow_unnamed_heads = true offset = 1000 path_prefix = 'prefix1' tag_prefix = 'prefix2-' branch_prefix = 'prefix3-' prefix_default_branch = true # Same as authors section in single mode, but for this child repository. [repositories.config.authors] 'aaa' = 'Bbb <bbb@company.xyz>' # Same as branches section in single mode, but for this child repository. [repositories.config.branches] 'branch1' = 'branch2' # This sections specify to which branches would be merged migrated # branches from this child Git repository. [repositories.merged_branches] branch_in_git = 'branch2' # Explanation: in this case branch_in_git will be a branch in 000_git repo # and it will contain branch2 merged from remote child repository. # This is second child repository. [[repositories]] # Here we can also specify alias, this field used to add reference in target 000_git repository. # Otherwise path_prefix is used from config section. alias = "another_002" path_hg = "002_hg" path_git = "002_git" [repositories.merged_branches] branch_in_git = 'branch_in_hg' # Actually this branch_in_hg is from second migrated Git repository. # Interesting to note - both child repository branches are merged # into single branch_in_git branch in target 000_git repository. ``` Each of child repositories will be imported in corresponding `path_git` from configuration, then single repository from top level `path_git` will reference child repositories as `remote`. For remote name either `alias` either `path_prefix` is taken. ### Authors list configuration example ```toml 'aaa 1' = 'Bbb <bbb@company.xyz>' aaa = 'Bbb <bbb@company.xyz>' ccc = 'Qqq <qqq@another.dom>' 'My <my_typo@wrong.xyz>' = 'My <my@normal.xyz>' ``` ## Requirements - Rust 1.32 or later (2018 edition) - Git 2.19 (optional, if you use `single` mode without repo creation) - Diff 2.8 (optional, if you do not use `--verify`) - Mercurial 4.8 (optional, if you do not need delta load of revisions) - Python 2.7 (optional, required for `Mercurial`) ## Docker support To setup all dependencies can be a tricky task - it is possible to use [```docker```](https://www.docker.com/) for running ```hg-git-fast-import```. ### Docker installation ```bash git clone https://github.com/kilork/hg-git-fast-import.git cd hg-git-fast-import/docker ./build.sh ``` ### Docker running ```bash docker run -it --rm kilork/hg-git-fast-import hg-git-fast-import --help ``` To mount current directory with repositories and run ```hg-git-fast-import``` command with docker one can use wrapper ```hg-git-fast-import/docker/run.sh```: ```bash cd hg-git-fast-import/docker ./run.sh ``` By default this will mount current directory to ```/repositories``` dir inside docker container. This can be overriden by usage of env variable: ```bash HG_GIT_FAST_IMPORT_VOLUME=~/sandbox:/sandbox ./run.sh single /sandbox/source_hg /sandbox/target_git ``` */ use lazy_static::lazy_static; use std::collections::HashSet; use std::ops::Range; use std::process::Command; use log::{info, trace}; use regex::Regex; use ordered_parallel_iterator::OrderedParallelIterator; use std::collections::HashMap; use std::fs::File; use std::io::{ self, prelude::{Read, Write}, }; use std::path::Path; use std::path::PathBuf; use std::process::ExitStatus; pub mod config; pub mod env; pub mod error; pub mod git; pub mod multi; pub mod single; use self::config::RepositorySavedState; pub use error::ErrorKind; use hg_parser::{ file_content, Changeset, FileType, ManifestEntryDetails, MercurialRepository, Revision, SharedMercurialRepository, }; pub fn read_file(filename: &PathBuf) -> io::Result<String> { let mut file = File::open(filename)?; let mut buf = String::new(); file.read_to_string(&mut buf)?; Ok(buf) } #[derive(Debug)] pub enum TargetRepositoryError { Nope, IsNotDir, SavedStateDoesNotExist, CannotInitRepo(ExitStatus), CannotConfigRepo(ExitStatus), ImportFailed(ExitStatus), IOError(std::io::Error), VerifyFail, } impl From<std::io::Error> for TargetRepositoryError { fn from(value: std::io::Error) -> Self { TargetRepositoryError::IOError(value) } } pub trait TargetRepository { fn start_import( &mut self, git_active_branches: Option<usize>, ) -> Result<(&mut Write, Option<config::RepositorySavedState>), TargetRepositoryError>; fn finish(&mut self) -> Result<(), TargetRepositoryError>; fn verify( &self, _verified_repo: &str, _subfolder: Option<&str>, ) -> Result<(), TargetRepositoryError> { Ok(()) } fn save_state(&self, _state: RepositorySavedState) -> Result<(), TargetRepositoryError> { Ok(()) } fn get_saved_state(&self) -> Option<&RepositorySavedState> { None } fn remote_list(&self) -> Result<HashSet<String>, TargetRepositoryError> { unimplemented!(); } fn remote_add(&self, _name: &str, _url: &str) -> Result<(), TargetRepositoryError> { unimplemented!(); } fn checkout(&self, _branch: &str) -> Result<(), TargetRepositoryError> { unimplemented!(); } fn fetch_all(&self) -> Result<(), TargetRepositoryError> { unimplemented!(); } fn merge_unrelated(&self, _branches: &[&str]) -> Result<(), TargetRepositoryError> { unimplemented!(); } } #[derive(Debug)] pub enum SourceRepositoryError { PullFail(String), } struct MercurialRepo<'a> { path: PathBuf, inner: SharedMercurialRepository, config: &'a config::RepositoryConfig, env: &'a env::Environment, } impl<'a> MercurialRepo<'a> { /// Open Mercurial repository. pub fn open<P: AsRef<Path>>( path: P, config: &'a config::RepositoryConfig, env: &'a env::Environment, ) -> Result<MercurialRepo<'a>, ErrorKind> { Ok(Self { path: path.as_ref().to_path_buf(), inner: SharedMercurialRepository::new(MercurialRepository::open(path)?), config, env, }) } /// Open Mercurial repository with pull by `hg pull -u` command before import. /// Pull command triggered only if `env.source_pull` is `true`. pub fn open_with_pull<P: AsRef<Path>>( path: P, config: &'a config::RepositoryConfig, env: &'a env::Environment, ) -> Result<MercurialRepo<'a>, ErrorKind> { if env.source_pull { let mut hg = Command::new("hg"); hg.args(&["pull", "-u"]); if env.cron { hg.arg("-q"); } let status = hg.current_dir(path.as_ref()).status()?; if !status.success() { return Err(SourceRepositoryError::PullFail(format!( "Cannot pull {}", path.as_ref().to_str().unwrap() )) .into()); } } Self::open(path, config, env) } fn path(&self) -> &Path { self.path.as_path() } fn verify_heads(&self, _allow_unnamed_heads: bool) -> Result<bool, ErrorKind> { Ok(true) } fn changelog_len(&self) -> Result<usize, ErrorKind> { Ok(self.inner.last_rev().0 as usize) } fn fixup_user(&self, user: &str) -> String { if let Some(ref authors) = self.config.authors { if let Some(remap) = authors.get(user) { return remap.clone(); } } if let Some(ref authors) = self.env.authors { if let Some(remap) = authors.get(user) { return remap.clone(); } } lazy_static! { static ref RE: Regex = Regex::new("([^<]+) ?(<[^>]*>)$").unwrap(); } let (name, email) = if let Some(caps) = RE.captures(&user) { ( caps.get(1).unwrap().as_str().trim_end(), caps.get(2).unwrap().as_str(), ) } else { panic!("Wrong user: {}", user); }; format!("{} {}", name, email) } fn mark<R: Into<usize>>(&self, revision: R) -> usize { revision.into() + 1 + self.config.offset.unwrap_or(0) } fn range(&self, range: Range<usize>) -> OrderedParallelIterator<Changeset> { self.inner.par_range_iter(range.into()) } fn export_commit( &self, changeset: &mut Changeset, count: usize, brmap: &mut HashMap<String, String>, output: &mut Write, ) -> Result<usize, ErrorKind> { let header = &changeset.header; let user = self.fixup_user(std::str::from_utf8(&header.user)?); let mut branch = None; let mut closed = false; for (key, value) in &header.extra { if key == b"branch" { branch = Some(value.as_slice()); } if key == b"close" && value == b"1" { closed = true; } } let branch: String = std::str::from_utf8(branch.unwrap_or_else(|| b"master"))?.into(); let branch = brmap.entry(branch.clone()).or_insert_with(|| { sanitize_branchname( &branch, if branch != "master" || self.config.prefix_default_branch { self.config.branch_prefix.as_ref() } else { None }, self.env.fix_wrong_branchname, ) }); let revision = changeset.revision; if header.p1.is_some() || header.p2.is_some() || revision != 0.into() { writeln!(output, "reset refs/heads/{}", branch)?; } let desc = String::from_utf8_lossy(&header.comment); let time = header.time.timestamp_secs(); let timezone = header.time.tz_offset_secs(); let tz = format!("{:+03}{:02}", -timezone / 3600, ((-timezone % 3600) / 60)); writeln!(output, "commit refs/heads/{}", branch)?; let mark = self.mark(revision); writeln!(output, "mark :{}", mark)?; writeln!(output, "author {} {} {}", user, time, tz)?; writeln!(output, "committer {} {} {}", user, time, tz)?; writeln!(output, "data {}", desc.len() + 1)?; writeln!(output, "{}\n", desc)?; match (header.p1, header.p2) { (Some(p1), Some(p2)) => { writeln!(output, "from :{}", self.mark(p1))?; writeln!(output, "merge :{}", self.mark(p2))?; } (Some(p), None) | (None, Some(p)) => { writeln!(output, "from :{}", self.mark(p))?; } _ => (), } info!( "{} ({}) | {} | {} | {} | {}", mark, revision.0, branch, user, desc, header.time ); if self.env.cron { eprintln!( "{} ({}) | {} | {} | {} | {}", mark, revision.0, branch, user, desc, header.time ); } let prefix = strip_leading_slash(self.config.path_prefix.as_ref(), &"".into()); for file in &mut changeset.files { match (&mut file.data, &mut file.manifest_entry) { (None, None) => { write!(output, "D {}", prefix)?; output.write_all(&file.path)?; writeln!(output)?; } (Some(data), Some(manifest_entry)) => { write!( output, "M {} inline {}", match manifest_entry.details { ManifestEntryDetails::File(FileType::Symlink) => "120000", ManifestEntryDetails::File(FileType::Executable) => "100755", ManifestEntryDetails::Tree | ManifestEntryDetails::File(FileType::Regular) => "100644", }, prefix )?; output.write_all(&file.path)?; let data = file_content(&data); writeln!(output, "\ndata {}", data.len())?; output.write_all(&data[..])?; } _ => panic!("Wrong file data!"), } } if closed { writeln!(output, "reset refs/tags/archive/{}", branch)?; writeln!(output, "from :{}\n", self.mark(revision))?; writeln!(output, "reset refs/heads/{}", branch)?; writeln!(output, "from 0000000000000000000000000000000000000000\n")?; } Ok(count + 1) } fn export_tags( &self, range: Range<usize>, mut count: usize, output: &mut Write, ) -> Result<usize, ErrorKind> { info!("Exporting tags"); for (revision, tag) in self .inner .tags()? .range(Revision::from(range.start as u32)..Revision::from(range.end as u32)) { let tag = sanitize_name(&tag.name, self.config.tag_prefix.as_ref(), "tag"); writeln!(output, "reset refs/tags/{}", tag).unwrap(); writeln!(output, "from :{}", self.mark(*revision)).unwrap(); writeln!(output).unwrap(); count += 1; } Ok(count) } } fn strip_leading_slash(prefix: Option<&String>, x: &String) -> String { prefix.map_or_else(|| x.to_string(), |p| format!("{}/{}", p, x)) } fn sanitize_branchname(name: &str, prefix: Option<&String>, fix_branch_name: bool) -> String { let branchname = sanitize_name(name, prefix, "branch"); if !fix_branch_name { return branchname; } let mut result = String::new(); let mut chars = branchname.chars().peekable(); let mut last = None; while let Some(&c) = chars.peek() { if c != '/' { break; } result.push(c); last = chars.next(); } while let Some(&c) = chars.peek() { let c = match c { '\0'...' ' | '~' | '^' | ':' | '\\' => '-', '.' if last == Some('.') || last == None => '-', c => c, }; result.push(c); last = chars.next(); } if result.ends_with("/") { result.remove(result.len() - 1); result.push('-'); } if result.ends_with(".lock") { result.replace_range((result.len() - 5)..=(result.len() - 5), "-"); } result } fn sanitize_name(name: &str, prefix: Option<&String>, what: &str) -> String { trace!("Sanitize {} '{}'", what, name); prefix.map_or_else(|| name.into(), |p| format!("{}{}", p, name)) //TODO: git-check-ref-format } #[cfg(test)] mod tests { use super::*; #[test] fn sanitize_branchnames() { assert_eq!(&sanitize_branchname("normal", None, false), "normal"); assert_eq!(&sanitize_branchname("normal", None, true), "normal"); assert_eq!(&sanitize_branchname("////normal", None, true), "////normal"); assert_eq!( &sanitize_branchname("with spaces ", None, true), "with-spaces--" ); assert_eq!( &sanitize_branchname("with spaces ", Some(&"prefix-".into()), true), "prefix-with-spaces--" ); assert_eq!( &sanitize_branchname(".dotatstart", None, true), "-dotatstart" ); assert_eq!( &sanitize_branchname("dots.in.the.middle", None, true), "dots.in.the.middle" ); assert_eq!( &sanitize_branchname("doubledots..", None, true), "doubledots.-" ); assert_eq!(&sanitize_branchname("...", None, true), "---"); assert_eq!( &sanitize_branchname("branch.lock", None, true), "branch-lock" ); assert_eq!(&sanitize_branchname("//qqq//", None, true), "//qqq/-"); } }