Skip to content

Commit

Permalink
sourcegraph: fix wrong git config
Browse files Browse the repository at this point in the history
Turns out git config doesn't support "_" in the keys.

https://git-scm.com/docs/git-config/2.22.0#_configuration_file

"The variable names are case-insensitive, allow only alphanumeric
characters and -, and must start with an alphabetic character."

Test plan:
New unit test
  • Loading branch information
stefanhengl committed Oct 1, 2024
1 parent d15aa28 commit ec0f506
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,9 @@ func (r *Repository) UnmarshalJSON(data []byte) error {
// Sourcegraph indexserver doesn't set repo.Rank, so we set it here. Setting it
// on read instead of during indexing allows us to avoid a complete reindex.
//
// Prefer "latest_commit_date" over "priority" for ranking. We keep priority for
// Prefer "latestCommitDate" over "priority" for ranking. We keep priority for
// backwards compatibility.
if _, ok := repo.RawConfig["latest_commit_date"]; ok {
if _, ok := repo.RawConfig["latestCommitDate"]; ok {
// We use the number of months since 1970 as a simple measure of repo freshness.
// It is monotonically increasing and stable across re-indexes and restarts.
r.Rank = monthsSince1970(repo.LatestCommitDate)
Expand Down
2 changes: 1 addition & 1 deletion cmd/zoekt-sourcegraph-indexserver/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (o *indexArgs) BuildOptions() *build.Options {
"fork": marshalBool(o.Fork),
"archived": marshalBool(o.Archived),
// Calculate repo rank based on the latest commit date.
"latest_commit_date": "1",
"latestCommitDate": "1",
},
},
IndexDir: o.IndexDir,
Expand Down
36 changes: 32 additions & 4 deletions cmd/zoekt-sourcegraph-indexserver/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/sourcegraph/log/logtest"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -494,7 +495,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref HEAD deadbeef",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latestCommitDate 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand All @@ -517,7 +518,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref HEAD deadbeef",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latestCommitDate 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand Down Expand Up @@ -549,7 +550,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref refs/heads/dev feebdaed",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latestCommitDate 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand Down Expand Up @@ -597,7 +598,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref refs/heads/release 12345678",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latestCommitDate 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand Down Expand Up @@ -685,3 +686,30 @@ func (m *mockGRPCClient) UpdateIndexStatus(ctx context.Context, in *proto.Update
}

var _ proto.ZoektConfigurationServiceClient = &mockGRPCClient{}

// Tests whether we can set git config values without error.
func TestSetZoektConfig(t *testing.T) {
dir := t.TempDir()

// init git dir
script := `mkdir repo
cd repo
git init -b main
`
cmd := exec.Command("/bin/sh", "-euxc", script)
cmd.Dir = dir
_, err := cmd.CombinedOutput()
require.NoError(t, err)

var out []byte
c := gitIndexConfig{
runCmd: func(cmd *exec.Cmd) error {
var err error
out, err = cmd.CombinedOutput()
return err
},
}

err = setZoektConfig(context.Background(), filepath.Join(dir, "repo"), &indexArgs{}, c)
require.NoError(t, err, string(out))
}
2 changes: 1 addition & 1 deletion internal/e2e/e2e_rank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func indexURL(indexDir, u string) error {
RepositoryDescription: zoekt.Repository{
// Use the latest commit date to calculate the repo rank when loading the shard.
// This is the same setting we use in production.
RawConfig: map[string]string{"latest_commit_date": "1"},
RawConfig: map[string]string{"latestCommitDate": "1"},
},
})
if err != nil {
Expand Down

0 comments on commit ec0f506

Please sign in to comment.