Skip to content
Merged
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
4 changes: 3 additions & 1 deletion internal/project/auto/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (

// DetectGit detects if current directory is git repository.
func (builder *builder) DetectGit() (bool, error) {
repo, err := git.PlainOpen(".")
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{
EnableDotGitCommonDir: true,
})
if err != nil {
// not a git repo, ignore
return false, nil //nolint:nilerr
Expand Down
66 changes: 66 additions & 0 deletions internal/project/auto/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package auto_test

import (
"context"
"os/exec"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/siderolabs/kres/internal/project/auto"
"github.com/siderolabs/kres/internal/project/meta"
)

func TestDetectGitWorktree(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not found")
}

root := filepath.Join(t.TempDir(), "repo")
worktree := filepath.Join(t.TempDir(), "worktree")

runGit(t, "", "init", root)
runGit(t, root, "config", "user.email", "test@example.com")
runGit(t, root, "config", "user.name", "Test")
runGit(t, root, "config", "commit.gpgsign", "false")
runGit(t, root, "commit", "--allow-empty", "-m", "init")
runGit(t, root, "branch", "-M", "main")
runGit(t, root, "remote", "add", "origin", "git@github.com:siderolabs/example.git")
runGit(t, root, "config", "branch.main.remote", "origin")
runGit(t, root, "config", "branch.main.merge", "refs/heads/main")
runGit(t, root, "worktree", "add", "-b", "feature", worktree)

t.Chdir(worktree)

options := &meta.Options{
CompileGithubWorkflowsOnly: true,
}

_, err := auto.Build(options)
require.NoError(t, err)

assert.Equal(t, "main", options.MainBranch)
assert.Equal(t, "feature", options.CurrentBranch)
assert.Equal(t, "siderolabs", options.GitHubOrganization)
assert.Equal(t, "example", options.GitHubRepository)
}

func runGit(t *testing.T, dir string, args ...string) {
t.Helper()

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir

output, err := cmd.CombinedOutput()
require.NoError(t, err, string(output))
}