| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | |
| 6 | "github.com/git-lfs/git-lfs/v3/git" |
| 7 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 8 | "github.com/git-lfs/git-lfs/v3/tr" |
| 9 | "github.com/spf13/cobra" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | fileInstall = "" |
| 14 | forceInstall = false |
| 15 | localInstall = false |
| 16 | worktreeInstall = false |
| 17 | manualInstall = false |
| 18 | systemInstall = false |
| 19 | skipSmudgeInstall = false |
| 20 | skipRepoInstall = false |
| 21 | ) |
| 22 | |
| 23 | func installCommand(cmd *cobra.Command, args []string) { |
| 24 | if err := cmdInstallOptions().Install(); err != nil { |
| 25 | Print(tr.Tr.Get("warning: %s", err.Error())) |
| 26 | Print(tr.Tr.Get("Run `git lfs install --force` to reset Git configuration.")) |
| 27 | ExitWithCode(2) |
| 28 | } |
| 29 | |
| 30 | if !skipRepoInstall && (localInstall || worktreeInstall || cfg.InRepo()) { |
| 31 | installHooksCommand(cmd, args) |
| 32 | } |
| 33 | |
| 34 | Print(tr.Tr.Get("Git LFS initialized.")) |
| 35 | } |
| 36 | |
| 37 | func cmdInstallOptions() *lfs.FilterOptions { |
| 38 | requireGitVersion() |
| 39 | |
| 40 | if localInstall || worktreeInstall { |
| 41 | setupRepository() |
| 42 | } |
| 43 | |
| 44 | destArgs := 0 |
| 45 | if localInstall { |
| 46 | destArgs++ |
| 47 | } |
| 48 | if worktreeInstall { |
| 49 | destArgs++ |
| 50 | } |
| 51 | if systemInstall { |
| 52 | destArgs++ |
| 53 | } |
| 54 | if fileInstall != "" { |
| 55 | destArgs++ |
| 56 | } |
| 57 | |
| 58 | if destArgs > 1 { |
| 59 | Exit(tr.Tr.Get("Only one of the --local, --system, --worktree, and --file options can be specified.")) |
| 60 | } |
| 61 | |
| 62 | // This call will return -1 on Windows; don't warn about this there, |
| 63 | // since we can't detect it correctly. |
| 64 | uid := os.Geteuid() |
| 65 | if systemInstall && uid != 0 && uid != -1 { |
| 66 | Print(tr.Tr.Get("warning: current user is not root/admin, system install is likely to fail.")) |
| 67 | } |
| 68 | |
| 69 | return &lfs.FilterOptions{ |
| 70 | GitConfig: cfg.GitConfig(), |
| 71 | Force: forceInstall, |
| 72 | File: fileInstall, |
| 73 | Local: localInstall, |
| 74 | Worktree: worktreeInstall, |
| 75 | System: systemInstall, |
| 76 | SkipSmudge: skipSmudgeInstall, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func installHooksCommand(cmd *cobra.Command, args []string) { |
| 81 | updateForce = forceInstall |
| 82 | |
| 83 | // TODO(@ttaylorr): this is a hack since the `git-lfs-install(1)` calls |
| 84 | // into the function that implements `git-lfs-update(1)`. Given that, |
| 85 | // there is no way to pass flags into that function, other than |
| 86 | // hijacking the flags that `git-lfs-update(1)` already owns. |
| 87 | // |
| 88 | // At a later date, extract `git-lfs-update(1)`-related logic into its |
| 89 | // own function, and translate this flag as a boolean argument to it. |
| 90 | updateManual = manualInstall |
| 91 | |
| 92 | updateCommand(cmd, args) |
| 93 | } |
| 94 | |
| 95 | func init() { |
| 96 | RegisterCommand("install", installCommand, func(cmd *cobra.Command) { |
| 97 | cmd.Flags().BoolVarP(&forceInstall, "force", "f", false, "Set the Git LFS global config, overwriting previous values.") |
| 98 | cmd.Flags().BoolVarP(&localInstall, "local", "l", false, "Set the Git LFS config for the local Git repository only.") |
| 99 | cmd.Flags().StringVarP(&fileInstall, "file", "", "", "Set the Git LFS config for the given configuration file only.") |
| 100 | if git.IsGitVersionAtLeast("2.20.0") { |
| 101 | cmd.Flags().BoolVarP(&worktreeInstall, "worktree", "w", false, "Set the Git LFS config for the current Git working tree, if multiple working trees are configured; otherwise, the same as --local.") |
| 102 | } |
| 103 | cmd.Flags().BoolVarP(&systemInstall, "system", "", false, "Set the Git LFS config in system-wide scope.") |
| 104 | cmd.Flags().BoolVarP(&skipSmudgeInstall, "skip-smudge", "s", false, "Skip automatic downloading of objects on clone or pull.") |
| 105 | cmd.Flags().BoolVarP(&skipRepoInstall, "skip-repo", "", false, "Skip repo setup, just install global filters.") |
| 106 | cmd.Flags().BoolVarP(&manualInstall, "manual", "m", false, "Print instructions for manual install.") |
| 107 | cmd.AddCommand(NewCommand("hooks", installHooksCommand)) |
| 108 | }) |
| 109 | } |