using System; using System.Collections.Generic; using System.Diagnostics; namespace Semmle.Extraction.CSharp.DependencyFetching { /// /// Utilities for querying information from the git CLI. /// internal class Git { private readonly ProgressMonitor progressMonitor; private const string git = "git"; public Git(ProgressMonitor progressMonitor) { this.progressMonitor = progressMonitor; } /// /// Lists all files matching which are tracked in the /// current git repository. /// /// The file pattern. /// A list of all tracked files which match . /// public List ListFiles(string pattern) { var results = new List(); var args = string.Join(' ', "ls-files", $"\"{pattern}\""); progressMonitor.RunningProcess($"{git} {args}"); var pi = new ProcessStartInfo(git, args) { UseShellExecute = false, RedirectStandardOutput = true }; using var p = new Process() { StartInfo = pi }; p.OutputDataReceived += new DataReceivedEventHandler((sender, e) => { if (!string.IsNullOrWhiteSpace(e.Data)) { results.Add(e.Data); } }); p.Start(); p.BeginOutputReadLine(); p.WaitForExit(); if (p.ExitCode != 0) { progressMonitor.CommandFailed(git, args, p.ExitCode); throw new Exception($"{git} {args} failed"); } return results; } } }