Exemple #1
0
        public GitBackupStatus InPlaceGitBackup(string repositoryFolder)
        {
            var isValidGitRepository = Repository.IsValid(repositoryFolder);
            var createdRepository    = false;

            if (!isValidGitRepository)
            {
                _logger.InfoFormat("Initializing git repository in folder '{0}'...", repositoryFolder);
                repositoryFolder  = Repository.Init(repositoryFolder, false);
                createdRepository = true;
            }
            var numberOfFilesAdded   = 0;
            var numberOfFilesChanged = 0;
            var numberOfFilesRemoved = 0;

            using (var repository = new Repository(repositoryFolder))
            {
                var options = new StatusOptions();
                var status  = repository.RetrieveStatus(options);
                _logger.Debug("Repository Status: " + JsonConvert.SerializeObject(status));
                if (status.IsDirty)
                {
                    var untractedFiles = status.Untracked;
                    foreach (var untrackedFile in untractedFiles)
                    {
                        _logger.Info("Added: " + untrackedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(untrackedFile.FilePath, stageOptions);
                        numberOfFilesAdded++;
                    }

                    var modifiedFiles = status.Modified;
                    foreach (var modifiedFile in modifiedFiles)
                    {
                        _logger.Info("Modified: " + modifiedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(modifiedFile.FilePath, stageOptions);
                        numberOfFilesChanged++;
                    }

                    var removedFiles = status.Missing;
                    foreach (var removedFile in removedFiles)
                    {
                        _logger.Info("Removed: " + removedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(removedFile.FilePath, stageOptions);
                        numberOfFilesRemoved++;
                    }
                    var email         = GetNoReplyEmail();
                    var author        = new Signature(Environment.UserName, email, DateTimeOffset.Now);
                    var committer     = new Signature(Environment.UserName, email, DateTimeOffset.Now);
                    var commitOptions = new CommitOptions();
                    _logger.Info("Commiting...");
                    repository.Commit(DateTime.Now.ToString(CultureInfo.InvariantCulture), author, committer, commitOptions);
                }
            }
            var gitBackupStatus = new GitBackupStatus(createdRepository, numberOfFilesAdded, numberOfFilesChanged, numberOfFilesRemoved);

            return(gitBackupStatus);
        }
        public void InPlaceGitBackupExistingRepositoryWithTwoFilesModifyingOneFileTest()
        {
            using (var testBooStrapper = new TestBootStrapper(GetType()))
            {
                using (var testFolder = TestFolderHelper.CreateTestFolder())
                {
                    var testFile1 = testFolder.AddOneFile();
                    var testFile2 = testFolder.AddOneFile();
                    var target = testBooStrapper.Container.Resolve<IInPlaceGitBackupProvider>();
                    var status = target.InPlaceGitBackup(testFolder.Path);

                    testFolder.ModifyFile(testFile1);

                    var actual = target.InPlaceGitBackup(testFolder.Path);
                    var expected = new GitBackupStatus(false, 0, 1, 0);
                    Assert.AreEqual(expected.CreateRepository, actual.CreateRepository, "CreatedRepository");
                    Assert.AreEqual(expected.NumberOfFilesAdded, actual.NumberOfFilesAdded, "NumberOfFilesAdded");
                    Assert.AreEqual(expected.NumberOfFilesChanged, actual.NumberOfFilesChanged, "NumberOfFilesChanged");
                    Assert.AreEqual(expected.NumberOfFilesRemoved, actual.NumberOfFilesRemoved, "NumberOfFilesRemoved");
                }
            }
        }
 public void InPlaceGitBackupNewRepositoryWithOneFileTest()
 {
     using (var testBooStrapper = new TestBootStrapper(GetType()))
     {
         using (var testFolder = TestFolderHelper.CreateTestFolder())
         {
             testFolder.AddOneFile();
             var target = testBooStrapper.Container.Resolve<IInPlaceGitBackupProvider>();
             var actual = target.InPlaceGitBackup(testFolder.Path);
             var expected = new GitBackupStatus(true, 1, 0, 0);
             Assert.AreEqual(expected.CreateRepository, actual.CreateRepository, "CreatedRepository");
             Assert.AreEqual(expected.NumberOfFilesAdded, actual.NumberOfFilesAdded, "NumberOfFilesAdded");
             Assert.AreEqual(expected.NumberOfFilesChanged, actual.NumberOfFilesChanged, "NumberOfFilesChanged");
             Assert.AreEqual(expected.NumberOfFilesRemoved, actual.NumberOfFilesRemoved, "NumberOfFilesRemoved");
         }
     }
 }
        public GitBackupStatus InPlaceGitBackup(string repositoryFolder)
        {
            var isValidGitRepository = LibGit2Sharp.Repository.IsValid(repositoryFolder);
            var createdRepository = false;
            if (!isValidGitRepository)
            {
                _logger.InfoFormat("Initializing git repository in folder '{0}'...", repositoryFolder);
                repositoryFolder = LibGit2Sharp.Repository.Init(repositoryFolder, false);
                createdRepository = true;
            }
            var numberOfFilesAdded = 0;
            var numberOfFilesChanged = 0;
            var numberOfFilesRemoved = 0;
            using (var repository = new LibGit2Sharp.Repository(repositoryFolder))
            {
                var options = new StatusOptions();
                var status = repository.RetrieveStatus(options);
                _logger.Debug("Repository Status: " + JsonConvert.SerializeObject(status));
                if (status.IsDirty)
                {
                    var untractedFiles = status.Untracked;
                    foreach (var untrackedFile in untractedFiles)
                    {
                        _logger.Info("Added: " + untrackedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(untrackedFile.FilePath, stageOptions);
                        numberOfFilesAdded++;
                    }

                    var modifiedFiles = status.Modified;
                    foreach (var modifiedFile in modifiedFiles)
                    {
                        _logger.Info("Modified: " + modifiedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(modifiedFile.FilePath, stageOptions);
                        numberOfFilesChanged++;
                    }

                    var removedFiles = status.Missing;
                    foreach (var removedFile in removedFiles)
                    {
                        _logger.Info("Removed: " + removedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(removedFile.FilePath, stageOptions);
                        numberOfFilesRemoved++;
                    }
                    var author = new Signature(Environment.UserName, "*****@*****.**", System.DateTimeOffset.Now);
                    var committer = new Signature(Environment.UserName, "*****@*****.**", System.DateTimeOffset.Now);
                    var commitOptions = new CommitOptions();
                    _logger.Info("Commiting...");
                    repository.Commit(DateTime.Now.ToString(CultureInfo.InvariantCulture), author, committer, commitOptions);
                }
            }
            var gitBackupStatus = new GitBackupStatus(createdRepository, numberOfFilesAdded, numberOfFilesChanged, numberOfFilesRemoved);
            return gitBackupStatus;
        }