示例#1
0
        public void Repository_with_single_commit_and_single_file()
        {
            using (var repo = new Repository(m_Repository.Directory.Location))
            {
                var commit = repo.Commits.Single();

                var gitDirectory = new GitDirectory(null, m_Repository.Directory.Name, commit);

                Assert.Equal(m_Repository.Directory.Name, gitDirectory.Name);
                Assert.Empty(gitDirectory.Directories);
                Assert.Single(gitDirectory.Files);
                Assert.True(gitDirectory.FileExists(RepositoryInfoFile.RepositoryInfoFileName));
            }
        }
示例#2
0
        /// <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;

        } 
示例#3
0
        public ISyncPoint this[int id]
        {
            get
            {
                if (!GitGroup.Repository.LocalBranchExists(BranchName))
                {
                    throw new SyncPointNotFoundException(id);
                }

                var root = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);
                var relativePath = GetRelativeSynchronizationStateFilePath(id);

                if (!root.FileExists(relativePath))
                {
                    throw new SyncPointNotFoundException(id);
                }

                return SyncPointStateFile.Load(null, (IReadableFile)root.GetFile(relativePath)).Content;
            }
        }
示例#4
0
        public IEnumerable<SyncAction> this[SyncActionState state]
        {
            get
            {
                // branch does not exist => result is empty
                if (!GitGroup.Repository.LocalBranchExists(BranchName))
                {
                    return Enumerable.Empty<SyncAction>();
                }

                // load the root directory of the branch
                var gitDirectory = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);

                // determine if the directory for action with the specified state exists and load actions from it            
                var directoryPath = GetRelativeSyncActionDirectoryPath(state);
                if (!gitDirectory.DirectoryExists(directoryPath))
                {
                    return Enumerable.Empty<SyncAction>();
                }

                var directory = gitDirectory.GetDirectory(directoryPath);
                return LoadSyncActions(directory);
            }
        }
示例#5
0
        public ConflictInfo this[string filePath]
        {
            get
            {
                PathValidator.EnsureIsValidFilePath(filePath);
                PathValidator.EnsureIsRootedPath(filePath);

                if (!GitGroup.Repository.LocalBranchExists(BranchName))
                {
                    throw new ItemNotFoundException($"There is no ConflictInfo for file '{filePath}'");
                }

                var root = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);

                var relativePath = GetRelativeConflictInfoFilePath(filePath);
                if (!root.FileExists(relativePath))
                {
                    throw new ItemNotFoundException($"There is no ConflictInfo for file '{filePath}'");
                }

                var file = (IReadableFile) root.GetFile(relativePath);
                return ConflictInfoFile.Load(null, file).Content;
            }
        }
示例#6
0
        public void Repository_with_subdirectories()
        {
            // arrange
            string commitId;
            using (var workingDirectory = new TemporaryWorkingDirectory(m_Repository.Directory.Location, "master"))
            {                
                var directory = new Directory(Path.GetFileName(workingDirectory.Location))
                {
                    root => new Directory(root, s_Dir1)
                    {
                        dir1 => new EmptyFile(dir1, s_File1),
                        dir1 => new EmptyFile(dir1, s_File2)
                    },
                    root => new Directory(root, s_Dir2)
                    {
                        dir2 => new EmptyFile(dir2, s_File1)
                    }
                };

                System.IO.File.Delete(Path.Combine(workingDirectory.Location, RepositoryInfoFile.RepositoryInfoFileName));

                m_DirectoryCreator.CreateDirectory(directory, Path.GetDirectoryName(workingDirectory.Location));
                commitId = workingDirectory.Commit();
                workingDirectory.Push();
            }

            using (var repo = new Repository(m_Repository.Directory.Location))
            {
                // act
                var commit = repo.Lookup<Commit>(commitId);
                var gitDirectory = new GitDirectory(null, m_Repository.Directory.Name, commit);


                // assert
                Assert.Equal(m_Repository.Directory.Name, gitDirectory.Name);

                Assert.Equal(2, gitDirectory.Directories.Count());
                Assert.Empty(gitDirectory.Files);

                Assert.True(gitDirectory.DirectoryExists(s_Dir1));
                Assert.True(gitDirectory.GetDirectory(s_Dir1).FileExists(s_File1));
                Assert.True(gitDirectory.GetDirectory(s_Dir1).FileExists(s_File2));
                Assert.True(gitDirectory.DirectoryExists(s_Dir2));
                Assert.True(gitDirectory.GetDirectory(s_Dir2).FileExists(s_File1));
                
            }

        }
示例#7
0
        public bool ItemExists(int id)
        {
            if (!GitGroup.Repository.LocalBranchExists(BranchName))
            {
                return false;
            }

            var root = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);
            var relativePath = GetRelativeSynchronizationStateFilePath(id);

            return root.FileExists(relativePath);
        }
示例#8
0
        public void AddItems(IEnumerable<ConflictInfo> conflicts)
        {
            if (conflicts == null)
            {
                throw new ArgumentNullException(nameof(conflicts));
            }
            conflicts = conflicts.ToArray();

            if(!conflicts.Any())
            {
                return;
            }

            EnsureBranchExists();

            var exisitngRoot = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);
            var createdRoot = new Directory(null, "root");

            // verify conflicts
            foreach (var conflict in conflicts)
            {                
                var relativePath = GetRelativeConflictInfoFilePath(conflict.FilePath);
                if (exisitngRoot.FileExists(relativePath))
                {
                    throw new DuplicateItemException($"A ConflictInfo for '{conflict.FilePath}' already exists");
                }

                var directory = DirectoryHelper.GetOrAddDirectory(createdRoot, PathParser.GetDirectoryName(relativePath));
                directory.Add(f => new ConflictInfoFile(f, conflict));
            }


            using (var workingDirectory = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString()))
            {                
                var localItemCreator = new LocalItemCreator();
                localItemCreator.CreateDirectoryInPlace(createdRoot, workingDirectory.Location);

                workingDirectory.Commit($"{nameof(GitConflictService)}: Added {conflicts.Count()} items");
                workingDirectory.Push();
            }            

        }
示例#9
0
        public bool ItemExists(string filePath)
        {
            PathValidator.EnsureIsValidFilePath(filePath);
            PathValidator.EnsureIsRootedPath(filePath);
            
            if (!GitGroup.Repository.LocalBranchExists(BranchName))
            {
                return false;
            }

            var root = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);
            return root.FileExists(GetRelativeConflictInfoFilePath(filePath));
        }
示例#10
0
        public void RemoveItems(IEnumerable<ConflictInfo> conflicts)
        {
            if (conflicts == null)
            {
                throw new ArgumentNullException(nameof(conflicts));
            }

            conflicts = conflicts.ToArray();

            if (!conflicts.Any())
            {
                return;
            }

            if (!GitGroup.Repository.LocalBranchExists(BranchName))
            {
                throw new ItemNotFoundException($"There is no ConflictInfo for file '{conflicts.First().FilePath}'");
            }


            var root = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip);

            // verify conflicts
            foreach (var conflict in conflicts)
            {             
                var relativePath = GetRelativeConflictInfoFilePath(conflict.FilePath);
                if (!root.FileExists(relativePath))
                {
                    throw new ItemNotFoundException($"There is no ConflictInfo for file '{conflict.FilePath}'");
                }
            }


            // delete conflict info files
            using (var workingDirectory = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString()))
            {
                var localDirectory= new LocalDirectory(null, workingDirectory.Location);

                foreach (var conflict in conflicts)
                {
                    var relativePath = GetRelativeConflictInfoFilePath(conflict.FilePath);
                    System.IO.File.Delete(((ILocalFile)localDirectory.GetFile(relativePath)).Location);
                }

                workingDirectory.Commit($"{nameof(GitConflictService)}: Removed {conflicts.Count()} items");
                workingDirectory.Push();
            }                
        }