Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Fallout.Build/VCS/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ private static (string Name, string Branch) GetRemoteNameAndBranch(AbsolutePath
.SkipWhile(x => x != $"[branch {branch.DoubleQuote()}]")
.Skip(1)
.TakeWhile(x => !x.StartsWith("["))
.Select(x => x.Split('='))
.ToDictionary(x => x.ElementAt(0).Trim(), x => x.ElementAt(1).Trim());
.Where(x => x.Contains('='))
.Select(x => x.Split('=', 2))
.GroupBy(x => x[0].Trim(), x => x[1].Trim())
.ToDictionary(x => x.Key, x => x.Last());
return data.TryGetValue("remote", out var remote) && data.TryGetValue("merge", out var merge)
? (remote, merge.TrimStart("refs/heads/"))
: (null, null);
Expand Down
62 changes: 62 additions & 0 deletions tests/Fallout.Build.Tests/GitRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,66 @@ public void FromDirectoryTest()
repository.Commit.Should().NotBeNullOrEmpty();
repository.Tags.Should().NotBeNull();
}

[Fact]
public void FromDirectoryTest_WithDuplicateVscodeMergeBaseKey_DoesNotThrow()
{
// VS Code's Git extension is known to append a duplicate `vscode-merge-base` line to the
// `[branch "<name>"]` section instead of updating it in place, which used to crash
// GetRemoteNameAndBranch's ToDictionary call. See regression report for details.
var directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(directory);

try
{
RunGit(directory, "init --quiet --initial-branch=main");
RunGit(directory, "config user.email test@example.com");
RunGit(directory, "config user.name Test");
File.WriteAllText(Path.Combine(directory, "file.txt"), "content");
RunGit(directory, "add file.txt");
RunGit(directory, "commit --quiet -m initial");
RunGit(directory, "remote add origin https://github.com/nuke-build/nuke.git");
RunGit(directory, "config branch.main.remote origin");
RunGit(directory, "config branch.main.merge refs/heads/main");

var configPath = Path.Combine(directory, ".git", "config");
File.AppendAllText(
configPath,
"\tvscode-merge-base = origin/main" + Environment.NewLine +
"\tvscode-merge-base = origin/main" + Environment.NewLine);

var repository = GitRepository.FromLocalDirectory(directory);

repository.Branch.Should().Be("main");
repository.Identifier.Should().Be("nuke-build/nuke");
}
finally
{
ForceDeleteDirectory(directory);
}
}

private static void ForceDeleteDirectory(string directory)
{
// Git marks object files read-only, which trips up Directory.Delete on Windows.
foreach (var file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories))
File.SetAttributes(file, FileAttributes.Normal);

Directory.Delete(directory, recursive: true);
}

private static void RunGit(string workingDirectory, string arguments)
{
var startInfo = new System.Diagnostics.ProcessStartInfo("git", arguments)
{
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};

using var process = System.Diagnostics.Process.Start(startInfo).NotNull();
process.WaitForExit();
process.ExitCode.Should().Be(0, process.StandardError.ReadToEnd());
}
}
Loading