2015-03-19 19:30:55 +00:00
package lfs
2014-06-03 22:03:43 +00:00
2016-04-26 11:04:55 +00:00
import (
"bytes"
"fmt"
)
2014-06-04 14:29:19 +00:00
var (
2015-08-29 04:14:06 +00:00
// prePushHook invokes `git lfs push` at the pre-push phase.
prePushHook = & Hook {
2015-09-02 18:57:09 +00:00
Type : "pre-push" ,
2015-08-29 04:14:06 +00:00
Contents : "#!/bin/sh\ncommand -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/pre-push.\\n\"; exit 2; }\ngit lfs pre-push \"$@\"" ,
Upgradeables : [ ] string {
"#!/bin/sh\ngit lfs push --stdin $*" ,
"#!/bin/sh\ngit lfs push --stdin \"$@\"" ,
"#!/bin/sh\ngit lfs pre-push \"$@\"" ,
"#!/bin/sh\ncommand -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 0; }\ngit lfs pre-push \"$@\"" ,
"#!/bin/sh\ncommand -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 2; }\ngit lfs pre-push \"$@\"" ,
} ,
2014-06-03 22:03:43 +00:00
}
2015-08-29 04:14:06 +00:00
hooks = [ ] * Hook {
prePushHook ,
2014-08-06 20:41:43 +00:00
}
2015-08-31 00:09:28 +00:00
filters = & Attribute {
2015-09-02 18:57:09 +00:00
Section : "filter.lfs" ,
2015-08-31 00:09:28 +00:00
Properties : map [ string ] string {
2016-03-17 21:25:33 +00:00
"clean" : "git-lfs clean -- %f" ,
"smudge" : "git-lfs smudge -- %f" ,
2015-08-31 00:09:28 +00:00
"required" : "true" ,
} ,
2015-07-10 16:43:20 +00:00
}
2015-09-23 18:20:30 +00:00
passFilters = & Attribute {
Section : "filter.lfs" ,
Properties : map [ string ] string {
2016-03-17 21:25:33 +00:00
"clean" : "git-lfs clean -- %f" ,
"smudge" : "git-lfs smudge --skip -- %f" ,
2015-09-23 18:20:30 +00:00
"required" : "true" ,
} ,
}
2015-08-29 04:14:06 +00:00
)
2015-07-10 16:43:20 +00:00
2016-04-26 11:04:55 +00:00
// Get user-readable manual install steps for hooks
func GetHookInstallSteps ( ) string {
var buf bytes . Buffer
for _ , h := range hooks {
buf . WriteString ( fmt . Sprintf ( "Add the following to .git/hooks/%s :\n\n" , h . Type ) )
buf . WriteString ( h . Contents )
buf . WriteString ( "\n" )
}
return buf . String ( )
}
2015-08-29 04:14:06 +00:00
// InstallHooks installs all hooks in the `hooks` var.
func InstallHooks ( force bool ) error {
for _ , h := range hooks {
if err := h . Install ( force ) ; err != nil {
return err
}
2015-07-10 16:43:20 +00:00
}
return nil
}
2015-10-01 19:44:28 +00:00
// UninstallHooks removes all hooks in range of the `hooks` var.
2015-08-29 04:14:06 +00:00
func UninstallHooks ( ) error {
for _ , h := range hooks {
if err := h . Uninstall ( ) ; err != nil {
return err
}
2015-04-24 17:33:22 +00:00
}
2015-05-12 08:50:20 +00:00
return nil
2014-06-03 22:03:43 +00:00
}
2015-08-31 00:13:19 +00:00
// InstallFilters installs filters necessary for git-lfs to process normal git
2015-08-29 04:14:06 +00:00
// 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.
2015-09-23 18:20:30 +00:00
func InstallFilters ( opt InstallOptions , passThrough bool ) error {
if passThrough {
return passFilters . Install ( opt )
}
2015-09-23 17:58:16 +00:00
return filters . Install ( opt )
2015-07-10 16:43:20 +00:00
}
2015-08-31 00:13:19 +00:00
// UninstallFilters proxies into the Uninstall method on the Filters type to
2015-08-29 04:14:06 +00:00
// remove all installed filters.
2015-08-31 00:13:19 +00:00
func UninstallFilters ( ) error {
2015-08-31 00:09:28 +00:00
filters . Uninstall ( )
2014-06-05 18:48:23 +00:00
return nil
2014-06-03 22:03:43 +00:00
}