diff --git a/docs/commands.md b/docs/commands.md index 11436e46..05f47bd4 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -530,7 +530,8 @@ git drs bucket add-project production \ ### `git drs version` -Display version information. +Display version information. Works from any directory, without a Git repository +or git-drs initialization, and does not create a repository log file. ```bash git drs version diff --git a/docs/installation.md b/docs/installation.md index 7941fa7d..173c90df 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -62,6 +62,8 @@ export PATH="$PATH:$HOME/.local/bin" ## Verify The Install +Run either command from any directory; no Git repository or git-drs initialization is required. + ```bash git-drs version git drs version diff --git a/git-drs.go b/git-drs.go index f2dc2c62..d0dd1e24 100644 --- a/git-drs.go +++ b/git-drs.go @@ -11,10 +11,12 @@ import ( func main() { - _, err := drslog.NewLogger("", true) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to open log file: %v", err) - os.Exit(1) + // Version only reads build metadata and needs no repository log. + if len(os.Args) < 2 || os.Args[1] != "version" { + if _, err := drslog.NewLogger("", true); err != nil { + fmt.Fprintf(os.Stderr, "Failed to open log file: %v", err) + os.Exit(1) + } } // Keep credential helper out of the user-facing command tree/help output. diff --git a/git-drs_test.go b/git-drs_test.go new file mode 100644 index 00000000..15005383 --- /dev/null +++ b/git-drs_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "bytes" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestVersionOutsideRepository(t *testing.T) { + binary := filepath.Join(t.TempDir(), "git-drs") + build := exec.Command("go", "build", "-o", binary, ".") + output, err := build.CombinedOutput() + require.NoError(t, err, "%s", output) + + dir := t.TempDir() + command := exec.Command(binary, "version") + command.Dir = dir + // Prevent an inherited Git environment from pointing at a repository. + command.Env = append(os.Environ(), "GIT_DIR=", "GIT_WORK_TREE=", "GIT_CEILING_DIRECTORIES="+filepath.Dir(dir)) + var stderr bytes.Buffer + command.Stderr = &stderr + output, err = command.Output() + require.NoError(t, err, "%s", stderr.String()) + require.Empty(t, stderr.String()) + require.Regexp(t, `^git-drs \S+\n$`, string(output)) + entries, err := os.ReadDir(dir) + require.NoError(t, err) + require.Empty(t, entries, "version should not create repository or log files") +}