public void Enumerate(GitConfigurationEnumerationCallback cb)
        {
            string level = GetLevelFilterArg();

            using (Process git = _git.CreateProcess($"config --null {level} --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={_filterLevel})");
                    throw CreateGitException(git, "Failed to enumerate all Git configuration entries");
                }

                IEnumerable <string> entries = data.Split('\0').Where(x => !string.IsNullOrWhiteSpace(x));
                foreach (string entry in entries)
                {
                    string[] kvp = entry.Split(new[] { '\n' }, count: 2);

                    if (kvp.Length == 2 && !cb(kvp[0], kvp[1]))
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 2
0
        public void Enumerate(GitConfigurationEnumerationCallback cb)
        {
            string level = GetLevelFilterArg();

            using (Process git = _git.CreateProcess($"config --null {level} --list"))
            {
                git.Start();
                git.WaitForExit();

                switch (git.ExitCode)
                {
                case 0:     // OK
                    break;

                default:
                    throw new Exception(
                              $"Failed to enumerate all Git configuration entries. Exit code '{git.ExitCode}' (level={_filterLevel})");
                }

                // TODO: don't read in all the data at once; stream it
                string data = git.StandardOutput.ReadToEnd();

                IEnumerable <string> entries = data.Split('\0').Where(x => !string.IsNullOrWhiteSpace(x));
                foreach (string entry in entries)
                {
                    string[] kvp = entry.Split(new[] { '\n' }, count: 2);

                    if (!cb(kvp[0], kvp[1]))
                    {
                        break;
                    }
                }
            }
        }
        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;
                    }
                }
            }
        }