public void RefreshRepositories(RootDirectoryItem rootItem, bool eventTriggered)
        {
            if (rootItem == null)
            {
                return;
            }

            rootItem.LastRefresh = DateTime.Now;
            rootItem.Refreshing = true;

            SetRootRefreshInProgress(rootItem);

            if (!Directory.Exists(rootItem.Path))
            {
                RemoveRootDirectoryItem(rootItem);
                return;
            }

            new Thread(() =>
            {
                if (eventTriggered)
                {
                    Thread.Sleep(TimeSpan.FromMinutes(1));
                }

                DispatchInvoker.InvokeBackground(() =>
                {
                    rootItem.RepositoryBranches.Clear();
                });

                rootItem.RepositoryItems.ToList().ForEach(repo =>
                {
                    if (!Directory.Exists(repo.Path))
                    {
                        DispatchInvoker.InvokeBackground(() =>
                        {
                            rootItem.RepositoryItems.Remove(repo);
                            //item.RepositoryItems = new GitManager().FindRepositories(item.Path);
                        });
                        return;
                    }

                    var changesList = new ObservableCollection<string>();
                    new GitManager().Changes(repo).ForEach(changesList.Add);
                    var branchList = GetBranchList(repo);
                    var currentBranch = GetCurrentBranchName(repo);
                    var aheadCount = new GitManager().Aheadby(repo);
                    var behindCount = new GitManager().BehindBy(repo);

                    var commitLog = new ObservableCollection<string>();
                    new GitManager().CommitLog(repo).ForEach(commitLog.Add);

                    DispatchInvoker.InvokeBackground(() =>
                    {
                        branchList.ToList().ForEach(branch =>
                        {
                            if (!rootItem.RepositoryBranches.Contains(branch) && branch != "DETACHED HEAD")
                            {
                                rootItem.RepositoryBranches.Add(branch);
                            }
                        });

                        repo.Parent = rootItem;
                        repo.Modified = changesList.Count > 0;
                        repo.Changes = changesList;
                        repo.Branches = branchList;
                        repo.CurrentBranch = currentBranch;
                        repo.CommitsAheadBy = aheadCount;
                        repo.CommitsBehindBy = behindCount;
                        repo.CommitLog = commitLog;
                        repo.InProgress = false;
                        rootItem.Refreshing = false;

                    });
                });

                DispatchInvoker.InvokeBackground(() =>
                {
                    if (rootItem.RepositoryItems != null)
                    {
                        var sortedList = rootItem.RepositoryItems.OrderBy(x => x.CurrentBranch).ThenBy(x => x.Name).ToList();
                        rootItem.RepositoryItems.Clear();
                        sortedList.ForEach(x =>
                        {
                            rootItem.RepositoryItems.Add(x);
                        });
                    }
                });

            }).Start();
        }
        private RootDirectoryItem DiscoverRepositoriesAndAddToList(string path, bool collapsed = false)
        {
            if (!Directory.Exists(path) || GitListDataContext.RootDirectoryItems.Any(x => x.Path == path))
            {
                return null;
            }

            var repositories = new GitManager().FindRepositories(path);
            var newRootItem = new RootDirectoryItem()
            {
                Path = path,
                RepositoryItems = repositories,
                LastRefresh = DateTime.Now,
                RepositoriesDetected = repositories.Count > 0,
                Collapsed = collapsed,
                RepositoryBranches = new ObservableCollection<string>()
            };

            var watcher = new FileSystemWatcher(newRootItem.Path);
            watcher.Changed += (o, args) => OnChanged(o, args, newRootItem);
            watcher.Created += (o, args) => OnChanged(o, args, newRootItem);
            watcher.Deleted += (o, args) => OnChanged(o, args, newRootItem);
            watcher.Renamed += (o, args) => OnChanged(o, args, newRootItem);
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

            AddRootDirectoryItem(newRootItem);

            //if (gitListDataContext.RootDirectoryItems.Count > 0 && gitListDataContext.RootDirectoryItems[0].RepositoryItems.Count > 0 && gitListDataContext.RootDirectoryItems[0].Collapsed == false)
            //{
            //    gitListDataContext.SelectedRepositoryItem = gitListDataContext.RootDirectoryItems[0].RepositoryItems[0];
            //    //gitListDataContext.Controllers.ConsoleController.ChangeRepository(gitListDataContext.RootDirectoryItems[0].RepositoryItems[0]);
            //}

            return newRootItem;
        }
        public string GetCurrentBranchName(RepositoryItem item)
        {
            var branchName = new GitManager().CurrentBranchName(item);

            if (branchName == "(no branch)")
            {
                branchName = "DETACHED HEAD";
            }

            return branchName;
        }