/// <summary> /// Removes all local branches that are not tracking any remote branch /// </summary> private void RemoveRedundantBranches() { using (var repository = new Repository(this.LocalPath)) { foreach (var localBranch in repository.GetLocalBranches().Where(b => !b.IsTracking)) { repository.Branches.Remove(localBranch); } } }
/// <summary> /// Verifies that the specified path points to a valid repository usable by SyncTool /// (a repository with the expected tags and default branches as created by RepositoryInitHelper) /// </summary> public static bool IsValid(string path) { // check if the directory exists and is a valid git repository if (!Directory.Exists(path) || !Repository.IsValid(path)) { return false; } using (var repository = new Repository(path)) { // the repository needs to be a bare repository if (!repository.Info.IsBare) { return false; } // ensure there is a configuration branch if (!repository.LocalBranchExists(RepositoryInitHelper.ConfigurationBranchName)) { return false; } // ensure there is a tag for the initial commit if (repository.Tags[RepositoryInitHelper.InitialCommitTagName] == null) { return false; } // check if there is a repository info file in the root (on all branches) foreach (var localBranch in repository.GetLocalBranches()) { var gitDirectory = new GitDirectory(null, "Irrelevant", localBranch.Tip); if (!gitDirectory.FileExists(RepositoryInfoFile.RepositoryInfoFileName)) { return false; } //TODO: verify content of repository info file } } return true; }
internal bool CanReuseLocalRepository() { // if the directory does not exist, there is nothing to reuse if (!Directory.Exists(LocalPath)) { return false; } // if the directory is empty, there is nothing to reuse if (!Directory.EnumerateFileSystemEntries(LocalPath).Any()) { return false; } // check if the directory is a git repository if (!Repository.IsValid(LocalPath)) { return false; } using (var repository = new Repository(LocalPath)) { // check if the repository is a bare repository if (!repository.Info.IsBare) { return false; } // make sure the repository has a remote named "origin" that points to RemotePath if (repository.Network.Remotes[s_Origin]?.Url != RemotePath) { return false; } var localBranches = repository.GetLocalBranches().ToList(); // make sure there are no branches that are not tracking a remote if (localBranches.Any(b => b.IsTracking == false)) { return false; } //fetch all branches repository.Network.Fetch(repository.Network.Remotes[s_Origin]); // make sure there are no unpushed changes if (localBranches.Any(b => b.TrackingDetails.AheadBy > 0)) { return false; } } return true; }
public void Begin_creates_local_branches_for_all_remote_branches() { var transaction = CreateTransaction(); Directory.CreateDirectory(transaction.LocalPath); transaction.Begin(); using (var localRepository = new Repository(transaction.LocalPath)) { Assert.Equal(m_RemoteRepository.Branches.Count(), localRepository.GetLocalBranches().Count()); Assert.Equal(m_RemoteRepository.Branches.Select(x => x.FriendlyName), localRepository.GetLocalBranches().Select(b => b.FriendlyName)); // make sure all local branches are set up to track the remote branches foreach (var branch in localRepository.GetLocalBranches()) { Assert.True(branch.IsTracking); Assert.Equal("origin/" + branch.FriendlyName, branch.TrackedBranch.FriendlyName); } } }