public bool TryGetCopiedCommit(Commit copiedFrom, Repository targetRepo, out Commit?commit) { var hash = CommitMessageUtil.ReadBasedirCommitNameFromMessage(copiedFrom.Message); if (hash != null) { // TODO: make option: remote name commit = targetRepo.Lookup <Commit>(hash); if (commit != null) { return(true); } CommandGit.ShallowFetch(targetRepo, "origin", hash.Sha); commit = targetRepo.Lookup <Commit>(hash); if (commit != null) { return(true); } return(false); } else { commit = RepositoryUtil.GetAllCommits(targetRepo) .FirstOrDefault(copied => CommitMessageUtil.ReadSubdirCommitNameFromMessage(copied.Message, _copyOptions.DirInSrcs) == copiedFrom.Id); if (commit != null) { return(true); } return(false); } }
public static Repository PrepareRepository( string repositoryDescriptor, string descriptorIsFor, bool allowEmpty, bool allowRemote, int maxDepth, string mustLocalTailingMessage = "", string mustExistsTailingMessage = "") { string repositoryPath; #if SUPPORT_REMOTE_REPOSITORY if (UrlSchemas.Any(repositoryDescriptor.StartsWith)) { if (!allowRemote) { throw new InvalidUsageException($"{descriptorIsFor} must be local repository" + mustLocalTailingMessage + "."); } var tempDir = Path.GetTempFileName(); File.Delete(tempDir); Directory.CreateDirectory(tempDir); var options = new CloneOptions { RecurseSubmodules = false }; repositoryPath = Repository.Init(repositoryDescriptor); Directory.CreateDirectory(repositoryPath); repositoryPath = CommandGit.ShallowClone(repositoryDescriptor, tempDir, maxDepth); using var repo = new Repository(repositoryPath); foreach (var branch in repo.Branches) { if (branch.IsRemote && branch.RemoteName == "origin") { var localName = branch.FriendlyName.RemovePrefix("origin/"); repo.Config.Set($"branch.{localName}.remote", "origin"); repo.Config.Set($"branch.{localName}.merge", $"refs/heads/{localName}"); repo.Branches.Add(localName, branch.Tip); } } } else #endif { repositoryPath = Path.Join(repositoryDescriptor, ".git"); if (!Directory.Exists(repositoryPath)) { if (!allowEmpty) { throw new InvalidUsageException($"{descriptorIsFor} is not exists." + mustExistsTailingMessage); } repositoryPath = Repository.Init(repositoryDescriptor); } } return(new Repository(repositoryPath)); }