| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "sort" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/git" |
| 11 | "github.com/git-lfs/git-lfs/v3/locking" |
| 12 | "github.com/git-lfs/git-lfs/v3/tr" |
| 13 | "github.com/spf13/cobra" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | locksCmdFlags = new(locksFlags) |
| 18 | ) |
| 19 | |
| 20 | func locksCommand(cmd *cobra.Command, args []string) { |
| 21 | lockData, err := computeLockData() |
| 22 | if err != nil { |
| 23 | ExitWithError(err) |
| 24 | } |
| 25 | filters, err := locksCmdFlags.Filters(lockData) |
| 26 | if err != nil { |
| 27 | Exit(tr.Tr.Get("Error building filters: %v", err)) |
| 28 | } |
| 29 | |
| 30 | if len(lockRemote) > 0 { |
| 31 | cfg.SetRemote(lockRemote) |
| 32 | cfg.SetPushRemote(lockRemote) |
| 33 | } |
| 34 | |
| 35 | refUpdate := git.NewRefUpdate(cfg.Git, cfg.PushRemote(), cfg.CurrentRef(), nil) |
| 36 | lockClient := newLockClient() |
| 37 | lockClient.RemoteRef = refUpdate.RemoteRef() |
| 38 | defer lockClient.Close() |
| 39 | |
| 40 | if locksCmdFlags.Cached { |
| 41 | if locksCmdFlags.Limit > 0 { |
| 42 | Exit(tr.Tr.Get("--cached option can't be combined with --limit")) |
| 43 | } |
| 44 | if len(filters) > 0 { |
| 45 | Exit(tr.Tr.Get("--cached option can't be combined with filters")) |
| 46 | } |
| 47 | if locksCmdFlags.Local { |
| 48 | Exit(tr.Tr.Get("--cached option can't be combined with --local")) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if locksCmdFlags.Verify { |
| 53 | if len(filters) > 0 { |
| 54 | Exit(tr.Tr.Get("--verify option can't be combined with filters")) |
| 55 | } |
| 56 | if locksCmdFlags.Local { |
| 57 | Exit(tr.Tr.Get("--verify option can't be combined with --local")) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | var locks []locking.Lock |
| 62 | var locksOwned map[locking.Lock]bool |
| 63 | var jsonWriteFunc func(io.Writer) error |
| 64 | if locksCmdFlags.Verify { |
| 65 | var ourLocks, theirLocks []locking.Lock |
| 66 | ourLocks, theirLocks, err = lockClient.SearchLocksVerifiable(locksCmdFlags.Limit, locksCmdFlags.Cached) |
| 67 | jsonWriteFunc = func(writer io.Writer) error { |
| 68 | return lockClient.EncodeLocksVerifiable(ourLocks, theirLocks, writer) |
| 69 | } |
| 70 | |
| 71 | locks = append(ourLocks, theirLocks...) |
| 72 | locksOwned = make(map[locking.Lock]bool) |
| 73 | for _, lock := range ourLocks { |
| 74 | locksOwned[lock] = true |
| 75 | } |
| 76 | } else { |
| 77 | locks, err = lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local, locksCmdFlags.Cached) |
| 78 | jsonWriteFunc = func(writer io.Writer) error { |
| 79 | return lockClient.EncodeLocks(locks, writer) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Print any we got before exiting |
| 84 | |
| 85 | if locksCmdFlags.JSON { |
| 86 | if err := jsonWriteFunc(os.Stdout); err != nil { |
| 87 | Error(err.Error()) |
| 88 | } |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | var maxPathLen int |
| 93 | var maxNameLen int |
| 94 | lockPaths := make([]string, 0, len(locks)) |
| 95 | locksByPath := make(map[string]locking.Lock) |
| 96 | for _, lock := range locks { |
| 97 | lockPaths = append(lockPaths, lock.Path) |
| 98 | locksByPath[lock.Path] = lock |
| 99 | maxPathLen = max(maxPathLen, len(lock.Path)) |
| 100 | if lock.Owner != nil { |
| 101 | maxNameLen = max(maxNameLen, len(lock.Owner.Name)) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | sort.Strings(lockPaths) |
| 106 | for _, lockPath := range lockPaths { |
| 107 | var ownerName string |
| 108 | lock := locksByPath[lockPath] |
| 109 | if lock.Owner != nil { |
| 110 | ownerName = lock.Owner.Name |
| 111 | } |
| 112 | |
| 113 | pathPadding := max(maxPathLen-len(lock.Path), 0) |
| 114 | namePadding := max(maxNameLen-len(ownerName), 0) |
| 115 | kind := "" |
| 116 | if locksOwned != nil { |
| 117 | if locksOwned[lock] { |
| 118 | kind = "O " |
| 119 | } else { |
| 120 | kind = " " |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | Print("%s%s%s\t%s%s\tID:%s", kind, lock.Path, strings.Repeat(" ", pathPadding), |
| 125 | ownerName, strings.Repeat(" ", namePadding), |
| 126 | lock.Id, |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | if err != nil { |
| 131 | Exit(tr.Tr.Get("Error while retrieving locks: %v", errors.Cause(err))) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // locksFlags wraps up and holds all of the flags that can be given to the |
| 136 | // `git lfs locks` command. |
| 137 | type locksFlags struct { |
| 138 | // Path is an optional filter parameter to filter against the lock's |
| 139 | // path |
| 140 | Path string |
| 141 | // Id is an optional filter parameter used to filtere against the lock's |
| 142 | // ID. |
| 143 | Id string |
| 144 | // limit is an optional request parameter sent to the server used to |
| 145 | // limit the |
| 146 | Limit int |
| 147 | // local limits the scope of lock reporting to the locally cached record |
| 148 | // of locks for the current user & doesn't query the server |
| 149 | Local bool |
| 150 | // JSON is an optional parameter to output data in json format. |
| 151 | JSON bool |
| 152 | // for non-local queries, report cached query results from the last query |
| 153 | // instead of actually querying the server again |
| 154 | Cached bool |
| 155 | // for non-local queries, verify lock owner on server and |
| 156 | // denote our locks in output |
| 157 | Verify bool |
| 158 | } |
| 159 | |
| 160 | // Filters produces a filter based on locksFlags instance. |
| 161 | func (l *locksFlags) Filters(data *lockData) (map[string]string, error) { |
| 162 | filters := make(map[string]string) |
| 163 | |
| 164 | if l.Path != "" { |
| 165 | path, err := lockPath(data, l.Path) |
| 166 | if err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | |
| 170 | filters["path"] = path |
| 171 | } |
| 172 | if l.Id != "" { |
| 173 | filters["id"] = l.Id |
| 174 | } |
| 175 | |
| 176 | return filters, nil |
| 177 | } |
| 178 | |
| 179 | func init() { |
| 180 | RegisterCommand("locks", locksCommand, func(cmd *cobra.Command) { |
| 181 | cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", "specify which remote to use when interacting with locks") |
| 182 | cmd.Flags().StringVarP(&locksCmdFlags.Path, "path", "p", "", "filter locks results matching a particular path") |
| 183 | cmd.Flags().StringVarP(&locksCmdFlags.Id, "id", "i", "", "filter locks results matching a particular ID") |
| 184 | cmd.Flags().IntVarP(&locksCmdFlags.Limit, "limit", "l", 0, "optional limit for number of results to return") |
| 185 | cmd.Flags().BoolVarP(&locksCmdFlags.Local, "local", "", false, "only list cached local record of own locks") |
| 186 | cmd.Flags().BoolVarP(&locksCmdFlags.Cached, "cached", "", false, "list cached lock information from the last remote query, instead of actually querying the server") |
| 187 | cmd.Flags().BoolVarP(&locksCmdFlags.Verify, "verify", "", false, "verify lock owner on server and mark own locks by 'O'") |
| 188 | cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "j", false, "Give the output in a stable JSON format for scripts") |
| 189 | }) |
| 190 | } |