diff --git a/doc/commands/exportmap.md b/doc/commands/exportmap.md new file mode 100644 index 00000000..e476fd00 --- /dev/null +++ b/doc/commands/exportmap.md @@ -0,0 +1,17 @@ +## Summary + +The exportmap command creates a mapping file of Tfs ChangeSet Id and Commit Id from an already migrated repository. + +## Synopsis + + Usage: git-tfs exportmap [options] + where options are: + + -f=VALUE The path to the mapping file + -r=VALUE The path to the git repository + +## Examples + +### Create a mapping file from current working directory / git repository + + git tfs exportmap -f mapping.txt -r . \ No newline at end of file diff --git a/src/GitTfs/Commands/ExportMap.cs b/src/GitTfs/Commands/ExportMap.cs new file mode 100644 index 00000000..7e25836a --- /dev/null +++ b/src/GitTfs/Commands/ExportMap.cs @@ -0,0 +1,60 @@ +using System.ComponentModel; +using NDesk.Options; +using Newtonsoft.Json; +using StructureMap; + +namespace GitTfs.Commands +{ + [Pluggable("exportmap")] + [Description("exportmap -f -r ")] + public class ExportMap : GitTfsCommand + { + private readonly Globals _globals; + private readonly Init _init; + private readonly Help _helper; + + public ExportMap(Globals globals, Init init, Help helper) + { + _globals = globals; + _init = init; + _helper = helper; + } + + public string FilePath { get; set; } + public string RepositoryPath { get; set; } + + public OptionSet OptionSet + { + get + { + return new OptionSet + { + { "f|file=", "The output file path", + f => FilePath = f }, + { "r|repository=", "The path to the repository", + r => RepositoryPath = r } + }; + } + } + + public int Run() + { + if (string.IsNullOrWhiteSpace(RepositoryPath) + || string.IsNullOrWhiteSpace(FilePath)) + { + return _helper.Run(this); + } + + _globals.Repository = _init.GitHelper.MakeRepository(RepositoryPath); + var commits = _globals.Repository.GetCommitChangeSetPairs(); + var dic = JsonConvert.SerializeObject(commits); + using (System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath)) + { + file.WriteLine(dic); + file.Flush(); + } + + return 0; + } + } +} diff --git a/src/GitTfs/Core/GitRepository.cs b/src/GitTfs/Core/GitRepository.cs index a4b78308..b75ff77c 100644 --- a/src/GitTfs/Core/GitRepository.cs +++ b/src/GitTfs/Core/GitRepository.cs @@ -796,5 +796,31 @@ namespace GitTfs.Core return sha; } + + public IDictionary GetCommitChangeSetPairs() + { + var allCommits = _repository.Commits.QueryBy(new CommitFilter()); + var pairs = new Dictionary() ; + foreach (var c in allCommits) + { + int changesetId; + if (TryParseChangesetId(c.Message, out changesetId)) + { + pairs.Add(changesetId.ToString(), c.Sha); + } + else + { + foreach (var note in c.Notes) + { + if (TryParseChangesetId(note.Message, out changesetId)) + { + pairs.Add(changesetId.ToString(), c.Sha); + } + } + } + } + + return pairs; + } } } diff --git a/src/GitTfs/Core/IGitRepository.cs b/src/GitTfs/Core/IGitRepository.cs index 64d9ca92..cacee698 100644 --- a/src/GitTfs/Core/IGitRepository.cs +++ b/src/GitTfs/Core/IGitRepository.cs @@ -62,5 +62,7 @@ namespace GitTfs.Core IEnumerable FindParentCommits(string fromCommit, string toCommit); bool IsPathIgnored(string relativePath); string CommitGitIgnore(string pathToGitIgnoreFile); + + IDictionary GetCommitChangeSetPairs(); } } diff --git a/src/GitTfs/paket.references b/src/GitTfs/paket.references index 9630dcaa..83e64ba1 100644 --- a/src/GitTfs/paket.references +++ b/src/GitTfs/paket.references @@ -1,3 +1,4 @@ LibGit2Sharp nlog -structuremap \ No newline at end of file +structuremap +Newtonsoft.Json \ No newline at end of file