| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/config" |
| 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/lfshttp" |
| 13 | "github.com/git-lfs/git-lfs/v3/locking" |
| 14 | "github.com/git-lfs/git-lfs/v3/tq" |
| 15 | "github.com/git-lfs/git-lfs/v3/tr" |
| 16 | "github.com/rubyist/tracerx" |
| 17 | ) |
| 18 | |
| 19 | type verifyState byte |
| 20 | |
| 21 | const ( |
| 22 | verifyStateUnknown verifyState = iota |
| 23 | verifyStateEnabled |
| 24 | verifyStateDisabled |
| 25 | ) |
| 26 | |
| 27 | func verifyLocksForUpdates(lv *lockVerifier, updates []*git.RefUpdate) { |
| 28 | for _, update := range updates { |
| 29 | lv.Verify(update.RemoteRef()) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // lockVerifier verifies locked files before updating one or more refs. |
| 34 | type lockVerifier struct { |
| 35 | endpoint lfshttp.Endpoint |
| 36 | verifyState verifyState |
| 37 | verifiedRefs map[string]bool |
| 38 | |
| 39 | // all existing locks |
| 40 | ourLocks map[string]*refLock |
| 41 | theirLocks map[string]*refLock |
| 42 | |
| 43 | // locks from ourLocks that have been modified |
| 44 | ownedLocks []*refLock |
| 45 | |
| 46 | // locks from theirLocks that have been modified |
| 47 | unownedLocks []*refLock |
| 48 | } |
| 49 | |
| 50 | func (lv *lockVerifier) Verify(ref *git.Ref) { |
| 51 | if ref == nil { |
| 52 | panic(tr.Tr.Get("no ref specified for verification")) |
| 53 | } |
| 54 | |
| 55 | if lv.verifyState == verifyStateDisabled || lv.verifiedRefs[ref.Refspec()] { |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | tracerx.Printf("verifying locks for %s", ref.Name) |
| 60 | |
| 61 | lockClient := newLockClient() |
| 62 | lockClient.RemoteRef = ref |
| 63 | ours, theirs, err := lockClient.SearchLocksVerifiable(0, false) |
| 64 | if err != nil { |
| 65 | if errors.IsNotImplementedError(err) { |
| 66 | disableFor(lv.endpoint.Url) |
| 67 | } else if lv.verifyState == verifyStateUnknown || lv.verifyState == verifyStateEnabled { |
| 68 | if errors.IsAuthError(err) { |
| 69 | if lv.verifyState == verifyStateUnknown { |
| 70 | Error(tr.Tr.Get("warning: Authentication error: %s", err)) |
| 71 | } else if lv.verifyState == verifyStateEnabled { |
| 72 | Exit(tr.Tr.Get("error: Authentication error: %s", err)) |
| 73 | } |
| 74 | } else { |
| 75 | Error(tr.Tr.Get("Remote %q does not support the Git LFS locking API. Consider disabling it with:", cfg.PushRemote())) |
| 76 | Error(" $ git config lfs.%s.locksverify false", lv.endpoint.Url) |
| 77 | if lv.verifyState == verifyStateEnabled { |
| 78 | ExitWithError(err) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } else if lv.verifyState == verifyStateUnknown { |
| 83 | Error(tr.Tr.Get("Locking support detected on remote %q. Consider enabling it with:", cfg.PushRemote())) |
| 84 | Error(" $ git config lfs.%s.locksverify true", lv.endpoint.Url) |
| 85 | } |
| 86 | |
| 87 | lv.addLocks(ref, ours, lv.ourLocks) |
| 88 | lv.addLocks(ref, theirs, lv.theirLocks) |
| 89 | lv.verifiedRefs[ref.Refspec()] = true |
| 90 | |
| 91 | tracerx.Printf("verified locks for %s", ref.Name) |
| 92 | } |
| 93 | |
| 94 | func (lv *lockVerifier) addLocks(ref *git.Ref, locks []locking.Lock, set map[string]*refLock) { |
| 95 | for _, l := range locks { |
| 96 | if rl, ok := set[l.Path]; ok { |
| 97 | if err := rl.Add(ref, l); err != nil { |
| 98 | Error(tr.Tr.Get("warning: error adding %q lock for ref %q: %+v", l.Path, ref.Refspec(), err)) |
| 99 | } |
| 100 | } else { |
| 101 | set[l.Path] = lv.newRefLocks(ref, l) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Determines if a filename is lockable. Implements lfs.GitScannerSet |
| 107 | func (lv *lockVerifier) Contains(name string) bool { |
| 108 | if lv == nil { |
| 109 | return false |
| 110 | } |
| 111 | _, ok := lv.theirLocks[name] |
| 112 | return ok |
| 113 | } |
| 114 | |
| 115 | func (lv *lockVerifier) LockedByThem(name string) bool { |
| 116 | if lock, ok := lv.theirLocks[name]; ok { |
| 117 | lv.unownedLocks = append(lv.unownedLocks, lock) |
| 118 | return true |
| 119 | } |
| 120 | return false |
| 121 | } |
| 122 | |
| 123 | func (lv *lockVerifier) LockedByUs(name string) bool { |
| 124 | if lock, ok := lv.ourLocks[name]; ok { |
| 125 | lv.ownedLocks = append(lv.ownedLocks, lock) |
| 126 | return true |
| 127 | } |
| 128 | return false |
| 129 | } |
| 130 | |
| 131 | func (lv *lockVerifier) UnownedLocks() []*refLock { |
| 132 | return lv.unownedLocks |
| 133 | } |
| 134 | |
| 135 | func (lv *lockVerifier) HasUnownedLocks() bool { |
| 136 | return len(lv.unownedLocks) > 0 |
| 137 | } |
| 138 | |
| 139 | func (lv *lockVerifier) OwnedLocks() []*refLock { |
| 140 | return lv.ownedLocks |
| 141 | } |
| 142 | |
| 143 | func (lv *lockVerifier) HasOwnedLocks() bool { |
| 144 | return len(lv.ownedLocks) > 0 |
| 145 | } |
| 146 | |
| 147 | func (lv *lockVerifier) Enabled() bool { |
| 148 | return lv.verifyState == verifyStateEnabled |
| 149 | } |
| 150 | |
| 151 | func (lv *lockVerifier) newRefLocks(ref *git.Ref, l locking.Lock) *refLock { |
| 152 | return &refLock{ |
| 153 | allRefs: lv.verifiedRefs, |
| 154 | path: l.Path, |
| 155 | refs: map[*git.Ref]locking.Lock{ref: l}, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func newLockVerifier(m tq.Manifest) *lockVerifier { |
| 160 | lv := &lockVerifier{ |
| 161 | endpoint: getAPIClient().Endpoints.Endpoint("upload", cfg.PushRemote()), |
| 162 | verifiedRefs: make(map[string]bool), |
| 163 | ourLocks: make(map[string]*refLock), |
| 164 | theirLocks: make(map[string]*refLock), |
| 165 | } |
| 166 | |
| 167 | // Do not check locks for standalone transfer, because there is no LFS |
| 168 | // server to ask. |
| 169 | if m.IsStandaloneTransfer() { |
| 170 | lv.verifyState = verifyStateDisabled |
| 171 | } else { |
| 172 | lv.verifyState = getVerifyStateFor(lv.endpoint.Url) |
| 173 | } |
| 174 | |
| 175 | return lv |
| 176 | } |
| 177 | |
| 178 | // refLock represents a unique locked file path, potentially across multiple |
| 179 | // refs. It tracks each individual lock in case different users locked the |
| 180 | // same path across multiple refs. |
| 181 | type refLock struct { |
| 182 | path string |
| 183 | allRefs map[string]bool |
| 184 | refs map[*git.Ref]locking.Lock |
| 185 | } |
| 186 | |
| 187 | // Path returns the locked path. |
| 188 | func (r *refLock) Path() string { |
| 189 | return r.path |
| 190 | } |
| 191 | |
| 192 | // Owners returns the list of owners that locked this file, including what |
| 193 | // specific refs the files were locked in. If a user locked a file on all refs, |
| 194 | // don't bother listing them. |
| 195 | // |
| 196 | // Example: technoweenie, bob (refs: foo) |
| 197 | func (r *refLock) Owners() string { |
| 198 | users := make(map[string][]string, len(r.refs)) |
| 199 | for ref, lock := range r.refs { |
| 200 | u := lock.Owner.Name |
| 201 | if _, ok := users[u]; !ok { |
| 202 | users[u] = make([]string, 0, len(r.refs)) |
| 203 | } |
| 204 | users[u] = append(users[u], ref.Name) |
| 205 | } |
| 206 | |
| 207 | owners := make([]string, 0, len(users)) |
| 208 | for name, refs := range users { |
| 209 | seenRefCount := 0 |
| 210 | for _, ref := range refs { |
| 211 | if r.allRefs[ref] { |
| 212 | seenRefCount++ |
| 213 | } |
| 214 | } |
| 215 | if seenRefCount == len(r.allRefs) { // lock is included in all refs, so don't list them |
| 216 | owners = append(owners, name) |
| 217 | continue |
| 218 | } |
| 219 | |
| 220 | sort.Strings(refs) |
| 221 | owners = append(owners, fmt.Sprintf("%s (refs: %s)", name, strings.Join(refs, ", "))) |
| 222 | } |
| 223 | sort.Strings(owners) |
| 224 | return strings.Join(owners, ", ") |
| 225 | } |
| 226 | |
| 227 | func (r *refLock) Add(ref *git.Ref, l locking.Lock) error { |
| 228 | r.refs[ref] = l |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | // getVerifyStateFor returns whether or not lock verification is enabled for the |
| 233 | // given url. If no state has been explicitly set, an "unknown" state will be |
| 234 | // returned instead. |
| 235 | func getVerifyStateFor(rawurl string) verifyState { |
| 236 | uc := config.NewURLConfig(cfg.Git) |
| 237 | |
| 238 | v, ok := uc.Get("lfs", rawurl, "locksverify") |
| 239 | if !ok { |
| 240 | if supportsLockingAPI(rawurl) { |
| 241 | return verifyStateEnabled |
| 242 | } |
| 243 | return verifyStateUnknown |
| 244 | } |
| 245 | |
| 246 | if enabled, _ := strconv.ParseBool(v); enabled { |
| 247 | return verifyStateEnabled |
| 248 | } |
| 249 | return verifyStateDisabled |
| 250 | } |