Repository navigation
feat(cli): allow an alternate Git repository path to be specified #1468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smoyer64
wants to merge
7
commits into
trunk
Choose a base branch
from
feat/1467/git-repo-path
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
331e6dc
feat(cli): allow an alternate Git repository path to be specified
smoyer64 2e108ac
docs(cli): improve wording of help for the -C flag
smoyer64 1708387
docs: fix Go doc comment
smoyer64 8a9e4ce
Merge branch 'trunk' into feat/1467/git-repo-path
smoyer64 22f12e2
chore: rework --git-dir to behave like Git
smoyer64 923385c
test(git-dir): make tests (more) OS-agnostic
smoyer64 5928757
chore(git-dir): fix spelling in test name
smoyer64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ type Env struct { | |
| In In | ||
| Out Out | ||
| Err Out | ||
| GitDir string | ||
| } | ||
|
|
||
| func NewEnv() *Env { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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" | ||||||
|
smoyer64 marked this conversation as resolved.
|
||||||
| ) | ||||||
|
|
||||||
| func NewRootCommand(version string) *cobra.Command { | ||||||
|
|
@@ -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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| takes precedence over the CLI flag. | ||||||
| `, | ||||||
|
|
||||||
| PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||||||
|
|
@@ -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>`) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.