private IGitTfsRemote InitTfsBranch(RemoteOptions remoteOptions, string tfsRepositoryPath, int rootChangesetId = -1, bool fetchParentBranch = false, string gitBranchNameExpected = null, IRenameResult renameResult = null)
{
Trace.WriteLine("Begin process of creating branch for remote :" + tfsRepositoryPath);
// TFS string representations of repository paths do not end in trailing slashes
tfsRepositoryPath = (tfsRepositoryPath ?? string.Empty).TrimEnd('/');
string gitBranchName = ExtractGitBranchNameFromTfsRepositoryPath(
string.IsNullOrWhiteSpace(gitBranchNameExpected) ? tfsRepositoryPath : gitBranchNameExpected);
if (string.IsNullOrWhiteSpace(gitBranchName))
throw new GitTfsException("error: The Git branch name '" + gitBranchName + "' is not valid...\n");
Trace.WriteLine("Git local branch will be :" + gitBranchName);
string sha1RootCommit = null;
if (rootChangesetId != -1)
{
sha1RootCommit = Repository.FindCommitHashByChangesetId(rootChangesetId);
if (fetchParentBranch && string.IsNullOrWhiteSpace(sha1RootCommit))
sha1RootCommit = FindRootRemoteAndFetch(rootChangesetId, renameResult);
if (string.IsNullOrWhiteSpace(sha1RootCommit))
return null;
Trace.WriteLine("Found commit " + sha1RootCommit + " for changeset :" + rootChangesetId);
}
IGitTfsRemote tfsRemote;
if (Repository.HasRemote(gitBranchName))
{
Trace.WriteLine("Remote already exist");
tfsRemote = Repository.ReadTfsRemote(gitBranchName);
if (tfsRemote.TfsUrl != TfsUrl)
Trace.WriteLine("warning: Url is different");
if (tfsRemote.TfsRepositoryPath != tfsRepositoryPath)
Trace.WriteLine("warning: TFS repository path is different");
}
else
{
Trace.WriteLine("Try creating remote...");
tfsRemote = Repository.CreateTfsRemote(new RemoteInfo
{
Id = gitBranchName,
Url = TfsUrl,
Repository = tfsRepositoryPath,
RemoteOptions = remoteOptions
}, string.Empty);
}
if (sha1RootCommit != null && !Repository.HasRef(tfsRemote.RemoteRef))
{
if (!Repository.CreateBranch(tfsRemote.RemoteRef, sha1RootCommit))
throw new GitTfsException("error: Fail to create remote branch ref file!");
}
Trace.WriteLine("Remote created!");
return tfsRemote;
}