Skip to content

Avoid panic during update index - #570

Merged
rubyist merged 2 commits into
masterfrom
index-progress
Aug 14, 2015
Merged

rubyist merged 2 commits into
masterfrom
index-progress

Conversation

@rubyist

@rubyist rubyist commented Aug 7, 2015

Copy link
Copy Markdown
Contributor

This PR will avoid any use of Panic() while the git update-index loop is running. If the process exits while update-index is in the middle of processing a file, the index can be left in an inconsistent, locked state.

/cc #558

rubyist added 2 commits August 7, 2015 15:00
When using make with append, both a capacity and length must be used in
the make call. If only the length is used the array will have len zero
values at the beginning and append will start adding values at the end.
This is a small number of values so just skip make altogether.
@rubyist rubyist added the wip label Aug 7, 2015
@rubyist

rubyist commented Aug 13, 2015

Copy link
Copy Markdown
Contributor Author

Marking this for review as is, it is only fixing the index lock problem. Using the update-index output to drive progress ended up being messier than I'd like so I'm going to punt on that for now. There are a couple of optimizations for speeding up the checkout, I'll put those in a new PR so this can ship.

@rubyist rubyist added review and removed wip labels Aug 13, 2015
@sinbad

sinbad commented Aug 13, 2015

Copy link
Copy Markdown
Contributor

👍 Although this will conflict with #573 😭

sinbad added a commit to sinbad/git-lfs that referenced this pull request Aug 14, 2015
@sinbad

sinbad commented Aug 14, 2015

Copy link
Copy Markdown
Contributor

I've pre-emptively merged this into #573 to head off any merge conflicts.

rubyist added a commit that referenced this pull request Aug 14, 2015
Avoid panic during update index
@rubyist
rubyist merged commit 22dca3b into master Aug 14, 2015
@rubyist
rubyist deleted the index-progress branch August 14, 2015 13:26
@rubyist

rubyist commented Aug 14, 2015

Copy link
Copy Markdown
Contributor Author

Thanks for the review, one of these times I'll submit a PR that doesn't conflict with yours!

chrisd8088 added a commit that referenced this pull request Oct 16, 2025
In PR #527 we introduced the "git lfs checkout" and "git lfs pull"
commands and added some initial tests of those commands to our
test suite, which we have subsequently expanded over time.

At present, we do not have any tests of these commands' behaviour
when they unable to create a subdirectory because a regular file
already exists with the same name.  In these cases, we expect the
commands to report an error but otherwise continue and ultimately
return a zero exit code.

This has been true since commit e909264
of PR #570, when we revised the "git lfs checkout" command so it would
not panic if the DecodePointerFromFile() function in our "lfs" package
returned an error other than an ErrNotExist error.  In particular, the
stat(2) system call, which is invoked by the DecodePointerFromFile()
function via the Stat() function of the "os" package in the standard Go
library, will return an ENOTDIR error number on Unix systems when any of
the components of a given path prefix are not directories.

Before we adjust how the "git lfs checkout" and "git lfs pull" commands
check and create files, we add a pair of new tests which validate that
the commands make no changes when a file exists in place of a directory
which would otherwise be created by the commands.

After each time these tests run a "git lfs checkout" or "git lfs pull"
command, they also check that none of the pre-existing alterations to
the working tree have been accidentally staged in the Git index.  In the
normal case, when these commands successfully update a Git LFS file in
the working tree, they should then call the "git update-index" command to
refresh the modification timestamp Git has cached for the file.  However,
when a directory in the path between the root of the working tree and a
Git LFS file has been replaced with a file, our commands should not update
the Git index.  In order for our tests to confirm this is the case they
call a new assert_clean_index() test helper function which runs a
"git diff-index --cached HEAD" command to check that no unexpected
changes have been staged in the index.

When our new tests run on Unix systems, we expect the "stat" system call
to appear in the error messages output by the "git lfs checkout" and
"git lfs pull" commands, along with a file path relative to the current
working directory and a generic "Checkout error" message.  This log
message is reported by the Run() method of the singleCheckout structure
in our "commands" package when the DecodePointerFromFile() function
returns an error that the "os" package's IsNotExist() function considers
not equivalent to an ErrNotExist error.  (Note that in subsequent commits
we will adjust the text of the log message and change the reported file
path to be relative to the root of the repository.)

On Windows, our new tests expect a different error message, however,
because under the same test conditions the Stat() function of the "os"
package does return an error that the IsNotExist() function considers
equivalent to an ErrNotExist error.  As a result, the Run() method of
our singleCheckout structure handles the errors differently, and proceeds
to call the RunToPath() method of the same structure, which invokes the
SmudgeToFile() method of the GitFilter structure in our "lfs" package.
That method performs another Stat() call to check the size and
permissions mode of any extant file at the given path, but ignores any
errors which might be returned.

Next, the SmudgeToFile() method runs the Create() function from the
"os" package to try to create or truncate a file at the provided path.
When an ancestor in the path is not a directory but a file this function
returns an error, which the SmudgeToFile() method then returns wrapped
with a "could not create working directory file" message.  Finally, the
Run() method of the singleCheckout structure reports this error message
along with its own "could not check out" message and a file path
relative to the root of the repository.  Therefore these are the
messages and file paths our new tests check for on Windows.

The Stat() and Lstat() functions of the "os" package share a common
internal implementation on Windows, and the details of this internal
stat() function and the "syscall" package of the Go standard library
explain why, given our test conditions, the Stat() function returns
an error equivalent to an ErrNotExist error on Windows but not on
Unix systems.

The internal implementation of the Stat() and Lstat() functions on Windows
invokes the CreateFileW() Windows API function with the OPEN_EXISTING
flag, and that system call returns an ERROR_FILE_NOT_FOUND error number
when no file exists at the given path.  The "syscall" package in the Go
standard library then maps this error number to an ErrNotExist error,
unlike how it handles the ENOTDIR error number on Unix systems:

  https://github.com/golang/go/blob/go1.24.4/src/os/stat_windows.go#L66-L71
  https://github.com/golang/go/blob/go1.24.4/src/os/stat_windows.go#L80-L85
  https://github.com/golang/go/blob/go1.24.4/src/syscall/syscall_windows.go#L182-L186
  https://github.com/golang/go/blob/go1.24.4/src/syscall/syscall_unix.go#L126-L127
  https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#parameters
chrisd8088 added a commit that referenced this pull request Oct 16, 2025
In PR #527 we introduced the "git lfs checkout" and "git lfs pull"
commands and added some initial tests of those commands to our
test suite, which we have subsequently expanded over time.

At present, we do not have any tests of these commands' behaviour
when they unable to create a subdirectory because a regular file
already exists with the same name.  In these cases, we expect the
commands to report an error but otherwise continue and ultimately
return a zero exit code.

This has been true since commit e909264
of PR #570, when we revised the "git lfs checkout" command so it would
not panic if the DecodePointerFromFile() function in our "lfs" package
returned an error other than an ErrNotExist error.  In particular, the
stat(2) system call, which is invoked by the DecodePointerFromFile()
function via the Stat() function of the "os" package in the standard Go
library, will return an ENOTDIR error number on Unix systems when any of
the components of a given path prefix are not directories.

Before we adjust how the "git lfs checkout" and "git lfs pull" commands
check and create files, we add a pair of new tests which validate that
the commands make no changes when a file exists in place of a directory
which would otherwise be created by the commands.

After each time these tests run a "git lfs checkout" or "git lfs pull"
command, they also check that none of the pre-existing alterations to
the working tree have been accidentally staged in the Git index.  In the
normal case, when these commands successfully update a Git LFS file in
the working tree, they should then call the "git update-index" command to
refresh the modification timestamp Git has cached for the file.  However,
when a directory in the path between the root of the working tree and a
Git LFS file has been replaced with a file, our commands should not update
the Git index.  In order for our tests to confirm this is the case they
call a new assert_clean_index() test helper function which runs a
"git diff-index --cached HEAD" command to check that no unexpected
changes have been staged in the index.

When our new tests run on Unix systems, we expect the "stat" system call
to appear in the error messages output by the "git lfs checkout" and
"git lfs pull" commands, along with a file path relative to the current
working directory and a generic "Checkout error" message.  This log
message is reported by the Run() method of the singleCheckout structure
in our "commands" package when the DecodePointerFromFile() function
returns an error that the "os" package's IsNotExist() function considers
not equivalent to an ErrNotExist error.  (Note that in subsequent commits
we will adjust the text of the log message and change the reported file
path to be relative to the root of the repository.)

On Windows, our new tests expect a different error message, however,
because under the same test conditions the Stat() function of the "os"
package does return an error that the IsNotExist() function considers
equivalent to an ErrNotExist error.  As a result, the Run() method of
our singleCheckout structure handles the errors differently, and proceeds
to call the RunToPath() method of the same structure, which invokes the
SmudgeToFile() method of the GitFilter structure in our "lfs" package.
That method performs another Stat() call to check the size and
permissions mode of any extant file at the given path, but ignores any
errors which might be returned.

Next, the SmudgeToFile() method runs the Create() function from the
"os" package to try to create or truncate a file at the provided path.
When an ancestor in the path is not a directory but a file this function
returns an error, which the SmudgeToFile() method then returns wrapped
with a "could not create working directory file" message.  Finally, the
Run() method of the singleCheckout structure reports this error message
along with its own "could not check out" message and a file path
relative to the root of the repository.  Therefore these are the
messages and file paths our new tests check for on Windows.

The Stat() and Lstat() functions of the "os" package share a common
internal implementation on Windows, and the details of this internal
stat() function and the "syscall" package of the Go standard library
explain why, given our test conditions, the Stat() function returns
an error equivalent to an ErrNotExist error on Windows but not on
Unix systems.

The internal implementation of the Stat() and Lstat() functions on Windows
invokes the CreateFileW() Windows API function with the OPEN_EXISTING
flag, and that system call returns an ERROR_FILE_NOT_FOUND error number
when no file exists at the given path.  The "syscall" package in the Go
standard library then maps this error number to an ErrNotExist error,
unlike how it handles the ENOTDIR error number on Unix systems:

  https://github.com/golang/go/blob/go1.24.4/src/os/stat_windows.go#L66-L71
  https://github.com/golang/go/blob/go1.24.4/src/os/stat_windows.go#L80-L85
  https://github.com/golang/go/blob/go1.24.4/src/syscall/syscall_windows.go#L182-L186
  https://github.com/golang/go/blob/go1.24.4/src/syscall/syscall_unix.go#L126-L127
  https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#parameters
hswong3i pushed a commit to alvistack/git-lfs-git-lfs that referenced this pull request Oct 18, 2025
In PR git-lfs#527 we introduced the "git lfs checkout" and "git lfs pull"
commands and added some initial tests of those commands to our
test suite, which we have subsequently expanded over time.

At present, we do not have any tests of these commands' behaviour
when they unable to create a subdirectory because a regular file
already exists with the same name.  In these cases, we expect the
commands to report an error but otherwise continue and ultimately
return a zero exit code.

This has been true since commit e909264
of PR git-lfs#570, when we revised the "git lfs checkout" command so it would
not panic if the DecodePointerFromFile() function in our "lfs" package
returned an error other than an ErrNotExist error.  In particular, the
stat(2) system call, which is invoked by the DecodePointerFromFile()
function via the Stat() function of the "os" package in the standard Go
library, will return an ENOTDIR error number on Unix systems when any of
the components of a given path prefix are not directories.

Before we adjust how the "git lfs checkout" and "git lfs pull" commands
check and create files, we add a pair of new tests which validate that
the commands make no changes when a file exists in place of a directory
which would otherwise be created by the commands.

After each time these tests run a "git lfs checkout" or "git lfs pull"
command, they also check that none of the pre-existing alterations to
the working tree have been accidentally staged in the Git index.  In the
normal case, when these commands successfully update a Git LFS file in
the working tree, they should then call the "git update-index" command to
refresh the modification timestamp Git has cached for the file.  However,
when a directory in the path between the root of the working tree and a
Git LFS file has been replaced with a file, our commands should not update
the Git index.  In order for our tests to confirm this is the case they
call a new assert_clean_index() test helper function which runs a
"git diff-index --cached HEAD" command to check that no unexpected
changes have been staged in the index.

When our new tests run on Unix systems, we expect the "stat" system call
to appear in the error messages output by the "git lfs checkout" and
"git lfs pull" commands, along with a file path relative to the current
working directory and a generic "Checkout error" message.  This log
message is reported by the Run() method of the singleCheckout structure
in our "commands" package when the DecodePointerFromFile() function
returns an error that the "os" package's IsNotExist() function considers
not equivalent to an ErrNotExist error.  (Note that in subsequent commits
we will adjust the text of the log message and change the reported file
path to be relative to the root of the repository.)

On Windows, our new tests expect a different error message, however,
because under the same test conditions the Stat() function of the "os"
package does return an error that the IsNotExist() function considers
equivalent to an ErrNotExist error.  As a result, the Run() method of
our singleCheckout structure handles the errors differently, and proceeds
to call the RunToPath() method of the same structure, which invokes the
SmudgeToFile() method of the GitFilter structure in our "lfs" package.
That method performs another Stat() call to check the size and
permissions mode of any extant file at the given path, but ignores any
errors which might be returned.

Next, the SmudgeToFile() method runs the Create() function from the
"os" package to try to create or truncate a file at the provided path.
When an ancestor in the path is not a directory but a file this function
returns an error, which the SmudgeToFile() method then returns wrapped
with a "could not create working directory file" message.  Finally, the
Run() method of the singleCheckout structure reports this error message
along with its own "could not check out" message and a file path
relative to the root of the repository.  Therefore these are the
messages and file paths our new tests check for on Windows.

The Stat() and Lstat() functions of the "os" package share a common
internal implementation on Windows, and the details of this internal
stat() function and the "syscall" package of the Go standard library
explain why, given our test conditions, the Stat() function returns
an error equivalent to an ErrNotExist error on Windows but not on
Unix systems.

The internal implementation of the Stat() and Lstat() functions on Windows
invokes the CreateFileW() Windows API function with the OPEN_EXISTING
flag, and that system call returns an ERROR_FILE_NOT_FOUND error number
when no file exists at the given path.  The "syscall" package in the Go
standard library then maps this error number to an ErrNotExist error,
unlike how it handles the ENOTDIR error number on Unix systems:

  https://github.com/golang/go/blob/go1.24.4/src/os/stat_windows.go#L66-L71
  https://github.com/golang/go/blob/go1.24.4/src/os/stat_windows.go#L80-L85
  https://github.com/golang/go/blob/go1.24.4/src/syscall/syscall_windows.go#L182-L186
  https://github.com/golang/go/blob/go1.24.4/src/syscall/syscall_unix.go#L126-L127
  https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants