Skip to content

Commit 934ae52

Browse files
committed
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 <sha-1> -> LFS <sha-256>" 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.
1 parent 2d88201 commit 934ae52

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

commands/command_status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222

2323
func statusCommand(cmd *cobra.Command, args []string) {
2424
requireInRepo()
25+
requireWorkingCopy()
2526

2627
// tolerate errors getting ref so this works before first commit
2728
ref, _ := git.CurrentRef()

commands/commands.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ func requireInRepo() {
309309
}
310310
}
311311

312+
// requireWorkingCopy requires that the working directory be a work tree, i.e.,
313+
// that it not be bare. If it is bare (or the state of the repository could not
314+
// be determined), this function will terminate the program.
315+
func requireWorkingCopy() {
316+
bare, err := git.IsBare()
317+
if err != nil {
318+
ExitWithError(errors.Wrap(
319+
err, "fatal: could not determine bareness"))
320+
}
321+
322+
if bare {
323+
Print("This operation must be run in a work tree.")
324+
os.Exit(128)
325+
}
326+
}
327+
312328
func handlePanic(err error) string {
313329
if err == nil {
314330
return ""

docs/man/git-lfs-status.1.ronn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Display paths of Git LFS objects that
1818
* have differences between the working tree and the index file. These
1919
are files that could be staged using `git add`.
2020

21+
This command must be run in a non-bare repository.
22+
2123
## OPTIONS
2224

2325
* `--porcelain`:

t/t-status.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,19 @@ Git LFS objects not staged for commit:"
448448
[ "$expected" = "$(git lfs status)" ]
449449
)
450450
end_test
451+
452+
begin_test "status (without a working copy)"
453+
(
454+
reponame="status-no-working-copy.git"
455+
456+
git init --bare "$reponame"
457+
cd "$reponame"
458+
459+
git lfs status 2>&1 | tee status.log
460+
if [ "0" -eq "${PIPESTATUS[0]}" ]; then
461+
echo >&2 "git lfs status should have failed, didn't ..."
462+
exit 1
463+
fi
464+
[ "This operation must be run in a work tree." = "$(cat status.log)" ]
465+
)
466+
end_test

0 commit comments

Comments
 (0)