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 (
2017-01-02 13:02:00 +00:00
// prePushHook invokes `git lfs pre-push` at the pre-push phase.
prePushHook = NewStandardHook ( "pre-push" , [ ] 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 \"$@\"" ,
} )
2017-01-02 13:07:53 +00:00
// postCheckoutHook invokes `git lfs post-checkout`
postCheckoutHook = NewStandardHook ( "post-checkout" , [ ] string { } )
2017-01-02 14:36:28 +00:00
postCommitHook = NewStandardHook ( "post-commit" , [ ] string { } )
2017-01-02 16:56:47 +00:00
postMergeHook = NewStandardHook ( "post-merge" , [ ] string { } )
2014-06-03 22:03:43 +00:00
2015-08-29 04:14:06 +00:00
hooks = [ ] * Hook {
prePushHook ,
2017-01-02 13:07:53 +00:00
postCheckoutHook ,
2017-01-02 14:36:28 +00:00
postCommitHook ,
2017-01-02 16:56:47 +00:00
postMergeHook ,
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" ,
2016-11-08 20:05:12 +00:00
"process" : "git-lfs filter-process" ,
2015-08-31 00:09:28 +00:00
"required" : "true" ,
} ,
2016-09-01 11:29:49 +00:00
Upgradeables : map [ string ] [ ] string {
2016-11-08 14:24:36 +00:00
"clean" : [ ] string { "git-lfs clean %f" } ,
"smudge" : [ ] string { "git-lfs smudge %f" } ,
"process" : [ ] string { "git-lfs filter" } ,
2016-09-01 11:29:49 +00:00
} ,
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" ,
2016-11-08 20:05:12 +00:00
"process" : "git-lfs filter-process --skip" ,
2015-09-23 18:20:30 +00:00
"required" : "true" ,
} ,
2016-09-01 11:29:49 +00:00
Upgradeables : map [ string ] [ ] string {
2016-11-08 14:24:36 +00:00
"clean" : [ ] string { "git-lfs clean %f" } ,
"smudge" : [ ] string { "git-lfs smudge --skip %f" } ,
"process" : [ ] string { "git-lfs filter --skip" } ,
2016-09-01 11:29:49 +00:00
} ,
2015-09-23 18:20:30 +00:00
}
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
}