From 934ae5206625fa561fffc8c5d3e853cfe15b5063 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Tue, 13 Nov 2018 10:33:48 -0800 Subject: [PATCH] commands/command_status.go: require a working copy 'git lfs status' is functional in a non-work tree repository only some of the time. Particularly, when there is nothing to report (and therefore no files to open), 'git lfs status' work as expected. However, when there are files changed in the index, Git LFS tries to open them to see what their hash was before/after the change, and in particular, if they are/were/remain Git LFS pointers. This feature is used to power the "Git -> LFS " output of 'git lfs status'. When in a bare repository and in the aforementioned scenario, the above operation will fail to open a file that does not exist, for the repository is bare and thus does not contain a working tree. In order to bring about a more consistent experience when using 'git lfs status', let's match the behavior upstream and exit immediately with the message: $ git lfs status This operation must be run in a work tree. --- commands/command_status.go | 1 + commands/commands.go | 16 ++++++++++++++++ docs/man/git-lfs-status.1.ronn | 2 ++ t/t-status.sh | 16 ++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/commands/command_status.go b/commands/command_status.go index 47bec0ca..649574f5 100644 --- a/commands/command_status.go +++ b/commands/command_status.go @@ -22,6 +22,7 @@ var ( func statusCommand(cmd *cobra.Command, args []string) { requireInRepo() + requireWorkingCopy() // tolerate errors getting ref so this works before first commit ref, _ := git.CurrentRef() diff --git a/commands/commands.go b/commands/commands.go index bcffb7bd..e7e351de 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -309,6 +309,22 @@ func requireInRepo() { } } +// requireWorkingCopy requires that the working directory be a work tree, i.e., +// that it not be bare. If it is bare (or the state of the repository could not +// be determined), this function will terminate the program. +func requireWorkingCopy() { + bare, err := git.IsBare() + if err != nil { + ExitWithError(errors.Wrap( + err, "fatal: could not determine bareness")) + } + + if bare { + Print("This operation must be run in a work tree.") + os.Exit(128) + } +} + func handlePanic(err error) string { if err == nil { return "" diff --git a/docs/man/git-lfs-status.1.ronn b/docs/man/git-lfs-status.1.ronn index 4fe533db..8a37e723 100644 --- a/docs/man/git-lfs-status.1.ronn +++ b/docs/man/git-lfs-status.1.ronn @@ -18,6 +18,8 @@ Display paths of Git LFS objects that * have differences between the working tree and the index file. These are files that could be staged using `git add`. +This command must be run in a non-bare repository. + ## OPTIONS * `--porcelain`: diff --git a/t/t-status.sh b/t/t-status.sh index 0ce4c546..b55194b3 100755 --- a/t/t-status.sh +++ b/t/t-status.sh @@ -448,3 +448,19 @@ Git LFS objects not staged for commit:" [ "$expected" = "$(git lfs status)" ] ) end_test + +begin_test "status (without a working copy)" +( + reponame="status-no-working-copy.git" + + git init --bare "$reponame" + cd "$reponame" + + git lfs status 2>&1 | tee status.log + if [ "0" -eq "${PIPESTATUS[0]}" ]; then + echo >&2 "git lfs status should have failed, didn't ..." + exit 1 + fi + [ "This operation must be run in a work tree." = "$(cat status.log)" ] +) +end_test