diff --git a/commands/command_install.go b/commands/command_install.go index bc3bf997..9fd37d50 100644 --- a/commands/command_install.go +++ b/commands/command_install.go @@ -18,13 +18,7 @@ var ( ) func installCommand(cmd *cobra.Command, args []string) { - opt := cmdInstallOptions() - if skipSmudgeInstall { - // assume the user is changing their smudge mode, so enable force implicitly - opt.Force = true - } - - if err := lfs.InstallFilters(opt, skipSmudgeInstall); err != nil { + if err := cmdInstallOptions().Install(); err != nil { Print("WARNING: %s", err.Error()) Print("Run `git lfs install --force` to reset git config.") return @@ -38,7 +32,7 @@ func installCommand(cmd *cobra.Command, args []string) { Print("Git LFS initialized.") } -func cmdInstallOptions() lfs.InstallOptions { +func cmdInstallOptions() *lfs.FilterOptions { requireGitVersion() if localInstall { @@ -52,10 +46,12 @@ func cmdInstallOptions() lfs.InstallOptions { if systemInstall && os.Geteuid() != 0 { Print("WARNING: current user is not root/admin, system install is likely to fail.") } - return lfs.InstallOptions{ - Force: forceInstall, - Local: localInstall, - System: systemInstall, + + return &lfs.FilterOptions{ + Force: forceInstall, + Local: localInstall, + System: systemInstall, + SkipSmudge: skipSmudgeInstall, } } diff --git a/commands/command_uninstall.go b/commands/command_uninstall.go index a578e047..7b886d76 100644 --- a/commands/command_uninstall.go +++ b/commands/command_uninstall.go @@ -8,8 +8,7 @@ import ( // uninstallCmd removes any configuration and hooks set by Git LFS. func uninstallCommand(cmd *cobra.Command, args []string) { - opt := cmdInstallOptions() - if err := lfs.UninstallFilters(opt); err != nil { + if err := cmdInstallOptions().Uninstall(); err != nil { Error(err.Error()) } diff --git a/lfs/attribute.go b/lfs/attribute.go index ac8b1c5e..0c83420f 100644 --- a/lfs/attribute.go +++ b/lfs/attribute.go @@ -26,11 +26,68 @@ type Attribute struct { Upgradeables map[string][]string } -// InstallOptions serves as an argument to Install(). -type InstallOptions struct { - Force bool - Local bool - System bool +// FilterOptions serves as an argument to Install(). +type FilterOptions struct { + Force bool + Local bool + System bool + SkipSmudge bool +} + +func (o *FilterOptions) Install() error { + if o.SkipSmudge { + return skipSmudgeFilterAttribute().Install(o) + } + return filterAttribute().Install(o) +} + +func (o *FilterOptions) Uninstall() error { + filterAttribute().Uninstall(o) + return nil +} + +func filterAttribute() *Attribute { + return &Attribute{ + Section: "filter.lfs", + Properties: map[string]string{ + "clean": "git-lfs clean -- %f", + "smudge": "git-lfs smudge -- %f", + "process": "git-lfs filter-process", + "required": "true", + }, + Upgradeables: upgradeables(), + } +} + +func skipSmudgeFilterAttribute() *Attribute { + return &Attribute{ + Section: "filter.lfs", + Properties: map[string]string{ + "clean": "git-lfs clean -- %f", + "smudge": "git-lfs smudge --skip -- %f", + "process": "git-lfs filter-process --skip", + "required": "true", + }, + Upgradeables: upgradeables(), + } +} + +func upgradeables() map[string][]string { + return map[string][]string{ + "clean": []string{"git-lfs clean %f"}, + "smudge": []string{ + "git-lfs smudge %f", + "git-lfs smudge --skip %f", + "git-lfs smudge -- %f", + "git-lfs smudge --skip -- %f", + }, + "process": []string{ + "git-lfs filter", + "git-lfs filter --skip", + "git-lfs filter-process", + "git-lfs filter-process --skip", + }, + } } // Install instructs Git to set all keys and values relative to the root @@ -39,7 +96,7 @@ type InstallOptions struct { // `force` argument is passed as true. If an attribute is already set to a // different value than what is given, and force is false, an error will be // returned immediately, and the rest of the attributes will not be set. -func (a *Attribute) Install(opt InstallOptions) error { +func (a *Attribute) Install(opt *FilterOptions) error { for k, v := range a.Properties { var upgradeables []string if a.Upgradeables != nil { @@ -65,7 +122,7 @@ func (a *Attribute) normalizeKey(relative string) string { // matching key already exists and the value is not equal to the desired value, // an error will be thrown if force is set to false. If force is true, the value // will be overridden. -func (a *Attribute) set(key, value string, upgradeables []string, opt InstallOptions) error { +func (a *Attribute) set(key, value string, upgradeables []string, opt *FilterOptions) error { var currentValue string if opt.Local { currentValue = git.Config.FindLocal(key) @@ -94,7 +151,7 @@ func (a *Attribute) set(key, value string, upgradeables []string, opt InstallOpt } // Uninstall removes all properties in the path of this property. -func (a *Attribute) Uninstall(opt InstallOptions) { +func (a *Attribute) Uninstall(opt *FilterOptions) { if opt.Local { git.Config.UnsetLocalSection(a.Section) } else if opt.System { diff --git a/lfs/setup.go b/lfs/setup.go index b6c2e30f..09aba9c1 100644 --- a/lfs/setup.go +++ b/lfs/setup.go @@ -27,44 +27,6 @@ var ( postCommitHook, postMergeHook, } - - upgradeables = map[string][]string{ - "clean": []string{"git-lfs clean %f"}, - "smudge": []string{ - "git-lfs smudge %f", - "git-lfs smudge --skip %f", - "git-lfs smudge -- %f", - "git-lfs smudge --skip -- %f", - }, - "process": []string{ - "git-lfs filter", - "git-lfs filter --skip", - "git-lfs filter-process", - "git-lfs filter-process --skip", - }, - } - - filters = &Attribute{ - Section: "filter.lfs", - Properties: map[string]string{ - "clean": "git-lfs clean -- %f", - "smudge": "git-lfs smudge -- %f", - "process": "git-lfs filter-process", - "required": "true", - }, - Upgradeables: upgradeables, - } - - passFilters = &Attribute{ - Section: "filter.lfs", - Properties: map[string]string{ - "clean": "git-lfs clean -- %f", - "smudge": "git-lfs smudge --skip -- %f", - "process": "git-lfs filter-process --skip", - "required": "true", - }, - Upgradeables: upgradeables, - } ) // Get user-readable manual install steps for hooks @@ -100,24 +62,3 @@ func UninstallHooks() error { return nil } - -// InstallFilters installs filters necessary for git-lfs to process normal git -// operations. Currently, that list includes: -// - smudge filter -// - clean filter -// -// An error will be returned if a filter is unable to be set, or if the required -// filters were not present. -func InstallFilters(opt InstallOptions, passThrough bool) error { - if passThrough { - return passFilters.Install(opt) - } - return filters.Install(opt) -} - -// UninstallFilters proxies into the Uninstall method on the Filters type to -// remove all installed filters. -func UninstallFilters(opt InstallOptions) error { - filters.Uninstall(opt) - return nil -}