| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "github.com/git-lfs/git-lfs/v3/errors" |
| 9 | "github.com/git-lfs/git-lfs/v3/filepathfilter" |
| 10 | "github.com/git-lfs/git-lfs/v3/git" |
| 11 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 12 | "github.com/git-lfs/git-lfs/v3/tasklog" |
| 13 | "github.com/git-lfs/git-lfs/v3/tools" |
| 14 | "github.com/git-lfs/git-lfs/v3/tq" |
| 15 | "github.com/git-lfs/git-lfs/v3/tr" |
| 16 | "github.com/spf13/cobra" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | checkoutTo string |
| 21 | checkoutBase bool |
| 22 | checkoutOurs bool |
| 23 | checkoutTheirs bool |
| 24 | ) |
| 25 | |
| 26 | func checkoutCommand(cmd *cobra.Command, args []string) { |
| 27 | setupRepository() |
| 28 | |
| 29 | // TODO: After suitable advance public notice, replace this block |
| 30 | // and the preceding call to setupRepository() with a single call to |
| 31 | // setupWorkingCopy(), which will perform the same check for a bare |
| 32 | // repository but will exit non-zero, as other commands already do. |
| 33 | if cfg.LocalWorkingDir() == "" { |
| 34 | Print(tr.Tr.Get("This operation must be run in a work tree.")) |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | stage, err := whichCheckout() |
| 39 | if err != nil { |
| 40 | Exit(tr.Tr.Get("Error parsing args: %v", err)) |
| 41 | } |
| 42 | |
| 43 | if checkoutTo != "" && stage != git.IndexStageDefault { |
| 44 | if len(args) != 1 { |
| 45 | Exit(tr.Tr.Get("--to requires exactly one Git LFS object file path")) |
| 46 | } |
| 47 | checkoutConflict(args[0], stage) |
| 48 | return |
| 49 | } else if checkoutTo != "" || stage != git.IndexStageDefault { |
| 50 | Exit(tr.Tr.Get("--to and exactly one of --theirs, --ours, and --base must be used together")) |
| 51 | } |
| 52 | |
| 53 | ref, err := git.CurrentRef() |
| 54 | if err != nil { |
| 55 | Panic(err, tr.Tr.Get("Could not checkout")) |
| 56 | } |
| 57 | |
| 58 | rootedPathPatterns := rootedPathPatterns(args) |
| 59 | |
| 60 | // will chdir to root of working tree, if one exists |
| 61 | singleCheckout := newSingleCheckout(cfg.Git, "") |
| 62 | if singleCheckout.Skip() { |
| 63 | fmt.Println(tr.Tr.Get("Cannot checkout LFS objects, Git LFS is not installed.")) |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | var totalBytes int64 |
| 68 | var pointers []*lfs.WrappedPointer |
| 69 | logger := tasklog.NewLogger(os.Stdout, |
| 70 | tasklog.ForceProgress(cfg.ForceProgress()), |
| 71 | ) |
| 72 | meter := tq.NewMeter(cfg) |
| 73 | meter.Direction = tq.Checkout |
| 74 | meter.Logger = meter.LoggerFromEnv(cfg.Os) |
| 75 | logger.Enqueue(meter) |
| 76 | chgitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) { |
| 77 | if err != nil { |
| 78 | LoggedError(err, tr.Tr.Get("Scanner error: %s", err)) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | totalBytes += p.Size |
| 83 | meter.Add(p.Size) |
| 84 | meter.StartTransfer(p.Name) |
| 85 | pointers = append(pointers, p) |
| 86 | }) |
| 87 | |
| 88 | chgitscanner.Filter = filepathfilter.New(rootedPathPatterns, nil, filepathfilter.GitIgnore, cfg.Git) |
| 89 | |
| 90 | if err := chgitscanner.ScanLFSFiles(ref.Sha, nil); err != nil { |
| 91 | ExitWithError(err) |
| 92 | } |
| 93 | |
| 94 | meter.Start() |
| 95 | for _, p := range pointers { |
| 96 | singleCheckout.Run(p) |
| 97 | |
| 98 | // not strictly correct (parallel) but we don't have a callback & it's just local |
| 99 | // plus only 1 slot in channel so it'll block & be close |
| 100 | meter.TransferBytes("checkout", p.Name, p.Size, totalBytes, int(p.Size)) |
| 101 | meter.FinishTransfer(p.Name) |
| 102 | } |
| 103 | |
| 104 | meter.Finish() |
| 105 | singleCheckout.Close() |
| 106 | } |
| 107 | |
| 108 | func checkoutConflict(file string, stage git.IndexStage) { |
| 109 | checkoutTo, err := filepath.Abs(checkoutTo) |
| 110 | if err != nil { |
| 111 | Exit(tr.Tr.Get("Could not convert %q to absolute path: %v", checkoutTo, err)) |
| 112 | } |
| 113 | |
| 114 | err = tools.MkdirAll(filepath.Dir(checkoutTo), cfg) |
| 115 | if err != nil { |
| 116 | Exit(tr.Tr.Get("Could not create path %q: %v", checkoutTo, err)) |
| 117 | } |
| 118 | |
| 119 | pathConverter, err := lfs.NewCurrentToRepoPathConverter(cfg) |
| 120 | if err != nil { |
| 121 | Panic(err, tr.Tr.Get("Could not checkout")) |
| 122 | } |
| 123 | |
| 124 | file = pathConverter.Convert(file) |
| 125 | |
| 126 | // will chdir to root of working tree, if one exists |
| 127 | singleCheckout := newSingleCheckout(cfg.Git, "") |
| 128 | if singleCheckout.Skip() { |
| 129 | fmt.Println(tr.Tr.Get("Cannot checkout LFS objects, Git LFS is not installed.")) |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | ref, err := git.ResolveRef(fmt.Sprintf(":%d:%s", stage, file)) |
| 134 | if err != nil { |
| 135 | Exit(tr.Tr.Get("Could not checkout (are you not in the middle of a merge?): %v", err)) |
| 136 | } |
| 137 | |
| 138 | scanner, err := git.NewObjectScanner(cfg.GitEnv(), cfg.OSEnv()) |
| 139 | if err != nil { |
| 140 | Exit(tr.Tr.Get("Could not create object scanner: %v", err)) |
| 141 | } |
| 142 | |
| 143 | if !scanner.Scan(ref.Sha) { |
| 144 | Exit(tr.Tr.Get("Could not find object %q", ref.Sha)) |
| 145 | } |
| 146 | |
| 147 | ptr, err := lfs.DecodePointer(scanner.Contents()) |
| 148 | if err != nil { |
| 149 | Exit(tr.Tr.Get("Could not find decoder pointer for object %q: %v", ref.Sha, err)) |
| 150 | } |
| 151 | |
| 152 | p := &lfs.WrappedPointer{Name: file, Pointer: ptr} |
| 153 | |
| 154 | if err := singleCheckout.RunToPath(p, checkoutTo); err != nil { |
| 155 | Exit(tr.Tr.Get("Error checking out %v to %q: %v", ref.Sha, checkoutTo, err)) |
| 156 | } |
| 157 | singleCheckout.Close() |
| 158 | } |
| 159 | |
| 160 | func whichCheckout() (stage git.IndexStage, err error) { |
| 161 | seen := 0 |
| 162 | stage = git.IndexStageDefault |
| 163 | |
| 164 | if checkoutBase { |
| 165 | seen++ |
| 166 | stage = git.IndexStageBase |
| 167 | } |
| 168 | if checkoutOurs { |
| 169 | seen++ |
| 170 | stage = git.IndexStageOurs |
| 171 | } |
| 172 | if checkoutTheirs { |
| 173 | seen++ |
| 174 | stage = git.IndexStageTheirs |
| 175 | } |
| 176 | |
| 177 | if seen > 1 { |
| 178 | return 0, errors.New(tr.Tr.Get("at most one of --base, --theirs, and --ours is allowed")) |
| 179 | } |
| 180 | return stage, nil |
| 181 | } |
| 182 | |
| 183 | // Parameters are filters |
| 184 | // firstly convert any pathspecs to patterns relative to the root of the repo, |
| 185 | // in case this is being executed in a sub-folder |
| 186 | func rootedPathPatterns(args []string) []string { |
| 187 | patternConverter, err := lfs.NewCurrentToRepoPatternConverter(cfg) |
| 188 | if err != nil { |
| 189 | Panic(err, tr.Tr.Get("Could not checkout")) |
| 190 | } |
| 191 | |
| 192 | rootedPathPatterns := make([]string, 0, len(args)) |
| 193 | for _, arg := range args { |
| 194 | rootedPathPatterns = append(rootedPathPatterns, patternConverter.Convert(arg)) |
| 195 | } |
| 196 | return rootedPathPatterns |
| 197 | } |
| 198 | |
| 199 | func init() { |
| 200 | RegisterCommand("checkout", checkoutCommand, func(cmd *cobra.Command) { |
| 201 | cmd.Flags().StringVar(&checkoutTo, "to", "", "Checkout a conflicted file to this path") |
| 202 | cmd.Flags().BoolVar(&checkoutOurs, "ours", false, "Checkout our version of a conflicted file") |
| 203 | cmd.Flags().BoolVar(&checkoutTheirs, "theirs", false, "Checkout their version of a conflicted file") |
| 204 | cmd.Flags().BoolVar(&checkoutBase, "base", false, "Checkout the base version of a conflicted file") |
| 205 | }) |
| 206 | } |