| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/config" |
| 11 | "github.com/git-lfs/git-lfs/v3/errors" |
| 12 | "github.com/git-lfs/git-lfs/v3/tools" |
| 13 | "github.com/git-lfs/git-lfs/v3/tr" |
| 14 | "github.com/rubyist/tracerx" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | // The basic hook which just calls 'git lfs TYPE' |
| 19 | hookBaseContent = "#!/bin/sh\ncommand -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This 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 the '{{Command}}' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }\ngit lfs {{Command}} \"$@\"" |
| 20 | hookOldContent = "#!/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 the '{{Command}}' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\\n\"; exit 2; }\ngit lfs {{Command}} \"$@\"" |
| 21 | hookOldContent2 = "#!/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/{{Command}}'.\\n\"; exit 2; }\ngit lfs {{Command}} \"$@\"" |
| 22 | hookOldContent3 = "#!/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/{{Command}}.\\n\"; exit 2; }\ngit lfs {{Command}} \"$@\"" |
| 23 | ) |
| 24 | |
| 25 | // A Hook represents a githook as described in http://git-scm.com/docs/githooks. |
| 26 | // Hooks have a type, which is the type of hook that they are, and a body, which |
| 27 | // represents the thing they will execute when invoked by Git. |
| 28 | type Hook struct { |
| 29 | Type string |
| 30 | Contents string |
| 31 | Dir string |
| 32 | upgradeables []string |
| 33 | cfg *config.Configuration |
| 34 | } |
| 35 | |
| 36 | func LoadHooks(hookDir string, cfg *config.Configuration) []*Hook { |
| 37 | return []*Hook{ |
| 38 | NewStandardHook("pre-push", hookDir, []string{ |
| 39 | "#!/bin/sh\ngit lfs push --stdin $*", |
| 40 | "#!/bin/sh\ngit lfs push --stdin \"$@\"", |
| 41 | "#!/bin/sh\ngit lfs pre-push \"$@\"", |
| 42 | "#!/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 \"$@\"", |
| 43 | "#!/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 \"$@\"", |
| 44 | hookOldContent, |
| 45 | hookOldContent2, |
| 46 | hookOldContent3, |
| 47 | }, cfg), |
| 48 | NewStandardHook("post-checkout", hookDir, []string{hookOldContent, hookOldContent2, hookOldContent3}, cfg), |
| 49 | NewStandardHook("post-commit", hookDir, []string{hookOldContent, hookOldContent2, hookOldContent3}, cfg), |
| 50 | NewStandardHook("post-merge", hookDir, []string{hookOldContent, hookOldContent2, hookOldContent3}, cfg), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // NewStandardHook creates a new hook using the template script calling 'git lfs theType' |
| 55 | func NewStandardHook(theType, hookDir string, upgradeables []string, cfg *config.Configuration) *Hook { |
| 56 | formattedUpgradeables := make([]string, 0, len(upgradeables)) |
| 57 | for _, s := range upgradeables { |
| 58 | formattedUpgradeables = append(formattedUpgradeables, strings.Replace(s, "{{Command}}", theType, -1)) |
| 59 | } |
| 60 | return &Hook{ |
| 61 | Type: theType, |
| 62 | Contents: strings.Replace(hookBaseContent, "{{Command}}", theType, -1), |
| 63 | Dir: hookDir, |
| 64 | upgradeables: formattedUpgradeables, |
| 65 | cfg: cfg, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func (h *Hook) Exists() bool { |
| 70 | _, err := os.Stat(h.Path()) |
| 71 | |
| 72 | return !os.IsNotExist(err) |
| 73 | } |
| 74 | |
| 75 | // Path returns the desired (or actual, if installed) location where this hook |
| 76 | // should be installed. It returns an absolute path in all cases. |
| 77 | func (h *Hook) Path() string { |
| 78 | return filepath.Join(h.Dir, h.Type) |
| 79 | } |
| 80 | |
| 81 | // Install installs this Git hook on disk, or upgrades it if it does exist, and |
| 82 | // is upgradeable. It will create a hooks directory relative to the local Git |
| 83 | // directory. It returns and halts at any errors, and returns nil if the |
| 84 | // operation was a success. |
| 85 | func (h *Hook) Install(force bool) error { |
| 86 | msg := fmt.Sprintf("Install hook: %s, force=%t, path=%s", h.Type, force, h.Path()) |
| 87 | |
| 88 | if err := tools.MkdirAll(h.Dir, h.cfg); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | if h.Exists() && !force { |
| 93 | tracerx.Printf(msg + ", upgrading...") |
| 94 | return h.Upgrade() |
| 95 | } |
| 96 | |
| 97 | tracerx.Printf(msg) |
| 98 | return h.write() |
| 99 | } |
| 100 | |
| 101 | // write writes the contents of this Hook to disk, appending a newline at the |
| 102 | // end, and sets the mode to octal 0755. It writes to disk unconditionally, and |
| 103 | // returns at any error. |
| 104 | func (h *Hook) write() error { |
| 105 | return os.WriteFile(h.Path(), []byte(h.Contents+"\n"), 0755) |
| 106 | } |
| 107 | |
| 108 | // Upgrade upgrades the (assumed to be) existing git hook to the current |
| 109 | // contents. A hook is considered "upgrade-able" if its contents are matched in |
| 110 | // the member variable `Upgradeables`. It halts and returns any errors as they |
| 111 | // arise. |
| 112 | func (h *Hook) Upgrade() error { |
| 113 | upgradable, match, err := h.matchesCurrent() |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | |
| 118 | if !upgradable || match { |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | return h.write() |
| 123 | } |
| 124 | |
| 125 | // Uninstall removes the hook on disk so long as it matches the current version, |
| 126 | // or any of the past versions of this hook. |
| 127 | func (h *Hook) Uninstall() error { |
| 128 | msg := fmt.Sprintf("Uninstall hook: %s, path=%s", h.Type, h.Path()) |
| 129 | |
| 130 | upgradable, _, err := h.matchesCurrent() |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | |
| 135 | if !upgradable { |
| 136 | tracerx.Printf(msg + ", doesn't match...") |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | tracerx.Printf(msg) |
| 141 | return os.RemoveAll(h.Path()) |
| 142 | } |
| 143 | |
| 144 | // matchesCurrent returns whether or not an existing git hook is able to be |
| 145 | // written to or upgraded and additionally whether it is identical to the |
| 146 | // current hook. A git hook matches those conditions if and only if its contents |
| 147 | // match the current contents, or any past "upgrade-able" contents of this hook. |
| 148 | func (h *Hook) matchesCurrent() (bool, bool, error) { |
| 149 | file, err := os.Open(h.Path()) |
| 150 | if err != nil { |
| 151 | return false, false, err |
| 152 | } |
| 153 | |
| 154 | by, err := io.ReadAll(io.LimitReader(file, 1024)) |
| 155 | file.Close() |
| 156 | if err != nil { |
| 157 | return false, false, err |
| 158 | } |
| 159 | |
| 160 | contents := strings.TrimSpace(tools.Undent(string(by))) |
| 161 | if contents == h.Contents { |
| 162 | return true, true, nil |
| 163 | } else if len(contents) == 0 { |
| 164 | return true, false, nil |
| 165 | } |
| 166 | |
| 167 | for _, u := range h.upgradeables { |
| 168 | if u == contents { |
| 169 | return true, false, nil |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return false, false, errors.New(fmt.Sprintf("%s\n\n%s\n", tr.Tr.Get("Hook already exists: %s", string(h.Type)), tools.Indent(contents))) |
| 174 | } |