Exemplo n.º 1
0
 public GitStatus AddEntry(string path, GitStatus status)
 {
     if (string.IsNullOrEmpty(path) || path == ".")
     {
         this._status = status;
         return status;
     }
     if (_entries == null)
         _entries = new Dictionary<string, GitStatusSet>();
     string entry;
     string subpath;
     var pos = path.IndexOf('/');
     if (pos < 0)
     {
         entry = path;
         subpath = null;
     }
     else
     {
         entry = path.Substring(0, pos);
         subpath = path.Substring(pos + 1);
     }
     GitStatusSet subset;
     if (!_entries.TryGetValue(entry, out subset))
         _entries[entry] = subset = new GitStatusSet();
     status = subset.AddEntry(subpath, status);
     if (status == GitStatus.Added || status == GitStatus.Deleted || status == GitStatus.Modified)
     {
         this._status = GitStatus.Modified;
     }
     return this._status;
 }
        private static void GitGetFiles(string wcRoot, GitStatusSet statusSet)
        {
            var git = Git.FindGit();
            if (git == null)
                return;

            var runner = new ProcessRunner();
            runner.WorkingDirectory = wcRoot;
            runner.LogStandardOutputAndError = false;
            runner.OutputLineReceived += delegate (object sender, LineReceivedEventArgs e)
            {
                if (!string.IsNullOrEmpty(e.Line))
                {
                    statusSet.AddEntry(e.Line, GitStatus.OK);
                }
            };

            var command = "ls-files";
            var hasErrors = false;
            runner.ErrorLineReceived += delegate (object sender, LineReceivedEventArgs e)
            {
                if (!hasErrors)
                {
                    hasErrors = true;
                    GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command);
                }
                GitMessageView.AppendLine(e.Line);
            };
            runner.Start(git, command);
            runner.WaitForExit();
        }
        public static GitStatusSet GetStatusSet(string wcRoot)
        {
            lock (StatusSetMap)
            {
                GitStatusSet statusSet;
                foreach (var pair in StatusSetMap)
                {
                    if (FileUtility.IsEqualFileName(pair.Key, wcRoot))
                        return pair.Value;
                }

                statusSet = new GitStatusSet();
                GitGetFiles(wcRoot, statusSet);
                GitGetStatus(wcRoot, statusSet);
                StatusSetMap.Add(new KeyValuePair<string, GitStatusSet>(wcRoot, statusSet));
                return statusSet;
            }
        }
        private static void GitGetStatus(string wcRoot, GitStatusSet statusSet)
        {
            var git = Git.FindGit();
            if (git == null)
                return;

            var command = "status --porcelain --untracked-files=no";
            var hasErrors = false;

            var runner = new ProcessRunner();
            runner.WorkingDirectory = wcRoot;
            runner.LogStandardOutputAndError = false;
            runner.OutputLineReceived += delegate (object sender, LineReceivedEventArgs e)
            {
                if (!string.IsNullOrEmpty(e.Line))
                {
                    var m = StatusParseRegex.Match(e.Line);
                    if (m.Success)
                    {
                        statusSet.AddEntry(m.Groups[2].Value, StatusFromText(m.Groups[1].Value));
                    }
                    else
                    {
                        if (!hasErrors)
                        {
                            // in front of first output line, print the command line we invoked
                            hasErrors = true;
                            GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command);
                        }
                        GitMessageView.AppendLine("unknown output: " + e.Line);
                    }
                }
            };
            runner.ErrorLineReceived += delegate (object sender, LineReceivedEventArgs e)
            {
                if (!hasErrors)
                {
                    hasErrors = true;
                    GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command);
                }
                GitMessageView.AppendLine(e.Line);
            };
            runner.Start(git, command);
            runner.WaitForExit();
        }