| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/url" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/errors" |
| 11 | "github.com/git-lfs/git-lfs/v3/git" |
| 12 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 13 | "github.com/git-lfs/git-lfs/v3/tasklog" |
| 14 | "github.com/git-lfs/git-lfs/v3/tools" |
| 15 | "github.com/git-lfs/git-lfs/v3/tq" |
| 16 | "github.com/git-lfs/git-lfs/v3/tr" |
| 17 | "github.com/rubyist/tracerx" |
| 18 | ) |
| 19 | |
| 20 | func uploadForRefUpdates(ctx *uploadContext, updates []*git.RefUpdate, pushAll bool) error { |
| 21 | gitscanner := ctx.buildGitScanner() |
| 22 | defer ctx.ReportErrors() |
| 23 | |
| 24 | verifyLocksForUpdates(ctx.lockVerifier, updates) |
| 25 | exclude := make([]string, 0, len(updates)) |
| 26 | for _, update := range updates { |
| 27 | remoteRefSha := update.RemoteRef().Sha |
| 28 | if update.LocalRefCommitish() != remoteRefSha { |
| 29 | exclude = append(exclude, remoteRefSha) |
| 30 | } |
| 31 | } |
| 32 | for _, update := range updates { |
| 33 | // initialized here to prevent looped defer |
| 34 | q := ctx.NewQueue( |
| 35 | tq.RemoteRef(update.RemoteRef()), |
| 36 | ) |
| 37 | err := uploadRangeOrAll(gitscanner, ctx, q, exclude, update, pushAll) |
| 38 | ctx.CollectErrors(q) |
| 39 | |
| 40 | if err != nil { |
| 41 | return errors.Wrap(err, tr.Tr.Get("ref %q:", update.LocalRef().Name)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | func uploadRangeOrAll(g *lfs.GitScanner, ctx *uploadContext, q *tq.TransferQueue, exclude []string, update *git.RefUpdate, pushAll bool) error { |
| 49 | cb := ctx.gitScannerCallback(q) |
| 50 | if pushAll { |
| 51 | if err := g.ScanRefWithDeleted(update.LocalRefCommitish(), cb); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | } else { |
| 55 | if err := g.ScanMultiRangeToRemote(update.LocalRefCommitish(), exclude, cb); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | } |
| 59 | return ctx.scannerError() |
| 60 | } |
| 61 | |
| 62 | type uploadContext struct { |
| 63 | Remote string |
| 64 | DryRun bool |
| 65 | Manifest tq.Manifest |
| 66 | uploadedOids tools.StringSet |
| 67 | gitfilter *lfs.GitFilter |
| 68 | |
| 69 | logger *tasklog.Logger |
| 70 | meter *tq.Meter |
| 71 | |
| 72 | committerName string |
| 73 | committerEmail string |
| 74 | |
| 75 | lockVerifier *lockVerifier |
| 76 | |
| 77 | // allowMissing specifies whether pushes containing missing/corrupt |
| 78 | // pointers should allow pushing Git blobs |
| 79 | allowMissing bool |
| 80 | |
| 81 | // tracks errors from gitscanner callbacks |
| 82 | scannerErr error |
| 83 | errMu sync.Mutex |
| 84 | |
| 85 | // filename => oid |
| 86 | missing map[string]string |
| 87 | corrupt map[string]string |
| 88 | otherErrs []error |
| 89 | } |
| 90 | |
| 91 | func newUploadContext(dryRun bool) *uploadContext { |
| 92 | remote := cfg.PushRemote() |
| 93 | manifest := getTransferManifestOperationRemote("upload", remote) |
| 94 | ctx := &uploadContext{ |
| 95 | Remote: remote, |
| 96 | Manifest: manifest, |
| 97 | DryRun: dryRun, |
| 98 | uploadedOids: tools.NewStringSet(), |
| 99 | gitfilter: lfs.NewGitFilter(cfg), |
| 100 | lockVerifier: newLockVerifier(manifest), |
| 101 | allowMissing: cfg.Git.Bool("lfs.allowincompletepush", false), |
| 102 | missing: make(map[string]string), |
| 103 | corrupt: make(map[string]string), |
| 104 | otherErrs: make([]error, 0), |
| 105 | } |
| 106 | |
| 107 | var sink io.Writer = os.Stdout |
| 108 | if dryRun { |
| 109 | sink = io.Discard |
| 110 | } |
| 111 | |
| 112 | ctx.logger = tasklog.NewLogger(sink, |
| 113 | tasklog.ForceProgress(cfg.ForceProgress()), |
| 114 | ) |
| 115 | ctx.meter = buildProgressMeter(ctx.DryRun, tq.Upload) |
| 116 | ctx.logger.Enqueue(ctx.meter) |
| 117 | ctx.committerName, ctx.committerEmail = cfg.CurrentCommitter() |
| 118 | return ctx |
| 119 | } |
| 120 | |
| 121 | func (c *uploadContext) NewQueue(options ...tq.Option) *tq.TransferQueue { |
| 122 | return tq.NewTransferQueue(tq.Upload, c.Manifest, c.Remote, append(options, |
| 123 | tq.DryRun(c.DryRun), |
| 124 | tq.WithProgress(c.meter), |
| 125 | tq.WithBatchSize(cfg.TransferBatchSize()), |
| 126 | )...) |
| 127 | } |
| 128 | |
| 129 | func (c *uploadContext) scannerError() error { |
| 130 | c.errMu.Lock() |
| 131 | defer c.errMu.Unlock() |
| 132 | |
| 133 | return c.scannerErr |
| 134 | } |
| 135 | |
| 136 | func (c *uploadContext) addScannerError(err error) { |
| 137 | c.errMu.Lock() |
| 138 | defer c.errMu.Unlock() |
| 139 | |
| 140 | c.scannerErr = errors.Join(c.scannerErr, err) |
| 141 | } |
| 142 | |
| 143 | func (c *uploadContext) buildGitScanner() *lfs.GitScanner { |
| 144 | return lfs.NewGitScannerForPush(cfg, c.Remote, func(n string) { c.lockVerifier.LockedByThem(n) }, c.lockVerifier) |
| 145 | } |
| 146 | |
| 147 | func (c *uploadContext) gitScannerCallback(tqueue *tq.TransferQueue) func(*lfs.WrappedPointer, error) { |
| 148 | return func(p *lfs.WrappedPointer, err error) { |
| 149 | if err != nil { |
| 150 | c.addScannerError(err) |
| 151 | } else { |
| 152 | c.UploadPointers(tqueue, p) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // AddUpload adds the given oid to the set of oids that have been uploaded in |
| 158 | // the current process. |
| 159 | func (c *uploadContext) SetUploaded(oid string) { |
| 160 | c.uploadedOids.Add(oid) |
| 161 | } |
| 162 | |
| 163 | // HasUploaded determines if the given oid has already been uploaded in the |
| 164 | // current process. |
| 165 | func (c *uploadContext) HasUploaded(oid string) bool { |
| 166 | return c.uploadedOids.Contains(oid) |
| 167 | } |
| 168 | |
| 169 | func (c *uploadContext) prepareUpload(unfiltered ...*lfs.WrappedPointer) []*lfs.WrappedPointer { |
| 170 | numUnfiltered := len(unfiltered) |
| 171 | uploadables := make([]*lfs.WrappedPointer, 0, numUnfiltered) |
| 172 | |
| 173 | // XXX(taylor): temporary measure to fix duplicate (broken) results from |
| 174 | // scanner |
| 175 | uniqOids := tools.NewStringSet() |
| 176 | |
| 177 | // Skip any objects which we've seen or already uploaded, as well |
| 178 | // as any which are locked by other users. |
| 179 | for _, p := range unfiltered { |
| 180 | // object already uploaded in this process, or we've already |
| 181 | // seen this OID (see above), skip! |
| 182 | if uniqOids.Contains(p.Oid) || c.HasUploaded(p.Oid) || p.Size == 0 { |
| 183 | continue |
| 184 | } |
| 185 | uniqOids.Add(p.Oid) |
| 186 | |
| 187 | // canUpload determines whether the current pointer "p" can be |
| 188 | // uploaded through the TransferQueue below. It is set to false |
| 189 | // only when the file is locked by someone other than the |
| 190 | // current committer. |
| 191 | var canUpload bool = true |
| 192 | |
| 193 | if c.lockVerifier.LockedByThem(p.Name) { |
| 194 | // If the verification state is enabled, this failed |
| 195 | // locks verification means that the push should fail. |
| 196 | // |
| 197 | // If the state is disabled, the verification error is |
| 198 | // silent and the user can upload. |
| 199 | // |
| 200 | // If the state is undefined, the verification error is |
| 201 | // sent as a warning and the user can upload. |
| 202 | canUpload = !c.lockVerifier.Enabled() |
| 203 | } |
| 204 | |
| 205 | c.lockVerifier.LockedByUs(p.Name) |
| 206 | |
| 207 | if canUpload { |
| 208 | // estimate in meter early (even if it's not going into |
| 209 | // uploadables), since we will call Skip() based on the |
| 210 | // results of the download check queue. |
| 211 | c.meter.Add(p.Size) |
| 212 | |
| 213 | uploadables = append(uploadables, p) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return uploadables |
| 218 | } |
| 219 | |
| 220 | func (c *uploadContext) UploadPointers(q *tq.TransferQueue, unfiltered ...*lfs.WrappedPointer) { |
| 221 | if c.DryRun { |
| 222 | for _, p := range unfiltered { |
| 223 | if c.HasUploaded(p.Oid) { |
| 224 | continue |
| 225 | } |
| 226 | |
| 227 | Print("%s %s => %s", tr.Tr.Get("push"), p.Oid, p.Name) |
| 228 | c.SetUploaded(p.Oid) |
| 229 | } |
| 230 | |
| 231 | return |
| 232 | } |
| 233 | |
| 234 | pointers := c.prepareUpload(unfiltered...) |
| 235 | for _, p := range pointers { |
| 236 | t, err := c.uploadTransfer(p) |
| 237 | if err != nil { |
| 238 | ExitWithError(err) |
| 239 | } |
| 240 | |
| 241 | q.Add(t.Name, t.Path, t.Oid, t.Size, t.Missing, nil) |
| 242 | c.SetUploaded(p.Oid) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func (c *uploadContext) CollectErrors(tqueue *tq.TransferQueue) { |
| 247 | tqueue.Wait() |
| 248 | |
| 249 | for _, err := range tqueue.Errors() { |
| 250 | if malformed, ok := err.(*tq.MalformedObjectError); ok { |
| 251 | if malformed.Missing() { |
| 252 | c.missing[malformed.Name] = malformed.Oid |
| 253 | } else if malformed.Corrupt() { |
| 254 | c.corrupt[malformed.Name] = malformed.Oid |
| 255 | } |
| 256 | } else { |
| 257 | c.otherErrs = append(c.otherErrs, err) |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func (c *uploadContext) ReportErrors() { |
| 263 | c.meter.Finish() |
| 264 | |
| 265 | for _, err := range c.otherErrs { |
| 266 | FullError(err) |
| 267 | } |
| 268 | |
| 269 | if len(c.missing) > 0 || len(c.corrupt) > 0 { |
| 270 | var action string |
| 271 | if c.allowMissing { |
| 272 | action = tr.Tr.Get("missing objects") |
| 273 | } else { |
| 274 | action = tr.Tr.Get("failed") |
| 275 | } |
| 276 | |
| 277 | Print(tr.Tr.Get("Git LFS upload %s:", action)) |
| 278 | for name, oid := range c.missing { |
| 279 | // TRANSLATORS: Leading spaces should be preserved. |
| 280 | Print(tr.Tr.Get(" (missing) %s (%s)", name, oid)) |
| 281 | } |
| 282 | for name, oid := range c.corrupt { |
| 283 | // TRANSLATORS: Leading spaces should be preserved. |
| 284 | Print(tr.Tr.Get(" (corrupt) %s (%s)", name, oid)) |
| 285 | } |
| 286 | |
| 287 | if !c.allowMissing { |
| 288 | pushMissingHint := []string{ |
| 289 | tr.Tr.Get("hint: Your push was rejected due to missing or corrupt local objects."), |
| 290 | tr.Tr.Get("hint: You can disable this check with: `git config lfs.allowincompletepush true`"), |
| 291 | } |
| 292 | Print(strings.Join(pushMissingHint, "\n")) |
| 293 | ExitWithCode(2) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if len(c.otherErrs) > 0 { |
| 298 | ExitWithCode(2) |
| 299 | } |
| 300 | |
| 301 | if c.lockVerifier.HasUnownedLocks() { |
| 302 | Print(tr.Tr.Get("Unable to push locked files:")) |
| 303 | for _, unowned := range c.lockVerifier.UnownedLocks() { |
| 304 | Print("* %s - %s", unowned.Path(), unowned.Owners()) |
| 305 | } |
| 306 | |
| 307 | if c.lockVerifier.Enabled() { |
| 308 | Exit(tr.Tr.Get("Cannot update locked files.")) |
| 309 | } else { |
| 310 | Error(tr.Tr.Get("warning: The above files would have halted this push.")) |
| 311 | } |
| 312 | } else if c.lockVerifier.HasOwnedLocks() { |
| 313 | Print(tr.Tr.Get("Consider unlocking your own locked files: (`git lfs unlock <path>`)")) |
| 314 | for _, owned := range c.lockVerifier.OwnedLocks() { |
| 315 | Print("* %s", owned.Path()) |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | var ( |
| 321 | githubHttps, _ = url.Parse("https://github.com") |
| 322 | githubSsh, _ = url.Parse("ssh://github.com") |
| 323 | |
| 324 | // hostsWithKnownLockingSupport is a list of scheme-less hostnames |
| 325 | // (without port numbers) that are known to implement the LFS locking |
| 326 | // API. |
| 327 | // |
| 328 | // Additions are welcome. |
| 329 | hostsWithKnownLockingSupport = []*url.URL{ |
| 330 | githubHttps, githubSsh, |
| 331 | } |
| 332 | ) |
| 333 | |
| 334 | func (c *uploadContext) uploadTransfer(p *lfs.WrappedPointer) (*tq.Transfer, error) { |
| 335 | var missing bool |
| 336 | |
| 337 | filename := p.Name |
| 338 | oid := p.Oid |
| 339 | |
| 340 | localMediaPath, err := c.gitfilter.ObjectPath(oid) |
| 341 | if err != nil { |
| 342 | return nil, errors.Wrap(err, tr.Tr.Get("Error uploading file %s (%s)", filename, oid)) |
| 343 | } |
| 344 | |
| 345 | // Skip the object if its corresponding file does not exist in |
| 346 | // .git/lfs/objects/. |
| 347 | if _, err := os.Stat(localMediaPath); err != nil { |
| 348 | if os.IsNotExist(err) { |
| 349 | missing = !c.allowMissing |
| 350 | } else { |
| 351 | return nil, errors.Wrap(err, tr.Tr.Get("Error uploading file %s (%s)", filename, oid)) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return &tq.Transfer{ |
| 356 | Name: filename, |
| 357 | Path: localMediaPath, |
| 358 | Oid: oid, |
| 359 | Size: p.Size, |
| 360 | Missing: missing, |
| 361 | }, nil |
| 362 | } |
| 363 | |
| 364 | // supportsLockingAPI returns whether or not a given url is known to support |
| 365 | // the LFS locking API by whether or not its hostname is included in the list |
| 366 | // above. |
| 367 | func supportsLockingAPI(rawurl string) bool { |
| 368 | u, err := url.Parse(rawurl) |
| 369 | if err != nil { |
| 370 | tracerx.Printf("commands: unable to parse %q to determine locking support: %v", rawurl, err) |
| 371 | return false |
| 372 | } |
| 373 | |
| 374 | for _, supported := range hostsWithKnownLockingSupport { |
| 375 | if supported.Scheme == u.Scheme && |
| 376 | supported.Hostname() == u.Hostname() && |
| 377 | strings.HasPrefix(u.Path, supported.Path) { |
| 378 | return true |
| 379 | } |
| 380 | } |
| 381 | return false |
| 382 | } |
| 383 | |
| 384 | // disableFor disables lock verification for the given lfsapi.Endpoint, |
| 385 | // "endpoint". |
| 386 | func disableFor(rawurl string) error { |
| 387 | tracerx.Printf("commands: disabling lock verification for %q", rawurl) |
| 388 | |
| 389 | key := strings.Join([]string{"lfs", rawurl, "locksverify"}, ".") |
| 390 | |
| 391 | _, err := cfg.SetGitLocalKey(key, "false") |
| 392 | return err |
| 393 | } |