Skip to content

Commit 58efafb

Browse files
authored
Fix version command outside Git repositories (#270)
1 parent 86d82dc commit 58efafb

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎docs/commands.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ git drs bucket add-project production \
530530

531531
### `git drs version`
532532

533-
Display version information.
533+
Display version information. Works from any directory, without a Git repository
534+
or git-drs initialization, and does not create a repository log file.
534535

535536
```bash
536537
git drs version

‎docs/installation.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export PATH="$PATH:$HOME/.local/bin"
6262

6363
## Verify The Install
6464

65+
Run either command from any directory; no Git repository or git-drs initialization is required.
66+
6567
```bash
6668
git-drs version
6769
git drs version

‎git-drs.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111

1212
func main() {
1313

14-
_, err := drslog.NewLogger("", true)
15-
if err != nil {
16-
fmt.Fprintf(os.Stderr, "Failed to open log file: %v", err)
17-
os.Exit(1)
14+
// Version only reads build metadata and needs no repository log.
15+
if len(os.Args) < 2 || os.Args[1] != "version" {
16+
if _, err := drslog.NewLogger("", true); err != nil {
17+
fmt.Fprintf(os.Stderr, "Failed to open log file: %v", err)
18+
os.Exit(1)
19+
}
1820
}
1921

2022
// Keep credential helper out of the user-facing command tree/help output.

‎git-drs_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestVersionOutsideRepository(t *testing.T) {
14+
binary := filepath.Join(t.TempDir(), "git-drs")
15+
build := exec.Command("go", "build", "-o", binary, ".")
16+
output, err := build.CombinedOutput()
17+
require.NoError(t, err, "%s", output)
18+
19+
dir := t.TempDir()
20+
command := exec.Command(binary, "version")
21+
command.Dir = dir
22+
// Prevent an inherited Git environment from pointing at a repository.
23+
command.Env = append(os.Environ(), "GIT_DIR=", "GIT_WORK_TREE=", "GIT_CEILING_DIRECTORIES="+filepath.Dir(dir))
24+
var stderr bytes.Buffer
25+
command.Stderr = &stderr
26+
output, err = command.Output()
27+
require.NoError(t, err, "%s", stderr.String())
28+
require.Empty(t, stderr.String())
29+
require.Regexp(t, `^git-drs \S+\n$`, string(output))
30+
entries, err := os.ReadDir(dir)
31+
require.NoError(t, err)
32+
require.Empty(t, entries, "version should not create repository or log files")
33+
}

0 commit comments

Comments
 (0)