| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/subprocess" |
| 11 | "github.com/git-lfs/git-lfs/v3/tr" |
| 12 | |
| 13 | "github.com/git-lfs/git-lfs/v3/git" |
| 14 | "github.com/git-lfs/git-lfs/v3/tools" |
| 15 | "github.com/spf13/cobra" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | cloneFlags git.CloneFlags |
| 20 | |
| 21 | cloneSkipRepoInstall bool |
| 22 | ) |
| 23 | |
| 24 | func cloneCommand(cmd *cobra.Command, args []string) { |
| 25 | requireGitVersion() |
| 26 | |
| 27 | if git.IsGitVersionAtLeast("2.15.0") { |
| 28 | // TRANSLATORS: Individual lines should not exceed 80 |
| 29 | // characters, and any additional lines in the first message |
| 30 | // should be indented to align with the first line's text |
| 31 | // following the warning prefix and punctuation. |
| 32 | msg := fmt.Sprintf("%s\n\n%s", |
| 33 | tr.Tr.Get("WARNING: `git lfs clone` is deprecated and will not be updated\n with new flags from `git clone`"), |
| 34 | tr.Tr.Get("`git clone` has been updated in upstream Git to have comparable\nspeeds to `git lfs clone`.")) |
| 35 | |
| 36 | fmt.Fprintln(os.Stderr, msg) |
| 37 | } |
| 38 | |
| 39 | // We pass all args to git clone |
| 40 | err := git.CloneWithoutFilters(cloneFlags, args) |
| 41 | if err != nil { |
| 42 | Exit("%s\n%v", tr.Tr.Get("Error(s) during clone:"), err) |
| 43 | } |
| 44 | |
| 45 | // now execute pull (need to be inside dir) |
| 46 | cwd, err := tools.Getwd() |
| 47 | if err != nil { |
| 48 | Exit(tr.Tr.Get("Unable to derive current working dir: %v", err)) |
| 49 | } |
| 50 | |
| 51 | // Either the last argument was a relative or local dir, or we have to |
| 52 | // derive it from the clone URL |
| 53 | clonedir, err := filepath.Abs(args[len(args)-1]) |
| 54 | if err != nil || !tools.DirExists(clonedir) { |
| 55 | // Derive from clone URL instead |
| 56 | base := path.Base(args[len(args)-1]) |
| 57 | if strings.HasSuffix(base, ".git") { |
| 58 | base = base[:len(base)-4] |
| 59 | } |
| 60 | clonedir, _ = filepath.Abs(base) |
| 61 | if !tools.DirExists(clonedir) { |
| 62 | Exit(tr.Tr.Get("Unable to find clone dir at %q", clonedir)) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | err = os.Chdir(clonedir) |
| 67 | if err != nil { |
| 68 | Exit(tr.Tr.Get("Unable to change directory to clone dir %q: %v", clonedir, err)) |
| 69 | } |
| 70 | |
| 71 | // Make sure we pop back to dir we started in at the end |
| 72 | defer os.Chdir(cwd) |
| 73 | |
| 74 | setupRepository() |
| 75 | |
| 76 | // Support --origin option to clone |
| 77 | if len(cloneFlags.Origin) > 0 { |
| 78 | cfg.SetRemote(cloneFlags.Origin) |
| 79 | } |
| 80 | |
| 81 | if ref, err := git.CurrentRef(); err == nil { |
| 82 | includeArg, excludeArg := getIncludeExcludeArgs(cmd) |
| 83 | filter := buildFilepathFilter(cfg, includeArg, excludeArg, true) |
| 84 | if cloneFlags.NoCheckout || cloneFlags.Bare { |
| 85 | // If --no-checkout or --bare then we shouldn't check out, just fetch instead |
| 86 | fetchRef(ref.Name, filter, nil) |
| 87 | } else { |
| 88 | pull(filter) |
| 89 | err := postCloneSubmodules(args) |
| 90 | if err != nil { |
| 91 | Exit(tr.Tr.Get("Error performing `git lfs pull` for submodules: %v", err)) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if !cloneSkipRepoInstall { |
| 97 | // If --skip-repo wasn't given, install repo-level hooks while |
| 98 | // we're still in the checkout directory. |
| 99 | |
| 100 | if err := installHooks(false); err != nil { |
| 101 | ExitWithError(err) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func postCloneSubmodules(args []string) error { |
| 107 | // In git 2.9+ the filter option will have been passed through to submodules |
| 108 | // So we need to lfs pull inside each |
| 109 | if !git.IsGitVersionAtLeast("2.9.0") { |
| 110 | // In earlier versions submodules would have used smudge filter |
| 111 | return nil |
| 112 | } |
| 113 | // Also we only do this if --recursive or --recurse-submodules was provided |
| 114 | if !cloneFlags.Recursive && !cloneFlags.RecurseSubmodules { |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // Use `git submodule foreach --recursive` to cascade into nested submodules |
| 119 | // Also good to call a new instance of git-lfs rather than do things |
| 120 | // inside this instance, since that way we get a clean env in that subrepo |
| 121 | cmd, err := subprocess.ExecCommand("git", "submodule", "foreach", "--recursive", |
| 122 | "git lfs pull") |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | cmd.Stderr = os.Stderr |
| 127 | cmd.Stdin = os.Stdin |
| 128 | cmd.Stdout = os.Stdout |
| 129 | return cmd.Run() |
| 130 | } |
| 131 | |
| 132 | func init() { |
| 133 | RegisterCommand("clone", cloneCommand, func(cmd *cobra.Command) { |
| 134 | cmd.PreRun = nil |
| 135 | |
| 136 | // Mirror all git clone flags |
| 137 | cmd.Flags().StringVarP(&cloneFlags.TemplateDirectory, "template", "", "", "See 'git clone --help'") |
| 138 | cmd.Flags().BoolVarP(&cloneFlags.Local, "local", "l", false, "See 'git clone --help'") |
| 139 | cmd.Flags().BoolVarP(&cloneFlags.Shared, "shared", "s", false, "See 'git clone --help'") |
| 140 | cmd.Flags().BoolVarP(&cloneFlags.NoHardlinks, "no-hardlinks", "", false, "See 'git clone --help'") |
| 141 | cmd.Flags().BoolVarP(&cloneFlags.Quiet, "quiet", "q", false, "See 'git clone --help'") |
| 142 | cmd.Flags().BoolVarP(&cloneFlags.NoCheckout, "no-checkout", "n", false, "See 'git clone --help'") |
| 143 | cmd.Flags().BoolVarP(&cloneFlags.Progress, "progress", "", false, "See 'git clone --help'") |
| 144 | cmd.Flags().BoolVarP(&cloneFlags.Bare, "bare", "", false, "See 'git clone --help'") |
| 145 | cmd.Flags().BoolVarP(&cloneFlags.Mirror, "mirror", "", false, "See 'git clone --help'") |
| 146 | cmd.Flags().StringVarP(&cloneFlags.Origin, "origin", "o", "", "See 'git clone --help'") |
| 147 | cmd.Flags().StringVarP(&cloneFlags.Branch, "branch", "b", "", "See 'git clone --help'") |
| 148 | cmd.Flags().StringVarP(&cloneFlags.Upload, "upload-pack", "u", "", "See 'git clone --help'") |
| 149 | cmd.Flags().StringVarP(&cloneFlags.Reference, "reference", "", "", "See 'git clone --help'") |
| 150 | cmd.Flags().StringVarP(&cloneFlags.ReferenceIfAble, "reference-if-able", "", "", "See 'git clone --help'") |
| 151 | cmd.Flags().BoolVarP(&cloneFlags.Dissociate, "dissociate", "", false, "See 'git clone --help'") |
| 152 | cmd.Flags().StringVarP(&cloneFlags.SeparateGit, "separate-git-dir", "", "", "See 'git clone --help'") |
| 153 | cmd.Flags().StringVarP(&cloneFlags.Depth, "depth", "", "", "See 'git clone --help'") |
| 154 | cmd.Flags().BoolVarP(&cloneFlags.Recursive, "recursive", "", false, "See 'git clone --help'") |
| 155 | cmd.Flags().BoolVarP(&cloneFlags.RecurseSubmodules, "recurse-submodules", "", false, "See 'git clone --help'") |
| 156 | cmd.Flags().StringVarP(&cloneFlags.Config, "config", "c", "", "See 'git clone --help'") |
| 157 | cmd.Flags().BoolVarP(&cloneFlags.SingleBranch, "single-branch", "", false, "See 'git clone --help'") |
| 158 | cmd.Flags().BoolVarP(&cloneFlags.NoSingleBranch, "no-single-branch", "", false, "See 'git clone --help'") |
| 159 | cmd.Flags().BoolVarP(&cloneFlags.Verbose, "verbose", "v", false, "See 'git clone --help'") |
| 160 | cmd.Flags().BoolVarP(&cloneFlags.Ipv4, "ipv4", "", false, "See 'git clone --help'") |
| 161 | cmd.Flags().BoolVarP(&cloneFlags.Ipv6, "ipv6", "", false, "See 'git clone --help'") |
| 162 | cmd.Flags().StringVarP(&cloneFlags.ShallowSince, "shallow-since", "", "", "See 'git clone --help'") |
| 163 | cmd.Flags().StringVarP(&cloneFlags.ShallowExclude, "shallow-exclude", "", "", "See 'git clone --help'") |
| 164 | cmd.Flags().BoolVarP(&cloneFlags.ShallowSubmodules, "shallow-submodules", "", false, "See 'git clone --help'") |
| 165 | cmd.Flags().BoolVarP(&cloneFlags.NoShallowSubmodules, "no-shallow-submodules", "", false, "See 'git clone --help'") |
| 166 | cmd.Flags().Int64VarP(&cloneFlags.Jobs, "jobs", "j", -1, "See 'git clone --help'") |
| 167 | |
| 168 | cmd.Flags().StringVarP(&includeArg, "include", "I", "", "Include a list of paths") |
| 169 | cmd.Flags().StringVarP(&excludeArg, "exclude", "X", "", "Exclude a list of paths") |
| 170 | |
| 171 | cmd.Flags().BoolVar(&cloneSkipRepoInstall, "skip-repo", false, "Skip LFS repo setup") |
| 172 | }) |
| 173 | } |