Skip to content

Git LFS Extensions - #486

Merged
technoweenie merged 5 commits into
git-lfs:masterfrom
ryansimmen:extensions
Aug 4, 2015
Merged

technoweenie merged 5 commits into
git-lfs:masterfrom
ryansimmen:extensions

Conversation

@ryansimmen

Copy link
Copy Markdown
Contributor

@technoweenie

Copy link
Copy Markdown
Contributor

I haven't done an exhaustive review of this yet. But I'm glad you were able to pull this off without touching too much of the code base. Hopefully the extension piping can stay in lfs.PointerClean and lfs. PointerSmudge. The change in commands/command_clean.go is an example of something that I'd love to clean up in a future PR.

Also, what do you think about git lfs fsck changing so that it actually runs the smudge extension(s) to confirm that the object is still valid? Right now it just compares the hash of the content on disk to the object ID.

Comment thread lfs/scanner.go

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened an issue to turn this into a dynamic setting based on the repo config: #524

@technoweenie technoweenie mentioned this pull request Jul 24, 2015
8 of 13 tasks
Comment thread commands/command_ext.go

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer short guard clauses as opposed to if/else:

if len(args) == 0 {
  printAllExts()
  return
}

config := lfs.Config
for _, key := range args {
  ext := config.Extensions()[key]
  printExt(ext)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works for me.

@technoweenie

Copy link
Copy Markdown
Contributor

On a first glance, it's difficult to tell the difference between Extension and PointerExtension. I think the documentation should be a little more clear. Extension objects are parsed from the Git config, while PointerExtension objects are parsed from the Git LFS Pointer.

Comment thread lfs/pointer.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should use *PointerExtension where possible, so you don't have to worry about what functions take the direct objects, and which take pointers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

@technoweenie

Copy link
Copy Markdown
Contributor

So IMO the only things left before merging:

After that, I really want to put this system through its paces. But we should have plenty of time before v0.6.0 for that.

@ryansimmen

Copy link
Copy Markdown
Contributor Author

Merge conflicts are resolved. Once you give the thumbs up I'll squash the fixup! commit.

Comment thread lfs/extension.go

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could combine this loop into the previous one:

priorities []int
for n, ext := range m {
        p := ext.Priority
        if _, exist := pMap[p]; exist {
            err := fmt.Errorf("duplicate priority %d on %s", p, n)
            return nil, err
        }
        pMap[p] = ext
                priorities = append(priorities, p)
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

technoweenie added a commit that referenced this pull request Aug 4, 2015
@technoweenie
technoweenie merged commit e01eff6 into git-lfs:master Aug 4, 2015
@ryansimmen
ryansimmen deleted the extensions branch January 4, 2016 23:33
@technoweenie technoweenie removed their assignment Jul 21, 2016
chrisd8088 added a commit that referenced this pull request Oct 16, 2025
In PR #486 we introduced support for Git LFS pointer extensions, along
with some related tests in our Go test suite and the t/t-ext.sh shell
test script, which validates that pointer extensions are reported as
expected by the "git lfs ext" command.  (Note that Git LFS pointer
extension support is still technically considered experimental,
according to our documentation.)

However, we do not have any tests which exercise a complete Git LFS
pointer extension configuration including an extension program which
transforms the content of files tracked as Git LFS objects.

We therefore add a new lfstest-caseinverterextension test utility
program and make use of it in a set of shell tests of our "git lfs clean",
"git lfs smudge", and "git lfs filter-process" commands, as well as
in tests of our "git lfs checkout" and "git lfs pull" commands.  All
of these commands execute the lfstest-caseinverterextension utility
and confirm that it runs as expected.

Note that at present, when we execute a pointer extension program within
one of our "git lfs clean", "git lfs smudge", and "git lfs filter-process"
commands, our commands have typically inherited their execution
environment from Git.  When Git runs one of these commands to perform
a "clean" or "smudge" filter operation, it has already changed the current
working directory to be the root of the repository's working tree.  The
setup_git_directory_gently() function in Git is normally run shortly
after starting, and if it detects that the current working directory
is within a Git work tree, it changes the current working directory to
the root of that work tree:

  https://github.com/git/git/blob/v2.50.1/setup.c#L1758-L1760

The file paths Git then passes to our filter commands are always relative
to the root of the repository.  Git passes these file paths in place of
the "%f" command-line specifier from the filter configuration options, and
for long-running filter processes such as our "git lfs filter-process"
command, Git passes an equivalent file path as the value of the "pathname"
keys it sends to the filter process.  The gitattributes(5) manual page
notes that files may not actually exist at these file paths, or may have
different contents than the ones Git pipes to the filter process, and so
filter programs should not attempt to access files at these paths:

  https://github.com/git/git/blob/v2.50.1/Documentation/gitattributes.adoc?plain=1#L503-L507

Likewise, when the Git LFS client invokes a Git LFS pointer extension
program, it passes a file path in place of any "%f" command-line
specifier in the extension configuration options, while piping the
actual file contents to the program on the standard input file
descriptor.  If the client itself has been run by Git as a filter program,
then the file path will be relative to the root of the repository, and
the current working directory will be the root of the repository's
working tree.

When the Git LFS client is not run by Git as a filter program but executed
directly via the "git lfs checkout" or "git lfs pull" commands, however,
we do not change the current working directory before invoking pointer
extension programs, and pass file paths that are relative to the current
working directory rather than the repository root.  One exception is
when the --to option of the "git lfs checkout" command is specified,
in which case we pass the file path argument of that option to any
pointer extension programs instead of a path to the pointer file whose
contents they are processing.

Like Git filter programs, Git LFS pointer extension program should not
expect to access an actual file at the paths passed in place of the "%f"
command-line specifiers.  At present, though, we do not make this explicit
in our documentation.

In our new tests of the pointer extension support of our "git lfs checkout"
and "git lfs pull" commands, we specifically check that the file paths
logged by the lfstest-caseinverterextension test utility are relative
to the current working directory, except when the --to option of the
"git lfs checkout" command is specified, in which case that option's
argument is passed to the test utility.  We include comments noting that
both of these behaviours differ from that seen when an extension program
is run by a Git LFS filter command executed by Git.

In subsequent commits we expect to adjust how our "git lfs checkout" and
"git lfs pull" commands handle file paths, including by changing the
current working directory to the root of the current working tree before
writing files into that work tree.  As a consequence, these commands will
pass only paths relative to the root of the repository to the
SmudgeToFile() method of the GitFilter structure in our "lfs" package.

As such, we will also necessarily change the file paths passed to Git LFS
pointer extensions so they are also always relative to the root of the
repository, and when we invoke pointer extension programs, the current
working directory will already be set to the root of the current work
tree.

At the same time we will then revise our "checkout: pointer extension",
"checkout: pointer extension with conflict", and "pull: pointer extension"
tests so they verify that the file paths received by the
lfstest-caseinverterextension test utility are relative to the root of
the repository, and will update the utility itself so it checks that
its current working directory is the root of a Git work tree.

Co-authored-by: Lars Schneider <[email protected]>
chrisd8088 added a commit that referenced this pull request Oct 16, 2025
In PR #486 we introduced support for Git LFS pointer extensions, along
with some related tests in our Go test suite and the t/t-ext.sh shell
test script, which validates that pointer extensions are reported as
expected by the "git lfs ext" command.  (Note that Git LFS pointer
extension support is still technically considered experimental,
according to our documentation.)

However, we do not have any tests which exercise a complete Git LFS
pointer extension configuration including an extension program which
transforms the content of files tracked as Git LFS objects.

We therefore add a new lfstest-caseinverterextension test utility
program and make use of it in a set of shell tests of our "git lfs clean",
"git lfs smudge", and "git lfs filter-process" commands, as well as
in tests of our "git lfs checkout" and "git lfs pull" commands.  All
of these commands execute the lfstest-caseinverterextension utility
and confirm that it runs as expected.

Note that at present, when we execute a pointer extension program within
one of our "git lfs clean", "git lfs smudge", and "git lfs filter-process"
commands, our commands have typically inherited their execution
environment from Git.  When Git runs one of these commands to perform
a "clean" or "smudge" filter operation, it has already changed the current
working directory to be the root of the repository's working tree.  The
setup_git_directory_gently() function in Git is normally run shortly
after starting, and if it detects that the current working directory
is within a Git work tree, it changes the current working directory to
the root of that work tree:

  https://github.com/git/git/blob/v2.50.1/setup.c#L1758-L1760

The file paths Git then passes to our filter commands are always relative
to the root of the repository.  Git passes these file paths in place of
the "%f" command-line specifier from the filter configuration options, and
for long-running filter processes such as our "git lfs filter-process"
command, Git passes an equivalent file path as the value of the "pathname"
keys it sends to the filter process.  The gitattributes(5) manual page
notes that files may not actually exist at these file paths, or may have
different contents than the ones Git pipes to the filter process, and so
filter programs should not attempt to access files at these paths:

  https://github.com/git/git/blob/v2.50.1/Documentation/gitattributes.adoc?plain=1#L503-L507

Likewise, when the Git LFS client invokes a Git LFS pointer extension
program, it passes a file path in place of any "%f" command-line
specifier in the extension configuration options, while piping the
actual file contents to the program on the standard input file
descriptor.  If the client itself has been run by Git as a filter program,
then the file path will be relative to the root of the repository, and
the current working directory will be the root of the repository's
working tree.

When the Git LFS client is not run by Git as a filter program but executed
directly via the "git lfs checkout" or "git lfs pull" commands, however,
we do not change the current working directory before invoking pointer
extension programs, and pass file paths that are relative to the current
working directory rather than the repository root.  One exception is
when the --to option of the "git lfs checkout" command is specified,
in which case we pass the file path argument of that option to any
pointer extension programs instead of a path to the pointer file whose
contents they are processing.

Like Git filter programs, Git LFS pointer extension program should not
expect to access an actual file at the paths passed in place of the "%f"
command-line specifiers.  At present, though, we do not make this explicit
in our documentation.

In our new tests of the pointer extension support of our "git lfs checkout"
and "git lfs pull" commands, we specifically check that the file paths
logged by the lfstest-caseinverterextension test utility are relative
to the current working directory, except when the --to option of the
"git lfs checkout" command is specified, in which case that option's
argument is passed to the test utility.  We include comments noting that
both of these behaviours differ from that seen when an extension program
is run by a Git LFS filter command executed by Git.

In subsequent commits we expect to adjust how our "git lfs checkout" and
"git lfs pull" commands handle file paths, including by changing the
current working directory to the root of the current working tree before
writing files into that work tree.  As a consequence, these commands will
pass only paths relative to the root of the repository to the
SmudgeToFile() method of the GitFilter structure in our "lfs" package.

As such, we will also necessarily change the file paths passed to Git LFS
pointer extensions so they are also always relative to the root of the
repository, and when we invoke pointer extension programs, the current
working directory will already be set to the root of the current work
tree.

At the same time we will then revise our "checkout: pointer extension",
"checkout: pointer extension with conflict", and "pull: pointer extension"
tests so they verify that the file paths received by the
lfstest-caseinverterextension test utility are relative to the root of
the repository, and will update the utility itself so it checks that
its current working directory is the root of a Git work tree.

Co-authored-by: Lars Schneider <[email protected]>
chrisd8088 added a commit that referenced this pull request Oct 16, 2025
Our "git lfs checkout" and "git lfs pull" commands retrieve a list of
Git LFS pointer files from the ScanLFSFiles() method of the GitScanner
structure type in our "lfs" package, and for each file, invoke the Run()
method of the singleCheckout structure type in our "commands" package.

For a given Git LFS pointer file, the Run() method determines whether
or not to write the contents of the object referenced by the pointer
into a file in the working tree at the appropriate path.

Because the user may execute the "git lfs checkout" and "git lfs pull"
commands from any location within a Git repository, the Run() method
tries to convert the file path of pointer, which is always relative
to the root of the repository, into a path relative to the current
working directory.  To do this, it calls the Convert() method of the
repoToCurrentPathConverter structure type in our "lfs" package, which
first prepends the absolute path to the root of the current working
tree, and then generates a relative path to that location from the
current working directory.

The repoToCurrentPathConverter structure and its methods were refactored
in commit 68efd05 of PR #1771 from the
original ConvertRepoFilesRelativeToCwd() function.  That function was
added in commit 760c7d7 of PR #527,
the same PR which introduced the "git lfs checkout" and "git lfs pull"
commands.

After calling the Convert() method to generate a path relative to the
current working directory, the Run() method passes that path to
several other functions and methods, while also using the original
input path (the one relative to the root of the repository) in other
function calls and error messages.

Because the Convert() method assumes that a current working tree is
defined (and that the current working directory is within this tree),
it will return invalid paths when these conditions are not true, such
as when the user is working in a bare repository.  In a prior commit
we therefore added a check to the Run() method so that it will not
execute the Convert() method when the no working tree is defined, which
resolved a bug whereby under unusual conditions the "git lfs checkout"
and "git lfs pull" commands could write to a file outside a bare
repository.  (In another prior commit we then also updated the
"git lfs checkout" command so that it will exit immediately when run
in a bare repository.)

The Run() method now checks the state of a "hasWorkTree" element in the
singleCheckout structure and returns without taking further action if
the element's value is "false".  When we initialize a new singleCheckout
structure in the newSingleCheckout() function of our "commands" package,
we set the value of the "hasWorkTree" element to "true" only if the
the LocalWorkingDir() method of the Configuration structure type from
our "config" package returns a non-empty path.

The LocalWorkingDir() method returns the absolute path to the root of
the current working tree, or an empty path if no working tree is defined,
as determined by the GitAndRootDirs() function in our "git" package.
The GitAndRootDirs() function runs the "git rev-parse" command with the
--show-toplevel option, and then interprets that command's output and
exit code so that if no working tree is defined, an empty path is
returned instead of a path to the work tree's root directory.

If a working tree exists and so the "hasWorkTree" element is "true",
the Run() method will proceed to invoke the Convert() method and then
pass the resultant path, which is relative to the current working
directory, to the DecodePointerFromFile() function from our "lfs"
package, and then to the RunToPath() method of the singleCheckout
structure, which passes it to the SmudgeToFile() method of the GitFilter
structure in our "lfs" package.  The Run() method later also passes the
path to the Add() method of the gitIndexer structure in our "commands"
package, which writes the path to a "git update-index" command on its
standard input file descriptor.

In a prior commit we updated the SmudgeToFile() method so that it
always creates a new file, rather than writing Git LFS object data into
an existing file, which ensures that the method will not write through
a symbolic link which exists in the place of the final filename component
of a given Git LFS pointer's file path.

In subsequent commits we will next revise the Run() method and add new
methods for the singleCheckout structure so that we check each ancestor
component of a Git LFS pointer's file path to verify that none of the
directory components of the path are symbolic links.  If a symbolic link
is found, we will report it in a new error message log format, and the
RunToPath() method will then not be invoked, nor will the gitIndexer
structure's Add() method.

With our current design, performing these checks for symbolic links,
which must be made on each path component from the root of the current
working tree to the parent directory of a given file, is complicated
by the fact that the current working directory may be located anywhere
within the work tree.  We either have to prepend zero or more ".."
path components to reach the root of the working tree, or construct an
absolute path to the root of the tree and then prepend that path to
each Git LFS pointer's file path within the repository.

To simplify both the future implementation of our checks for symbolic
links in file paths and the overall design of the Run() method, we
first adopt the approach taken by Git, which is to change the current
working directory to the root of the working tree, if one exists, before
checking for symbolic links and creating files in the work tree:

  https://github.com/git/git/blob/v2.50.1/setup.c#L1759-L1760
  https://github.com/git/git/blob/v2.50.1/symlinks.c#L63-L193

Git runs its setup_git_directory_gently() function shortly after
starting, and when it detects that the current working directory is
within a work tree, it changes the working directory to that root of
that work tree.

Since we only need to change the working directory once, we revise the
newSingleCheckout() function so it attempts to do this if a working
tree was detected and it has therefore set the "hasWorkTree" flag to
"true".  If the Chdir() function from the "os" package in the Go
standard library returns an error, the newSingleCheckout() function
reports the error and sets the "hasWorkTree" flag to "false" so that
the Run() method will always return immediately and never try to read
or write any files.

Note that when the Chdir() function returns an error, we explicitly
do not cause the current Git LFS command to exit, because we want
the command to continue even if it is unable to read or write files
in the current working tree.  In the case of the "git lfs checkout"
command, the command may have been invoked with the --to option, in
which case it should write its output to the file specified by the
user rather than into a Git LFS file within the working tree.  In the
case of the "git lfs pull" command, the command should try to fetch
any Git LFS objects that not present in the local storage directories
even if their contents can not be written into files in the working
tree.  In either case, we do not want the newSingleCheckout() function
to cause the commands to exit prematurely, even if an error occurs.

Also note that we do not need to keep a record of the original current
working directory and avoid deleting that directory, because a change
we made in a previous commit to the DecodePointerFromFile() function
ensures that we detect whether the file path passed to the Run() method
is a directory, and if so, returns an error.  Therefore, if the user
has created a directory in place of a Git LFS file, and set that
directory as the working directory, we will not remove it when trying
to check out that file.  The "checkout: skip changed files" and
"pull: skip changed files" tests we added in a previous commit to our
t/t-checkout.sh and t/t-pull.sh test scripts include checks which
verify this behaviour by running the respective commands from within
a directory which has replaced a Git LFS file in the working tree.

Our revisions to the newSingleCheckout() function mean that the Run()
method will only proceed if a working tree is defined and the current
working directory is the root of that tree.  One key consequence of
this change is that the method no longer need to construct a path
relative to the current working directory, as it can simply use the
path provided by Git, which is stored in the "Name" element of the
WrappedPointer structure passed to the Run() method as its sole
parameter, named "p".

As a result, the Run() method can use the "Name" element of its "p"
parameter in all in the instances where it previously used the
"cwdfilepath" variable which stored the result of the call to the
Convert() method of the repoToCurrentPathConverter structure.

Further, because the Run() method was the only caller of the Convert()
method, and the singleCheckout structure's "pathConverter" element
was the only instance of a repoToCurrentPathConverter structure in our
codebase, we can now remove that structure type and all of its methods
from the "lfs" package.

We make two alterations in this commit to the initial steps performed by
the "git lfs checkout" command so that it continues to support the use
of command-line arguments that are specified as file paths relative to
the directory in which the command is run.

First, in the checkoutCommand() function we now call the rootedPaths()
function before calling the newSingleCheckout() function, since the
newSingleCheckout() function now changes the current working directory.
By calling the rootedPaths() function first, it can convert any file
path pattern arguments provided by the user that are relative to the
initial working directory into path patterns relative to the root
of the repository at a time before the newSingleCheckout() function
changes the current working directory.

In a prior commit we added checks to the initial "checkout" test in our
t/t-checkout.sh test script which run the "git lfs checkout" command in
a subdirectory of the working tree and pass relative path arguments like
".." and "../folder2/**", and then verify that the command updates the
appropriate files in the work tree.  These checks now serve to confirm
that our revisions to the operation of the "git lfs checkout" command in
this commit do not cause any regression in the command's support for
relative file path pattern arguments, regardless of the whether the
command is run in the root of the working tree or in one of its
subdirectories.

Second, if the --to option is specified, we invoke the Abs() function
from the "path/filepath" package on its argument to generate an absolute
path, which we then pass to the RunToPath() method of the singleCheckout
structure as its "path" parameter, instead of passing the original
command-line argument.  This allows the newSingleCheckout() function
to change the working directory without causing problems if the user
supplies a relative path argument with the --to option.  Otherwise,
we would have to convert the provided path from one which was relative
to the original working directory into one which was relative to the
root of the working tree, which might even point outside of the work
tree since the user is free to supply a path to any location in their
system.  Given this, using an absolute path is our simplest approach
to handling the --to option's argument.

The checks we added in a prior commit to the "checkout: conflicts" test
in our t/t-checkout.sh test script now help verify that the "git lfs
checkout" command continues to supports the use of relative paths with
the --to option and that when this option is supplied an output file is
written to the same location as before, even if the command is run in
a subdirectory of the working tree.

In addition to the foregoing, by altering the "git lfs checkout" and
"git lfs pull" commands to change the current working directory to the
root of the work tree before they begin processing any Git LFS files,
we gain one further benefit with regard to how we handle Git LFS pointer
extension programs.  If such programs are configured, we invoke them
while performing "clean" and "smudge" operations, including the "smudge"
operations initiated by the SmudgeToFile() method when it is invoked by
the "git lfs checkout" and "git lfs pull" commands.

We first introduced support for pointer extension programs in PR #486,
at which time we modelled their configuration on that of Git's own
"clean" and "smudge" filters.  In particular, Git provides filter
programs with the path to the file they are processing in place of any
"%f" specifiers in the command lines specified by the "filter.*.clean"
and "filter.*.smudge" configuration entries.  For long-running filter
programs configured using "filter.*.process" entries, Git sends the path
to each file they process as the value of a "pathname" key in the stream
of data piped to the programs, using the protocol designed for these
types of filter programs.

In all cases, the file paths provided by Git are relative to the root
of the repository, not to the user's current working directory at the time
the initial Git command was started.  Moreover, Git changes the current
working directory to the root of the working tree before invoking any
filter processes, so the file paths it passes to the processes correspond
with the files Git will read or write in the working tree.  However,
the gitattributes(5) manual page notes that files may not actually exist
at these file paths, or may have different contents than the ones Git
pipes to the filter process, and so filter programs should not attempt
to access files at these paths:

  https://github.com/git/git/blob/v2.50.1/Documentation/gitattributes.adoc?plain=1#L503-L507

The Smudge() method of the GitFilter structure in our "lfs" package is
used by both of our "git lfs smudge" and "git lfs filter-process"
commands, and is responsible for writing the contents of a Git LFS object
as a data stream to its "writer" parameter.  This output data is then
piped back to the Git process which executed the Git LFS filter command.
In such a context, the "workingfile" parameter of the Smudge() process
contains a file path provided by Git, either in place of a "%f"
command-line specifier or as the value of a "pathname" key, per the
long-running filter protocol.

As the Git documentation states, files may not exist at these file
paths, or may have different content than the filter would expect, so
our Smudge() method is careful to only use the file paths passed to it
in its "workingfile" parameter for informational and error logging
purposes.  Likewise, all the methods and functions to which the Smudge()
method passes this parameter also only use it for logging purposes,
or at least that is our intention.

One particular use of this "workingfile" parameter's value pertains to
our support for Git LFS pointer extensions.  Like Git, the Git LFS client
will pass a file path in place of a "%f" command-line specifier if one
is found in the configuration setting for a pointer extension program.
(The actual contents of the pointer file, however, will be piped to the
extension program on its standard input file descriptor.)

When our Smudge() method invokes the readLocalFile() method of the
GitFilter structure, it passes its "workingfile" parameter.  If Git
has supplied this path to a "git lfs smudge" or "git lfs filter-process"
command, the path will be relative to the root of the repository.  Should
any Git LFS pointer extensions be configured, the readLocalFile() method
will use its "workingfile" parameter to populate the "fileName" elements
of new "pipeRequest" structures, which are then passed one at a time to
the pipeExtensions() function.  That function executes the given
extension program and substitutes the "%f" specifier in the program's
configured command line with the value from the "fileName" element
of the "pipeRequest" structure.

When the Git LFS client is not run by Git as a filter program but
executed directly via the "git lfs checkout" or "git lfs pull" commands,
however, we previously did not change the current working directory
before invoking pointer extension programs.  We also substituted for
the "%f" specifier file paths that were relative to the current working
directory (unless an absolute file path was specified by the user as
the argument of the "git lfs checkout" command's --to option).

Like Git filter programs, Git LFS pointer extension programs should not
expect to access an actual file at the paths passed in place of the "%f"
command-line specifiers.  At present, though, we do not make this
explicit.

To confirm that our changes in this commit function as expected when
Git LFS pointer extension programs are configured, we update the
lfstest-caseinverterextension test utility we added in a prior commit
so that it now reports an error and exits if it does not find a ".git"
directory in its current working directory, which would imply it is not
executing within the top-level directory of a work tree.

We also update our "checkout: pointer extension" and "pull: pointer
extension" tests so they check that the paths received and logged by
the lfstest-caseinverterextension test utility are relative to the
root of the repository even if the "git lfs checkout" or "git lfs pull"
command is executed in a subdirectory within the working tree.

However, we update our "checkout: pointer extension with conflict"
test so that it checks that the paths received and logged by the
lfstest-caseinverterextension test utility are absolute paths, because
now always convert the file path argument of the "git lfs checkout"
command's --to option into an absolute path before passing it to
the RunToPath() method.  This is the only use case in which the
RunToPath() method is invoked directly and not by the Run() method,
and thus the only instance in which the file paths of the RunToPath()
method's "path" parameter does not correspond in any way with the
file path of the given Git LFS pointer file.  This exceptional
behaviour dates from the introduction of the --to option in commit
cf7f967 of PR #3296, and we will
address this issue in a subsequent PR.
chrisd8088 added a commit that referenced this pull request Oct 16, 2025
Our "git lfs checkout" and "git lfs pull" commands retrieve a list of
Git LFS pointer files from the ScanLFSFiles() method of the GitScanner
structure type in our "lfs" package, and for each file, invoke the Run()
method of the singleCheckout structure type in our "commands" package.

For a given Git LFS pointer file, the Run() method determines whether
or not to write the contents of the object referenced by the pointer
into a file in the working tree at the appropriate path.

Because the user may execute the "git lfs checkout" and "git lfs pull"
commands from any location within a Git repository, the Run() method
tries to convert the file path of pointer, which is always relative
to the root of the repository, into a path relative to the current
working directory.  To do this, it calls the Convert() method of the
repoToCurrentPathConverter structure type in our "lfs" package, which
first prepends the absolute path to the root of the current working
tree, and then generates a relative path to that location from the
current working directory.

The repoToCurrentPathConverter structure and its methods were refactored
in commit 68efd05 of PR #1771 from the
original ConvertRepoFilesRelativeToCwd() function.  That function was
added in commit 760c7d7 of PR #527,
the same PR which introduced the "git lfs checkout" and "git lfs pull"
commands.

After calling the Convert() method to generate a path relative to the
current working directory, the Run() method passes that path to
several other functions and methods, while also using the original
input path (the one relative to the root of the repository) in other
function calls and error messages.

Because the Convert() method assumes that a current working tree is
defined (and that the current working directory is within this tree),
it will return invalid paths when these conditions are not true, such
as when the user is working in a bare repository.  In a prior commit
we therefore added a check to the Run() method so that it will not
execute the Convert() method when the no working tree is defined, which
resolved a bug whereby under unusual conditions the "git lfs checkout"
and "git lfs pull" commands could write to a file outside a bare
repository.  (In another prior commit we then also updated the
"git lfs checkout" command so that it will exit immediately when run
in a bare repository.)

The Run() method now checks the state of a "hasWorkTree" element in the
singleCheckout structure and returns without taking further action if
the element's value is "false".  When we initialize a new singleCheckout
structure in the newSingleCheckout() function of our "commands" package,
we set the value of the "hasWorkTree" element to "true" only if the
the LocalWorkingDir() method of the Configuration structure type from
our "config" package returns a non-empty path.

The LocalWorkingDir() method returns the absolute path to the root of
the current working tree, or an empty path if no working tree is defined,
as determined by the GitAndRootDirs() function in our "git" package.
The GitAndRootDirs() function runs the "git rev-parse" command with the
--show-toplevel option, and then interprets that command's output and
exit code so that if no working tree is defined, an empty path is
returned instead of a path to the work tree's root directory.

If a working tree exists and so the "hasWorkTree" element is "true",
the Run() method will proceed to invoke the Convert() method and then
pass the resultant path, which is relative to the current working
directory, to the DecodePointerFromFile() function from our "lfs"
package, and then to the RunToPath() method of the singleCheckout
structure, which passes it to the SmudgeToFile() method of the GitFilter
structure in our "lfs" package.  The Run() method later also passes the
path to the Add() method of the gitIndexer structure in our "commands"
package, which writes the path to a "git update-index" command on its
standard input file descriptor.

In a prior commit we updated the SmudgeToFile() method so that it
always creates a new file, rather than writing Git LFS object data into
an existing file, which ensures that the method will not write through
a symbolic link which exists in the place of the final filename component
of a given Git LFS pointer's file path.

In subsequent commits we will next revise the Run() method and add new
methods for the singleCheckout structure so that we check each ancestor
component of a Git LFS pointer's file path to verify that none of the
directory components of the path are symbolic links.  If a symbolic link
is found, we will report it in a new error message log format, and the
RunToPath() method will then not be invoked, nor will the gitIndexer
structure's Add() method.

With our current design, performing these checks for symbolic links,
which must be made on each path component from the root of the current
working tree to the parent directory of a given file, is complicated
by the fact that the current working directory may be located anywhere
within the work tree.  We either have to prepend zero or more ".."
path components to reach the root of the working tree, or construct an
absolute path to the root of the tree and then prepend that path to
each Git LFS pointer's file path within the repository.

To simplify both the future implementation of our checks for symbolic
links in file paths and the overall design of the Run() method, we
first adopt the approach taken by Git, which is to change the current
working directory to the root of the working tree, if one exists, before
checking for symbolic links and creating files in the work tree:

  https://github.com/git/git/blob/v2.50.1/setup.c#L1759-L1760
  https://github.com/git/git/blob/v2.50.1/symlinks.c#L63-L193

Git runs its setup_git_directory_gently() function shortly after
starting, and when it detects that the current working directory is
within a work tree, it changes the working directory to that root of
that work tree.

Since we only need to change the working directory once, we revise the
newSingleCheckout() function so it attempts to do this if a working
tree was detected and it has therefore set the "hasWorkTree" flag to
"true".  If the Chdir() function from the "os" package in the Go
standard library returns an error, the newSingleCheckout() function
reports the error and sets the "hasWorkTree" flag to "false" so that
the Run() method will always return immediately and never try to read
or write any files.

Note that when the Chdir() function returns an error, we explicitly
do not cause the current Git LFS command to exit, because we want
the command to continue even if it is unable to read or write files
in the current working tree.  In the case of the "git lfs checkout"
command, the command may have been invoked with the --to option, in
which case it should write its output to the file specified by the
user rather than into a Git LFS file within the working tree.  In the
case of the "git lfs pull" command, the command should try to fetch
any Git LFS objects that not present in the local storage directories
even if their contents can not be written into files in the working
tree.  In either case, we do not want the newSingleCheckout() function
to cause the commands to exit prematurely, even if an error occurs.

Also note that we do not need to keep a record of the original current
working directory and avoid deleting that directory, because a change
we made in a previous commit to the DecodePointerFromFile() function
ensures that we detect whether the file path passed to the Run() method
is a directory, and if so, returns an error.  Therefore, if the user
has created a directory in place of a Git LFS file, and set that
directory as the working directory, we will not remove it when trying
to check out that file.  The "checkout: skip changed files" and
"pull: skip changed files" tests we added in a previous commit to our
t/t-checkout.sh and t/t-pull.sh test scripts include checks which
verify this behaviour by running the respective commands from within
a directory which has replaced a Git LFS file in the working tree.

Our revisions to the newSingleCheckout() function mean that the Run()
method will only proceed if a working tree is defined and the current
working directory is the root of that tree.  One key consequence of
this change is that the method no longer need to construct a path
relative to the current working directory, as it can simply use the
path provided by Git, which is stored in the "Name" element of the
WrappedPointer structure passed to the Run() method as its sole
parameter, named "p".

As a result, the Run() method can use the "Name" element of its "p"
parameter in all in the instances where it previously used the
"cwdfilepath" variable which stored the result of the call to the
Convert() method of the repoToCurrentPathConverter structure.

Further, because the Run() method was the only caller of the Convert()
method, and the singleCheckout structure's "pathConverter" element
was the only instance of a repoToCurrentPathConverter structure in our
codebase, we can now remove that structure type and all of its methods
from the "lfs" package.

We make two alterations in this commit to the initial steps performed by
the "git lfs checkout" command so that it continues to support the use
of command-line arguments that are specified as file paths relative to
the directory in which the command is run.

First, in the checkoutCommand() function we now call the rootedPaths()
function before calling the newSingleCheckout() function, since the
newSingleCheckout() function now changes the current working directory.
By calling the rootedPaths() function first, it can convert any file
path pattern arguments provided by the user that are relative to the
initial working directory into path patterns relative to the root
of the repository at a time before the newSingleCheckout() function
changes the current working directory.

In a prior commit we added checks to the initial "checkout" test in our
t/t-checkout.sh test script which run the "git lfs checkout" command in
a subdirectory of the working tree and pass relative path arguments like
".." and "../folder2/**", and then verify that the command updates the
appropriate files in the work tree.  These checks now serve to confirm
that our revisions to the operation of the "git lfs checkout" command in
this commit do not cause any regression in the command's support for
relative file path pattern arguments, regardless of the whether the
command is run in the root of the working tree or in one of its
subdirectories.

Second, if the --to option is specified, we invoke the Abs() function
from the "path/filepath" package on its argument to generate an absolute
path, which we then pass to the RunToPath() method of the singleCheckout
structure as its "path" parameter, instead of passing the original
command-line argument.  This allows the newSingleCheckout() function
to change the working directory without causing problems if the user
supplies a relative path argument with the --to option.  Otherwise,
we would have to convert the provided path from one which was relative
to the original working directory into one which was relative to the
root of the working tree, which might even point outside of the work
tree since the user is free to supply a path to any location in their
system.  Given this, using an absolute path is our simplest approach
to handling the --to option's argument.

The checks we added in a prior commit to the "checkout: conflicts" test
in our t/t-checkout.sh test script now help verify that the "git lfs
checkout" command continues to supports the use of relative paths with
the --to option and that when this option is supplied an output file is
written to the same location as before, even if the command is run in
a subdirectory of the working tree.

In addition to the foregoing, by altering the "git lfs checkout" and
"git lfs pull" commands to change the current working directory to the
root of the work tree before they begin processing any Git LFS files,
we gain one further benefit with regard to how we handle Git LFS pointer
extension programs.  If such programs are configured, we invoke them
while performing "clean" and "smudge" operations, including the "smudge"
operations initiated by the SmudgeToFile() method when it is invoked by
the "git lfs checkout" and "git lfs pull" commands.

We first introduced support for pointer extension programs in PR #486,
at which time we modelled their configuration on that of Git's own
"clean" and "smudge" filters.  In particular, Git provides filter
programs with the path to the file they are processing in place of any
"%f" specifiers in the command lines specified by the "filter.*.clean"
and "filter.*.smudge" configuration entries.  For long-running filter
programs configured using "filter.*.process" entries, Git sends the path
to each file they process as the value of a "pathname" key in the stream
of data piped to the programs, using the protocol designed for these
types of filter programs.

In all cases, the file paths provided by Git are relative to the root
of the repository, not to the user's current working directory at the time
the initial Git command was started.  Moreover, Git changes the current
working directory to the root of the working tree before invoking any
filter processes, so the file paths it passes to the processes correspond
with the files Git will read or write in the working tree.  However,
the gitattributes(5) manual page notes that files may not actually exist
at these file paths, or may have different contents than the ones Git
pipes to the filter process, and so filter programs should not attempt
to access files at these paths:

  https://github.com/git/git/blob/v2.50.1/Documentation/gitattributes.adoc?plain=1#L503-L507

The Smudge() method of the GitFilter structure in our "lfs" package is
used by both of our "git lfs smudge" and "git lfs filter-process"
commands, and is responsible for writing the contents of a Git LFS object
as a data stream to its "writer" parameter.  This output data is then
piped back to the Git process which executed the Git LFS filter command.
In such a context, the "workingfile" parameter of the Smudge() process
contains a file path provided by Git, either in place of a "%f"
command-line specifier or as the value of a "pathname" key, per the
long-running filter protocol.

As the Git documentation states, files may not exist at these file
paths, or may have different content than the filter would expect, so
our Smudge() method is careful to only use the file paths passed to it
in its "workingfile" parameter for informational and error logging
purposes.  Likewise, all the methods and functions to which the Smudge()
method passes this parameter also only use it for logging purposes,
or at least that is our intention.

One particular use of this "workingfile" parameter's value pertains to
our support for Git LFS pointer extensions.  Like Git, the Git LFS client
will pass a file path in place of a "%f" command-line specifier if one
is found in the configuration setting for a pointer extension program.
(The actual contents of the pointer file, however, will be piped to the
extension program on its standard input file descriptor.)

When our Smudge() method invokes the readLocalFile() method of the
GitFilter structure, it passes its "workingfile" parameter.  If Git
has supplied this path to a "git lfs smudge" or "git lfs filter-process"
command, the path will be relative to the root of the repository.  Should
any Git LFS pointer extensions be configured, the readLocalFile() method
will use its "workingfile" parameter to populate the "fileName" elements
of new "pipeRequest" structures, which are then passed one at a time to
the pipeExtensions() function.  That function executes the given
extension program and substitutes the "%f" specifier in the program's
configured command line with the value from the "fileName" element
of the "pipeRequest" structure.

When the Git LFS client is not run by Git as a filter program but
executed directly via the "git lfs checkout" or "git lfs pull" commands,
however, we previously did not change the current working directory
before invoking pointer extension programs.  We also substituted for
the "%f" specifier file paths that were relative to the current working
directory (unless an absolute file path was specified by the user as
the argument of the "git lfs checkout" command's --to option).

Like Git filter programs, Git LFS pointer extension programs should not
expect to access an actual file at the paths passed in place of the "%f"
command-line specifiers.  At present, though, we do not make this
explicit.

To confirm that our changes in this commit function as expected when
Git LFS pointer extension programs are configured, we update the
lfstest-caseinverterextension test utility we added in a prior commit
so that it now reports an error and exits if it does not find a ".git"
directory in its current working directory, which would imply it is not
executing within the top-level directory of a work tree.

We also update our "checkout: pointer extension" and "pull: pointer
extension" tests so they check that the paths received and logged by
the lfstest-caseinverterextension test utility are relative to the
root of the repository even if the "git lfs checkout" or "git lfs pull"
command is executed in a subdirectory within the working tree.

However, we update our "checkout: pointer extension with conflict"
test so that it checks that the paths received and logged by the
lfstest-caseinverterextension test utility are absolute paths, because
now always convert the file path argument of the "git lfs checkout"
command's --to option into an absolute path before passing it to
the RunToPath() method.  This is the only use case in which the
RunToPath() method is invoked directly and not by the Run() method,
and thus the only instance in which the file paths of the RunToPath()
method's "path" parameter does not correspond in any way with the
file path of the given Git LFS pointer file.  This exceptional
behaviour dates from the introduction of the --to option in commit
cf7f967 of PR #3296, and we will
address this issue in a subsequent PR.
hswong3i pushed a commit to alvistack/git-lfs-git-lfs that referenced this pull request Oct 18, 2025
In PR git-lfs#486 we introduced support for Git LFS pointer extensions, along
with some related tests in our Go test suite and the t/t-ext.sh shell
test script, which validates that pointer extensions are reported as
expected by the "git lfs ext" command.  (Note that Git LFS pointer
extension support is still technically considered experimental,
according to our documentation.)

However, we do not have any tests which exercise a complete Git LFS
pointer extension configuration including an extension program which
transforms the content of files tracked as Git LFS objects.

We therefore add a new lfstest-caseinverterextension test utility
program and make use of it in a set of shell tests of our "git lfs clean",
"git lfs smudge", and "git lfs filter-process" commands, as well as
in tests of our "git lfs checkout" and "git lfs pull" commands.  All
of these commands execute the lfstest-caseinverterextension utility
and confirm that it runs as expected.

Note that at present, when we execute a pointer extension program within
one of our "git lfs clean", "git lfs smudge", and "git lfs filter-process"
commands, our commands have typically inherited their execution
environment from Git.  When Git runs one of these commands to perform
a "clean" or "smudge" filter operation, it has already changed the current
working directory to be the root of the repository's working tree.  The
setup_git_directory_gently() function in Git is normally run shortly
after starting, and if it detects that the current working directory
is within a Git work tree, it changes the current working directory to
the root of that work tree:

  https://github.com/git/git/blob/v2.50.1/setup.c#L1758-L1760

The file paths Git then passes to our filter commands are always relative
to the root of the repository.  Git passes these file paths in place of
the "%f" command-line specifier from the filter configuration options, and
for long-running filter processes such as our "git lfs filter-process"
command, Git passes an equivalent file path as the value of the "pathname"
keys it sends to the filter process.  The gitattributes(5) manual page
notes that files may not actually exist at these file paths, or may have
different contents than the ones Git pipes to the filter process, and so
filter programs should not attempt to access files at these paths:

  https://github.com/git/git/blob/v2.50.1/Documentation/gitattributes.adoc?plain=1#L503-L507

Likewise, when the Git LFS client invokes a Git LFS pointer extension
program, it passes a file path in place of any "%f" command-line
specifier in the extension configuration options, while piping the
actual file contents to the program on the standard input file
descriptor.  If the client itself has been run by Git as a filter program,
then the file path will be relative to the root of the repository, and
the current working directory will be the root of the repository's
working tree.

When the Git LFS client is not run by Git as a filter program but executed
directly via the "git lfs checkout" or "git lfs pull" commands, however,
we do not change the current working directory before invoking pointer
extension programs, and pass file paths that are relative to the current
working directory rather than the repository root.  One exception is
when the --to option of the "git lfs checkout" command is specified,
in which case we pass the file path argument of that option to any
pointer extension programs instead of a path to the pointer file whose
contents they are processing.

Like Git filter programs, Git LFS pointer extension program should not
expect to access an actual file at the paths passed in place of the "%f"
command-line specifiers.  At present, though, we do not make this explicit
in our documentation.

In our new tests of the pointer extension support of our "git lfs checkout"
and "git lfs pull" commands, we specifically check that the file paths
logged by the lfstest-caseinverterextension test utility are relative
to the current working directory, except when the --to option of the
"git lfs checkout" command is specified, in which case that option's
argument is passed to the test utility.  We include comments noting that
both of these behaviours differ from that seen when an extension program
is run by a Git LFS filter command executed by Git.

In subsequent commits we expect to adjust how our "git lfs checkout" and
"git lfs pull" commands handle file paths, including by changing the
current working directory to the root of the current working tree before
writing files into that work tree.  As a consequence, these commands will
pass only paths relative to the root of the repository to the
SmudgeToFile() method of the GitFilter structure in our "lfs" package.

As such, we will also necessarily change the file paths passed to Git LFS
pointer extensions so they are also always relative to the root of the
repository, and when we invoke pointer extension programs, the current
working directory will already be set to the root of the current work
tree.

At the same time we will then revise our "checkout: pointer extension",
"checkout: pointer extension with conflict", and "pull: pointer extension"
tests so they verify that the file paths received by the
lfstest-caseinverterextension test utility are relative to the root of
the repository, and will update the utility itself so it checks that
its current working directory is the root of a Git work tree.

Co-authored-by: Lars Schneider <[email protected]>
hswong3i pushed a commit to alvistack/git-lfs-git-lfs that referenced this pull request Oct 18, 2025
Our "git lfs checkout" and "git lfs pull" commands retrieve a list of
Git LFS pointer files from the ScanLFSFiles() method of the GitScanner
structure type in our "lfs" package, and for each file, invoke the Run()
method of the singleCheckout structure type in our "commands" package.

For a given Git LFS pointer file, the Run() method determines whether
or not to write the contents of the object referenced by the pointer
into a file in the working tree at the appropriate path.

Because the user may execute the "git lfs checkout" and "git lfs pull"
commands from any location within a Git repository, the Run() method
tries to convert the file path of pointer, which is always relative
to the root of the repository, into a path relative to the current
working directory.  To do this, it calls the Convert() method of the
repoToCurrentPathConverter structure type in our "lfs" package, which
first prepends the absolute path to the root of the current working
tree, and then generates a relative path to that location from the
current working directory.

The repoToCurrentPathConverter structure and its methods were refactored
in commit 68efd05 of PR git-lfs#1771 from the
original ConvertRepoFilesRelativeToCwd() function.  That function was
added in commit 760c7d7 of PR git-lfs#527,
the same PR which introduced the "git lfs checkout" and "git lfs pull"
commands.

After calling the Convert() method to generate a path relative to the
current working directory, the Run() method passes that path to
several other functions and methods, while also using the original
input path (the one relative to the root of the repository) in other
function calls and error messages.

Because the Convert() method assumes that a current working tree is
defined (and that the current working directory is within this tree),
it will return invalid paths when these conditions are not true, such
as when the user is working in a bare repository.  In a prior commit
we therefore added a check to the Run() method so that it will not
execute the Convert() method when the no working tree is defined, which
resolved a bug whereby under unusual conditions the "git lfs checkout"
and "git lfs pull" commands could write to a file outside a bare
repository.  (In another prior commit we then also updated the
"git lfs checkout" command so that it will exit immediately when run
in a bare repository.)

The Run() method now checks the state of a "hasWorkTree" element in the
singleCheckout structure and returns without taking further action if
the element's value is "false".  When we initialize a new singleCheckout
structure in the newSingleCheckout() function of our "commands" package,
we set the value of the "hasWorkTree" element to "true" only if the
the LocalWorkingDir() method of the Configuration structure type from
our "config" package returns a non-empty path.

The LocalWorkingDir() method returns the absolute path to the root of
the current working tree, or an empty path if no working tree is defined,
as determined by the GitAndRootDirs() function in our "git" package.
The GitAndRootDirs() function runs the "git rev-parse" command with the
--show-toplevel option, and then interprets that command's output and
exit code so that if no working tree is defined, an empty path is
returned instead of a path to the work tree's root directory.

If a working tree exists and so the "hasWorkTree" element is "true",
the Run() method will proceed to invoke the Convert() method and then
pass the resultant path, which is relative to the current working
directory, to the DecodePointerFromFile() function from our "lfs"
package, and then to the RunToPath() method of the singleCheckout
structure, which passes it to the SmudgeToFile() method of the GitFilter
structure in our "lfs" package.  The Run() method later also passes the
path to the Add() method of the gitIndexer structure in our "commands"
package, which writes the path to a "git update-index" command on its
standard input file descriptor.

In a prior commit we updated the SmudgeToFile() method so that it
always creates a new file, rather than writing Git LFS object data into
an existing file, which ensures that the method will not write through
a symbolic link which exists in the place of the final filename component
of a given Git LFS pointer's file path.

In subsequent commits we will next revise the Run() method and add new
methods for the singleCheckout structure so that we check each ancestor
component of a Git LFS pointer's file path to verify that none of the
directory components of the path are symbolic links.  If a symbolic link
is found, we will report it in a new error message log format, and the
RunToPath() method will then not be invoked, nor will the gitIndexer
structure's Add() method.

With our current design, performing these checks for symbolic links,
which must be made on each path component from the root of the current
working tree to the parent directory of a given file, is complicated
by the fact that the current working directory may be located anywhere
within the work tree.  We either have to prepend zero or more ".."
path components to reach the root of the working tree, or construct an
absolute path to the root of the tree and then prepend that path to
each Git LFS pointer's file path within the repository.

To simplify both the future implementation of our checks for symbolic
links in file paths and the overall design of the Run() method, we
first adopt the approach taken by Git, which is to change the current
working directory to the root of the working tree, if one exists, before
checking for symbolic links and creating files in the work tree:

  https://github.com/git/git/blob/v2.50.1/setup.c#L1759-L1760
  https://github.com/git/git/blob/v2.50.1/symlinks.c#L63-L193

Git runs its setup_git_directory_gently() function shortly after
starting, and when it detects that the current working directory is
within a work tree, it changes the working directory to that root of
that work tree.

Since we only need to change the working directory once, we revise the
newSingleCheckout() function so it attempts to do this if a working
tree was detected and it has therefore set the "hasWorkTree" flag to
"true".  If the Chdir() function from the "os" package in the Go
standard library returns an error, the newSingleCheckout() function
reports the error and sets the "hasWorkTree" flag to "false" so that
the Run() method will always return immediately and never try to read
or write any files.

Note that when the Chdir() function returns an error, we explicitly
do not cause the current Git LFS command to exit, because we want
the command to continue even if it is unable to read or write files
in the current working tree.  In the case of the "git lfs checkout"
command, the command may have been invoked with the --to option, in
which case it should write its output to the file specified by the
user rather than into a Git LFS file within the working tree.  In the
case of the "git lfs pull" command, the command should try to fetch
any Git LFS objects that not present in the local storage directories
even if their contents can not be written into files in the working
tree.  In either case, we do not want the newSingleCheckout() function
to cause the commands to exit prematurely, even if an error occurs.

Also note that we do not need to keep a record of the original current
working directory and avoid deleting that directory, because a change
we made in a previous commit to the DecodePointerFromFile() function
ensures that we detect whether the file path passed to the Run() method
is a directory, and if so, returns an error.  Therefore, if the user
has created a directory in place of a Git LFS file, and set that
directory as the working directory, we will not remove it when trying
to check out that file.  The "checkout: skip changed files" and
"pull: skip changed files" tests we added in a previous commit to our
t/t-checkout.sh and t/t-pull.sh test scripts include checks which
verify this behaviour by running the respective commands from within
a directory which has replaced a Git LFS file in the working tree.

Our revisions to the newSingleCheckout() function mean that the Run()
method will only proceed if a working tree is defined and the current
working directory is the root of that tree.  One key consequence of
this change is that the method no longer need to construct a path
relative to the current working directory, as it can simply use the
path provided by Git, which is stored in the "Name" element of the
WrappedPointer structure passed to the Run() method as its sole
parameter, named "p".

As a result, the Run() method can use the "Name" element of its "p"
parameter in all in the instances where it previously used the
"cwdfilepath" variable which stored the result of the call to the
Convert() method of the repoToCurrentPathConverter structure.

Further, because the Run() method was the only caller of the Convert()
method, and the singleCheckout structure's "pathConverter" element
was the only instance of a repoToCurrentPathConverter structure in our
codebase, we can now remove that structure type and all of its methods
from the "lfs" package.

We make two alterations in this commit to the initial steps performed by
the "git lfs checkout" command so that it continues to support the use
of command-line arguments that are specified as file paths relative to
the directory in which the command is run.

First, in the checkoutCommand() function we now call the rootedPaths()
function before calling the newSingleCheckout() function, since the
newSingleCheckout() function now changes the current working directory.
By calling the rootedPaths() function first, it can convert any file
path pattern arguments provided by the user that are relative to the
initial working directory into path patterns relative to the root
of the repository at a time before the newSingleCheckout() function
changes the current working directory.

In a prior commit we added checks to the initial "checkout" test in our
t/t-checkout.sh test script which run the "git lfs checkout" command in
a subdirectory of the working tree and pass relative path arguments like
".." and "../folder2/**", and then verify that the command updates the
appropriate files in the work tree.  These checks now serve to confirm
that our revisions to the operation of the "git lfs checkout" command in
this commit do not cause any regression in the command's support for
relative file path pattern arguments, regardless of the whether the
command is run in the root of the working tree or in one of its
subdirectories.

Second, if the --to option is specified, we invoke the Abs() function
from the "path/filepath" package on its argument to generate an absolute
path, which we then pass to the RunToPath() method of the singleCheckout
structure as its "path" parameter, instead of passing the original
command-line argument.  This allows the newSingleCheckout() function
to change the working directory without causing problems if the user
supplies a relative path argument with the --to option.  Otherwise,
we would have to convert the provided path from one which was relative
to the original working directory into one which was relative to the
root of the working tree, which might even point outside of the work
tree since the user is free to supply a path to any location in their
system.  Given this, using an absolute path is our simplest approach
to handling the --to option's argument.

The checks we added in a prior commit to the "checkout: conflicts" test
in our t/t-checkout.sh test script now help verify that the "git lfs
checkout" command continues to supports the use of relative paths with
the --to option and that when this option is supplied an output file is
written to the same location as before, even if the command is run in
a subdirectory of the working tree.

In addition to the foregoing, by altering the "git lfs checkout" and
"git lfs pull" commands to change the current working directory to the
root of the work tree before they begin processing any Git LFS files,
we gain one further benefit with regard to how we handle Git LFS pointer
extension programs.  If such programs are configured, we invoke them
while performing "clean" and "smudge" operations, including the "smudge"
operations initiated by the SmudgeToFile() method when it is invoked by
the "git lfs checkout" and "git lfs pull" commands.

We first introduced support for pointer extension programs in PR git-lfs#486,
at which time we modelled their configuration on that of Git's own
"clean" and "smudge" filters.  In particular, Git provides filter
programs with the path to the file they are processing in place of any
"%f" specifiers in the command lines specified by the "filter.*.clean"
and "filter.*.smudge" configuration entries.  For long-running filter
programs configured using "filter.*.process" entries, Git sends the path
to each file they process as the value of a "pathname" key in the stream
of data piped to the programs, using the protocol designed for these
types of filter programs.

In all cases, the file paths provided by Git are relative to the root
of the repository, not to the user's current working directory at the time
the initial Git command was started.  Moreover, Git changes the current
working directory to the root of the working tree before invoking any
filter processes, so the file paths it passes to the processes correspond
with the files Git will read or write in the working tree.  However,
the gitattributes(5) manual page notes that files may not actually exist
at these file paths, or may have different contents than the ones Git
pipes to the filter process, and so filter programs should not attempt
to access files at these paths:

  https://github.com/git/git/blob/v2.50.1/Documentation/gitattributes.adoc?plain=1#L503-L507

The Smudge() method of the GitFilter structure in our "lfs" package is
used by both of our "git lfs smudge" and "git lfs filter-process"
commands, and is responsible for writing the contents of a Git LFS object
as a data stream to its "writer" parameter.  This output data is then
piped back to the Git process which executed the Git LFS filter command.
In such a context, the "workingfile" parameter of the Smudge() process
contains a file path provided by Git, either in place of a "%f"
command-line specifier or as the value of a "pathname" key, per the
long-running filter protocol.

As the Git documentation states, files may not exist at these file
paths, or may have different content than the filter would expect, so
our Smudge() method is careful to only use the file paths passed to it
in its "workingfile" parameter for informational and error logging
purposes.  Likewise, all the methods and functions to which the Smudge()
method passes this parameter also only use it for logging purposes,
or at least that is our intention.

One particular use of this "workingfile" parameter's value pertains to
our support for Git LFS pointer extensions.  Like Git, the Git LFS client
will pass a file path in place of a "%f" command-line specifier if one
is found in the configuration setting for a pointer extension program.
(The actual contents of the pointer file, however, will be piped to the
extension program on its standard input file descriptor.)

When our Smudge() method invokes the readLocalFile() method of the
GitFilter structure, it passes its "workingfile" parameter.  If Git
has supplied this path to a "git lfs smudge" or "git lfs filter-process"
command, the path will be relative to the root of the repository.  Should
any Git LFS pointer extensions be configured, the readLocalFile() method
will use its "workingfile" parameter to populate the "fileName" elements
of new "pipeRequest" structures, which are then passed one at a time to
the pipeExtensions() function.  That function executes the given
extension program and substitutes the "%f" specifier in the program's
configured command line with the value from the "fileName" element
of the "pipeRequest" structure.

When the Git LFS client is not run by Git as a filter program but
executed directly via the "git lfs checkout" or "git lfs pull" commands,
however, we previously did not change the current working directory
before invoking pointer extension programs.  We also substituted for
the "%f" specifier file paths that were relative to the current working
directory (unless an absolute file path was specified by the user as
the argument of the "git lfs checkout" command's --to option).

Like Git filter programs, Git LFS pointer extension programs should not
expect to access an actual file at the paths passed in place of the "%f"
command-line specifiers.  At present, though, we do not make this
explicit.

To confirm that our changes in this commit function as expected when
Git LFS pointer extension programs are configured, we update the
lfstest-caseinverterextension test utility we added in a prior commit
so that it now reports an error and exits if it does not find a ".git"
directory in its current working directory, which would imply it is not
executing within the top-level directory of a work tree.

We also update our "checkout: pointer extension" and "pull: pointer
extension" tests so they check that the paths received and logged by
the lfstest-caseinverterextension test utility are relative to the
root of the repository even if the "git lfs checkout" or "git lfs pull"
command is executed in a subdirectory within the working tree.

However, we update our "checkout: pointer extension with conflict"
test so that it checks that the paths received and logged by the
lfstest-caseinverterextension test utility are absolute paths, because
now always convert the file path argument of the "git lfs checkout"
command's --to option into an absolute path before passing it to
the RunToPath() method.  This is the only use case in which the
RunToPath() method is invoked directly and not by the Run() method,
and thus the only instance in which the file paths of the RunToPath()
method's "path" parameter does not correspond in any way with the
file path of the given Git LFS pointer file.  This exceptional
behaviour dates from the introduction of the --to option in commit
cf7f967 of PR git-lfs#3296, and we will
address this issue in a subsequent PR.
chrisd8088 added a commit that referenced this pull request Oct 22, 2026
Since commit 9f4a8e2, the
Git LFS client has read Git configuration data by splitting the
output of "git config -l" commands on each line feed (LF) character
and then splitting each line on the first equals sign ("=")
character to determine an option's name and value.

However, Git configuration option values may contain line feed
characters, and so the output of a "git config -l" command is
ambiguous unless the -z option is also specified.  Without that
option, a line feed character in an option's value is
indistinguishable from the line feed character which separates
one option from the next.  As a consequence, the Git LFS client
may interpret portions of an option's value as separate
configuration options.

This behaviour is of particular concern because some Git LFS
configuration options specify local programs which the client
should execute.  In particular, custom transfer adapter programs
may be configured with the "lfs.customTransfer.<name>.path"
option, while pointer extension programs may be configured with
the "lfs.extension.<name>.clean" and "lfs.extension.<name>.smudge"
options.

(We added support for custom transfer adapter configurations in
commit 357703e of PR #1367.
Prior to that, we added support for pointer extension filter
configurations in several commits of PR #486, including commits
ffd52e6 and
6a3a2c3.)

For security reasons, we do not accept options which specify
custom transfer adapter or pointer extension programs from the
".lfsconfig" files that may be supplied in a repository.

However, a repository may supply a ".gitmodules" file containing
Git submodule configuration options, and when a user runs the
"git submodule init" command, Git copies some of these options into
the repository's local Git configuration file.  In particular,
the "submodule.<name>.url" option will be copied into the local
configuration file.

If the URL provided in a "submodule.<name>.url" option contains
a line feed character, then Git LFS will interpret any trailing
portion of the URL as a separate option, and may thus be induced
to run a potentially malicious executable.  This vulnerability
has been assigned the identifier CVE-2026-105802.

To resolve this problem, we add the -z option to the "git config -l"
commands run by the methods of the Configuration structure in our
"git" package which read Git configuration settings.

With the -z option, Git terminates each configuration entry with
a null (NUL) character and separates its name from its value with a
line feed character.  We therefore revise the ParseConfigEntries()
function in our "git" package so that it splits its input on
NUL characters and then splits each entry on its first line feed
character.

To confirm that our changes are effective, we update the Go tests
we expanded in a prior commit to use NUL-delimited configuration
input and check the parsed entry structures.  In our "git"
package, the TestParseConfigEntries() function now checks that
values containing line feed characters, including those followed
by equals sign characters, are retained without being split into
additional entries.

In our "config" package, we add a test case to the
TestReadGitConfigKeys() function to verify that a line feed
character is preserved in an option's value in both unrestricted
and restricted configuration sources.

We then add a shell integration test which attempts to inject an
"lfs.url" option in the value of an "lfs.fetchinclude" option in
an ".lfsconfig" file.  The test checks that the "git lfs env"
command reports the endpoint from the actual "lfs.url" option
rather than the injected one and does not report conflicting values
for that option.  We perform these checks with the ".lfsconfig"
file in the working tree, then with the file present only in the
index, and finally with the file available only from the HEAD
commit, so that configuration options are parsed from both file
and Git blob sources.

Finally, we add two shell tests to our "t/t-submodule-lfsconfig.sh"
script.  These tests both attempt to inject a Git LFS configuration
option into the submodule URL defined in a repository's ".gitmodules"
file.  The value of the injected option is a relative path to a
local file (with an appropriate ".exe" file extension on Windows
systems), and the repository includes a copy of our
"lfstest-exploitcheck" test utility with that name.

As we described in the prior commit in which we renamed the
"lfstest-exploitcheck" utility from its previous name of
"lfstest-badpathcheck", this program creates an empty sentinel
file named "exploit" in the current working directory.  Before
exiting, it then writes the string "exploit" to its standard
output and error file descriptors.

The first test attempts to override the default standalone custom
transfer adapter implemented in the Git LFS client, which is used
when fetching objects from a local repository.  After cloning the
repository and running the "git submodule init" command, the test
re-fetches a Git LFS object and confirms that no sentinel file has
been created, as it would be without the change in this commit to
the ParseConfigEntries() function.  Absent that change, the Git LFS
client would execute the utility to fetch objects from the source
repository, and the utility would create a sentinel file that the
test would detect.

The test then verifies that the re-fetch operation succeeded and
that the Git LFS client fetched an object without an error.  The
test also checks that the client did not output any trace log
messages which would indicate that it executed a custom transfer
adapter with the name "adapter", as that is the name we assign to
the copy of the "lfstest-exploitcheck" utility in the repository.

The second new test in the "t/t-submodule-lfsconfig.sh" script
attempts to configure a pointer extension program which would be
executed by the Git LFS "clean" filter.  After running the
"git submodule init" command, the test adds a Git LFS file to
the index and confirms that no sentinel file has been created,
as it would be without the change in this commit to the
ParseConfigEntries() function.

The test then verifies that the "git add" operation succeeded and
that a Git LFS object was created without any error.  The test also
confirms that no pointer extension line entry appears in the Git LFS
pointer file created for the object.  As well, the test checks that
the client did not output any trace log messages which would
indicate that it executed a pointer extension program with the
name "ext-clean", as that is the name we assign to the copy of the
"lfstest-exploitcheck" utility in the repository.

Finally, note that preserving line feed characters in configuration
values may make the output of the "git lfs env" command more
difficult to interpret, as that command does not quote the file
paths and URLs it reports.  However, any improvements to that
command's output fall outside of the scope of the present work
and so we defer such changes to a future PR.  Users should not
expect the output of the "git lfs env" command to have an
unambiguous format suitable for consumption by other tools.

Co-authored-by: Aniket Gupta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants