git-lfs/commands/command_update.go
brian m. carlson 75fb8f3719
commands: make sure we're in the working tree
In the normal case, Git commands perform repository autodiscovery based
on the current working directory.  However, in some cases, it's possible
to specify a Git working tree unrelated to the current working directory
by using GIT_WORK_TREE.  In such a case, we want to make sure that we
change into the working tree such that our working directory is always
within the working tree, if one exists.  This is what Git does, and it
means that when we write files into the repository, such as a
.gitattributes file, we write them into the proper place.

Note also that we adjust the code to require that the working directory
be non-empty when we require a working copy instead of that the
repository be non-bare.  That's because we don't want people to be
working inside of the Git directory in such situations, where the
repository would be non-bare but would not have a working tree.

We add tests for this case for track and untrack, which require a
working tree, and for checkout, which requires only a repository.  This
means that we can verify the behavior of the functions we've added
without needing to add tests for this case to each of the subcommands.
2020-10-14 20:58:28 +00:00

63 lines
1.5 KiB
Go

package commands
import (
"regexp"
"github.com/spf13/cobra"
)
var (
updateForce = false
updateManual = false
)
// updateCommand is used for updating parts of Git LFS that reside under
// .git/lfs.
func updateCommand(cmd *cobra.Command, args []string) {
requireGitVersion()
setupRepository()
lfsAccessRE := regexp.MustCompile(`\Alfs\.(.*)\.access\z`)
for key, _ := range cfg.Git.All() {
matches := lfsAccessRE.FindStringSubmatch(key)
if len(matches) < 2 {
continue
}
value, _ := cfg.Git.Get(key)
switch value {
case "basic":
case "private":
cfg.SetGitLocalKey(key, "basic")
Print("Updated %s access from %s to %s.", matches[1], value, "basic")
default:
cfg.UnsetGitLocalKey(key)
Print("Removed invalid %s access of %s.", matches[1], value)
}
}
if updateForce && updateManual {
Exit("You cannot use --force and --manual options together")
}
if updateManual {
Print(getHookInstallSteps())
} else {
if err := installHooks(updateForce); err != nil {
Error(err.Error())
Exit("To resolve this, either:\n 1: run `git lfs update --manual` for instructions on how to merge hooks.\n 2: run `git lfs update --force` to overwrite your hook.")
} else {
Print("Updated git hooks.")
}
}
}
func init() {
RegisterCommand("update", updateCommand, func(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&updateForce, "force", "f", false, "Overwrite existing hooks.")
cmd.Flags().BoolVarP(&updateManual, "manual", "m", false, "Print instructions for manual install.")
})
}