コード例 #1
0
 public Watcher(string path, int interval, Func <BranchAdded, Task> branchAdded, Func <BranchChanged, Task> branchChanged, Func <BranchRemoved, Task> branchRemoved)
 {
     _logger        = LogManager.GetLogger("watcher");
     _interval      = interval;
     _branchAdded   = branchAdded;
     _branchChanged = branchChanged;
     _branchRemoved = branchRemoved;
     _repo          = new Repository(path);
     _branches      = _repo.Branches
                      .Where(b => !b.IsRemote)
                      .ToBranchDictionary();
 }
コード例 #2
0
        async Task check()
        {
            var previousBranches = _branches;
            var currentBranches  = _repo.Branches
                                   .Where(b => !b.IsRemote)
                                   .ToBranchDictionary();

            _branches = currentBranches;

            await raiseEventsForDifferences(previousBranches, currentBranches);

            _timer.Change(_interval, Timeout.Infinite);
        }
コード例 #3
0
        async Task raiseEventsForDifferences(BranchDictionary previousBranches, BranchDictionary currentBranches)
        {
            foreach (var branchInfo in currentBranches.Where(current => previousBranches.HasBranch(current.Name) && current.Commit != previousBranches[current.Name].Commit))
            {
                await raiseBranchChanged(previousBranches, branchInfo);
            }


            foreach (var branchInfo in currentBranches.Where(current => !previousBranches.HasBranch(current.Name)))
            {
                await raiseBranchAdded(branchInfo);
            }


            foreach (var branchInfo in previousBranches.Where(current => !currentBranches.HasBranch(current.Name)))
            {
                await raiseBranchDeleted(branchInfo);
            }
        }
コード例 #4
0
        async Task raiseBranchChanged(BranchDictionary previousBranches, BranchInfo branch)
        {
            try
            {
                var previousCommit = _repo.Lookup <Commit>(previousBranches[branch.Name].Commit);
                var currentCommit  = _repo.Lookup <Commit>(branch.Commit);
                _logger.Trace($"Found differences on branch {branch.Name}, starting diff between {previousCommit.Sha} and {currentCommit.Sha}");
                var result = _repo.Diff.Compare <TreeChanges>(previousCommit.Tree, currentCommit.Tree);
                _logger.Info($"Finished diff on branch {branch.Name}, found {result.Added.Count()} added items, {result.Deleted.Count()} deleted items, {result.Renamed.Count()} renamed items and {result.Modified.Count()} modified items");

                var eventInfo = getBranchChanged <BranchChanged>(result, branch);
                eventInfo.PreviousCommit = previousCommit.Sha;

                await _branchChanged(eventInfo);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Error while checking for changes on branch {branch}. Previous commit is {previousBranches[branch.Name].Commit}, Current commit is {branch.Commit}");
                throw;
            }
        }