Skip to content
Open
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
1 change: 1 addition & 0 deletions commands/execenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Env struct {
In In
Out Out
Err Out
GitDir string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, let's remove this from Env. There is better ways and there is no good reason to have this here.

}

func NewEnv() *Env {
Expand Down
43 changes: 39 additions & 4 deletions commands/execenv/loading.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package execenv

import (
"errors"
"fmt"
"os"

Expand All @@ -14,18 +15,18 @@ import (
"github.com/git-bug/git-bug/util/interrupt"
)

// LoadRepo is a pre-run function that load the repository for use in a command
// LoadRepo is a pre-run function that loads the repository for use in a command.
func LoadRepo(env *Env) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
repoPath, err := getRepoPath(env)
if err != nil {
return fmt.Errorf("unable to get the current working directory: %q", err)
return err
}

// Note: we are not loading clocks here because we assume that LoadRepo is only used
// when we don't manipulate entities, or as a child call of LoadBackend which will
// read all clocks anyway.
env.Repo, err = repository.OpenGoGitRepo(cwd, gitBugNamespace, nil)
env.Repo, err = repository.OpenGoGitRepo(repoPath, gitBugNamespace, nil)
if err == repository.ErrNotARepo {
return fmt.Errorf("%s must be run from within a git Repo", RootCommandName)
}
Expand Down Expand Up @@ -171,3 +172,37 @@ func CacheBuildProgressBar(env *Env, events chan cache.BuildEvent) error {

return nil
}

var errNotDirectory = errors.New("path must be a directory for --git-dir or GIT_DIR")

func getRepoPath(env *Env) (string, error) {
Comment thread
smoyer64 marked this conversation as resolved.
var path string

if gitDir := os.Getenv("GIT_DIR"); gitDir != "" {
path = gitDir
}

if path == "" && env.GitDir != "" {
path = env.GitDir
}

if path == "" {
wd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("unable to get the current working directory: %q", err)
}

path = wd
}

fi, err := os.Stat(path)
if err != nil {
return "", err
}

if !fi.IsDir() {
return "", errNotDirectory
}

return path, nil
}
81 changes: 81 additions & 0 deletions commands/execenv/loading_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package execenv

import (
"os"
"path/filepath"
"strings"
"testing"

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

// ***** WARNING ***** - due to the use of testing with an environment
//
// variable, do NOT use t.Parallel() with these
// test cases.
func TestGetRepoPath(t *testing.T) {
t.Run("no Git path provided (use WD)", func(t *testing.T) {
// WD during tests is the directory containing the test file
path, err := getRepoPath(&Env{})
require.NoError(t, err)
assert.True(t, strings.HasSuffix(path, filepath.Join("commands", "execenv")))
})

t.Run("flag provided", func(t *testing.T) {
gitDir, err := os.UserHomeDir()
require.NoError(t, err)

path, err := getRepoPath(&Env{
GitDir: gitDir,
})
require.NoError(t, err)
assert.Equal(t, gitDir, path)
})

t.Run("environment variable provided", func(t *testing.T) {
gitDir, err := os.UserHomeDir()
require.NoError(t, err)
t.Setenv("GIT_DIR", gitDir)

path, err := getRepoPath(&Env{})
require.NoError(t, err)
assert.Equal(t, gitDir, path)
})

t.Run("GIT_DIR supersedes --git-dir", func(t *testing.T) {
cacheDir, err := os.UserCacheDir()
require.NoError(t, err)

configDir, err := os.UserConfigDir()
require.NoError(t, err)
t.Setenv("GIT_DIR", configDir)

path, err := getRepoPath(&Env{
GitDir: cacheDir,
})
require.NoError(t, err)
assert.Equal(t, configDir, path)

})

t.Run("path does not exist", func(t *testing.T) {
_, err := getRepoPath(&Env{
GitDir: filepath.Join("/", "tmp", "does-not-exist"),
})
require.ErrorIs(t, err, os.ErrNotExist)
})

t.Run("path is not a directory", func(t *testing.T) {
gitDir := filepath.Join(os.TempDir(), "plain-file")

f, err := os.Create(gitDir)
require.NoError(t, err)
require.NoError(t, f.Close())

_, err = getRepoPath(&Env{
GitDir: gitDir,
})
require.ErrorIs(t, err, errNotDirectory)
})
}
11 changes: 8 additions & 3 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (

"github.com/spf13/cobra"

"github.com/git-bug/git-bug/commands/bridge"
"github.com/git-bug/git-bug/commands/bug"
bridgecmd "github.com/git-bug/git-bug/commands/bridge"
bugcmd "github.com/git-bug/git-bug/commands/bug"
"github.com/git-bug/git-bug/commands/execenv"
"github.com/git-bug/git-bug/commands/user"
usercmd "github.com/git-bug/git-bug/commands/user"
Comment thread
smoyer64 marked this conversation as resolved.
)

func NewRootCommand(version string) *cobra.Command {
Expand All @@ -21,6 +21,10 @@ git-bug use git objects to store the bug tracking separated from the files
history. As bugs are regular git objects, they can be pushed and pulled from/to
the same git remote you are already using to collaborate with other people.

By default, git-bug manages bugs and users in the Git repository containing
the current working directory. This behavior can be changed using the --git-dir
flag described below, or by setting the GIT-DIR environment variable, which

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flag described below, or by setting the GIT-DIR environment variable, which
flag described below, or by setting the GIT_DIR environment variable, which

takes precedence over the CLI flag.
`,

PersistentPreRun: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -55,6 +59,7 @@ the same git remote you are already using to collaborate with other people.
}

env := execenv.NewEnv()
cmd.PersistentFlags().StringVar(&env.GitDir, "git-dir", "", `run as if git-bug was started in <path>`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we mention the env variable in the comment?


addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
Expand Down