public Watcher(string path, ILogger logger, int interval) { _logger = logger; _interval = interval; _repo = new Repository(path); _branches = _repo.Branches .Where(b => !b.IsRemote) .ToBranchDictionary(); }
void raiseEventsForDifferences(BranchDictionary previousBranches, BranchDictionary currentBranches) { currentBranches.Where(current => previousBranches.HasBranch(current.Name) && current.Commit != previousBranches[current.Name].Commit) .ForEach(branchInfo => raiseBranchChanged(previousBranches, branchInfo)); currentBranches.Where(current => !previousBranches.HasBranch(current.Name)) .ForEach(raiseBranchAdded); previousBranches.Where(current => !currentBranches.HasBranch(current.Name)) .ForEach(raiseBranchDeleted); }
void check(object state) { var previousBranches = _branches; var currentBranches = _repo.Branches .Where(b => !b.IsRemote) .ToBranchDictionary(); _branches = currentBranches; raiseEventsForDifferences(previousBranches, currentBranches); _timer.Change(_interval, Timeout.Infinite); }
void 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}, 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; BranchChanged?.Invoke(eventInfo); } catch (Exception ex) { _logger.Error($"Error while checking for changes on branch {branch}. Previous commit is {previousBranches[branch.Name].Commit}, Current commit is {branch.Commit}. {ex.Message}"); throw; } }