public void Enumerate(GitConfigurationLevel level, GitConfigurationEnumerationCallback cb) { string levelArg = GetLevelFilterArg(level); using (Process git = _git.CreateProcess($"config --null {levelArg} --list")) { git.Start(); // To avoid deadlocks, always read the output stream first and then wait // TODO: don't read in all the data at once; stream it string data = git.StandardOutput.ReadToEnd(); git.WaitForExit(); switch (git.ExitCode) { case 0: // OK break; default: _trace.WriteLine($"Failed to enumerate config entries (exit={git.ExitCode}, level={level})"); throw GitProcess.CreateGitException(git, "Failed to enumerate all Git configuration entries"); } var name = new StringBuilder(); var value = new StringBuilder(); int i = 0; while (i < data.Length) { name.Clear(); value.Clear(); // Read key name (LF terminated) while (i < data.Length && data[i] != '\n') { name.Append(data[i++]); } if (i >= data.Length) { _trace.WriteLine("Invalid Git configuration output. Expected newline terminator (\\n) after key."); break; } // Skip the LF terminator i++; // Read value (null terminated) while (i < data.Length && data[i] != '\0') { value.Append(data[i++]); } if (i >= data.Length) { _trace.WriteLine("Invalid Git configuration output. Expected null terminator (\\0) after value."); break; } // Skip the null terminator i++; var entry = new GitConfigurationEntry(name.ToString(), value.ToString()); if (!cb(entry)) { break; } } } }
public CommandContext(string appPath) { EnsureArgument.NotNullOrWhiteSpace(appPath, nameof(appPath)); ApplicationPath = appPath; Streams = new StandardStreams(); Trace = new Trace(); if (PlatformUtils.IsWindows()) { FileSystem = new WindowsFileSystem(); SessionManager = new WindowsSessionManager(); SystemPrompts = new WindowsSystemPrompts(); Environment = new WindowsEnvironment(FileSystem); Terminal = new WindowsTerminal(Trace); string gitPath = GetGitPath(Environment, FileSystem); Git = new GitProcess( Trace, gitPath, FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = new WindowsCredentialManager(Settings.CredentialNamespace); } else if (PlatformUtils.IsMacOS()) { FileSystem = new MacOSFileSystem(); SessionManager = new MacOSSessionManager(); SystemPrompts = new MacOSSystemPrompts(); Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); string gitPath = GetGitPath(Environment, FileSystem); Git = new GitProcess( Trace, gitPath, FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = new MacOSKeychain(Settings.CredentialNamespace); } else if (PlatformUtils.IsLinux()) { FileSystem = new LinuxFileSystem(); // TODO: support more than just 'Posix' or X11 SessionManager = new PosixSessionManager(); SystemPrompts = new LinuxSystemPrompts(); Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); string gitPath = GetGitPath(Environment, FileSystem); Git = new GitProcess( Trace, gitPath, FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); IGpg gpg = new Gpg( Environment.LocateExecutable("gpg"), SessionManager ); CredentialStore = new LinuxCredentialStore(FileSystem, Settings, SessionManager, gpg, Environment, Git); } else { throw new PlatformNotSupportedException(); } HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams); // Set the parent window handle/ID SystemPrompts.ParentWindowId = Settings.ParentWindowId; }
public CommandContext() { Streams = new StandardStreams(); Trace = new Trace(); if (PlatformUtils.IsWindows()) { FileSystem = new WindowsFileSystem(); SessionManager = new WindowsSessionManager(); SystemPrompts = new WindowsSystemPrompts(); Environment = new WindowsEnvironment(FileSystem); Terminal = new WindowsTerminal(Trace); Git = new GitProcess( Trace, Environment.LocateExecutable("git.exe"), FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = WindowsCredentialManager.Open(Settings.CredentialNamespace); } else if (PlatformUtils.IsMacOS()) { FileSystem = new MacOSFileSystem(); SessionManager = new MacOSSessionManager(); SystemPrompts = new MacOSSystemPrompts(); Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); Git = new GitProcess( Trace, Environment.LocateExecutable("git"), FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = MacOSKeychain.Open(Settings.CredentialNamespace); } else if (PlatformUtils.IsLinux()) { FileSystem = new LinuxFileSystem(); // TODO: support more than just 'Posix' or X11 SessionManager = new PosixSessionManager(); SystemPrompts = new LinuxSystemPrompts(); Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); Git = new GitProcess( Trace, Environment.LocateExecutable("git"), FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = new LinuxCredentialStore(Settings, Git, Settings.CredentialNamespace); } else { throw new PlatformNotSupportedException(); } HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams); // Set the parent window handle/ID SystemPrompts.ParentWindowId = Settings.ParentWindowId; }