GitBuilder: Explicitly set the default branch on GitInit().

This prevents GitBuilder-based tests from failing locally if the global value of Git option init.defaultBranch[1] differs from that of the git_common.DefaultBranch constant.

[1] https://git-scm.com/docs/git-config#Documentation/git-config.txt-initdefaultBranch

Change-Id: I8694eafa2b0d06f973609320697a8815f1e47ade
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/325897
Auto-Submit: Leandro Lovisolo <lovisolo@google.com>
Commit-Queue: Eric Boren <borenet@google.com>
Reviewed-by: Eric Boren <borenet@google.com>
diff --git a/go/git/testutils/git_builder.go b/go/git/testutils/git_builder.go
index 3f56d8b..bf827a3 100644
--- a/go/git/testutils/git_builder.go
+++ b/go/git/testutils/git_builder.go
@@ -52,6 +52,19 @@
 	}
 
 	g.Git(ctx, "init")
+	// Set the initial branch.
+	//
+	// It is important to set the initial branch explicitly because developer workstations might have
+	// a value for Git option init.defaultBranch[1] that differs from that of the CQ bots. This can
+	// cause tests that use GitBuilder to fail locally but pass on the CQ (or vice versa).
+	//
+	// [1] https://git-scm.com/docs/git-config#Documentation/git-config.txt-initdefaultBranch
+	//
+	// TODO(lovisolo): Replace with "git init --initial-branch <git_common.DefaultBranch>" once all
+	//                 GCE instances have been upgraded to Git >= v2.28, which introduces flag
+	//                 --initial-branch.
+	//                 See https://github.com/git/git/commit/32ba12dab2acf1ad11836a627956d1473f6b851a.
+	g.Git(ctx, "symbolic-ref", "HEAD", "refs/heads/"+g.branch)
 	g.Git(ctx, "remote", "add", git_common.DefaultRemote, ".")
 	g.Git(ctx, "config", "--local", "user.name", "test")
 	g.Git(ctx, "config", "--local", "user.email", "test@google.com")
diff --git a/go/git/testutils/git_builder_test.go b/go/git/testutils/git_builder_test.go
index 7a3f96a..5f5aa1d 100644
--- a/go/git/testutils/git_builder_test.go
+++ b/go/git/testutils/git_builder_test.go
@@ -8,6 +8,7 @@
 
 	"github.com/stretchr/testify/require"
 	"go.skia.org/infra/go/exec"
+	"go.skia.org/infra/go/git/git_common"
 	"go.skia.org/infra/go/testutils/unittest"
 )
 
@@ -16,9 +17,15 @@
 	ctx := context.Background()
 	g := GitInit(t, ctx)
 	defer g.Cleanup()
+
+	// Assert that the default branch has the expected name.
+	output, err := exec.RunCwd(ctx, g.Dir(), g.git, "symbolic-ref", "--short", "HEAD")
+	require.NoError(t, err)
+	require.Equal(t, git_common.DefaultBranch, strings.TrimSpace(output))
+
 	commits := GitSetup(ctx, g)
 
-	output, err := exec.RunCwd(ctx, g.Dir(), g.git, "log", "-n", "6", "--format=format:%H:%P", "HEAD")
+	output, err = exec.RunCwd(ctx, g.Dir(), g.git, "log", "-n", "6", "--format=format:%H:%P", "HEAD")
 	require.NoError(t, err)
 	t.Log(ctx, output)
 	lines := strings.Split(output, "\n")