File: command_locks.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (190 lines) | stat: -rw-r--r-- 5,630 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package commands

import (
	"io"
	"os"
	"sort"
	"strings"

	"github.com/git-lfs/git-lfs/v3/errors"
	"github.com/git-lfs/git-lfs/v3/git"
	"github.com/git-lfs/git-lfs/v3/locking"
	"github.com/git-lfs/git-lfs/v3/tools"
	"github.com/git-lfs/git-lfs/v3/tr"
	"github.com/spf13/cobra"
)

var (
	locksCmdFlags = new(locksFlags)
)

func locksCommand(cmd *cobra.Command, args []string) {
	lockData, err := computeLockData()
	if err != nil {
		ExitWithError(err)
	}
	filters, err := locksCmdFlags.Filters(lockData)
	if err != nil {
		Exit(tr.Tr.Get("Error building filters: %v", err))
	}

	if len(lockRemote) > 0 {
		cfg.SetRemote(lockRemote)
	}

	refUpdate := git.NewRefUpdate(cfg.Git, cfg.PushRemote(), cfg.CurrentRef(), nil)
	lockClient := newLockClient()
	lockClient.RemoteRef = refUpdate.RemoteRef()
	defer lockClient.Close()

	if locksCmdFlags.Cached {
		if locksCmdFlags.Limit > 0 {
			Exit(tr.Tr.Get("--cached option can't be combined with --limit"))
		}
		if len(filters) > 0 {
			Exit(tr.Tr.Get("--cached option can't be combined with filters"))
		}
		if locksCmdFlags.Local {
			Exit(tr.Tr.Get("--cached option can't be combined with --local"))
		}
	}

	if locksCmdFlags.Verify {
		if len(filters) > 0 {
			Exit(tr.Tr.Get("--verify option can't be combined with filters"))
		}
		if locksCmdFlags.Local {
			Exit(tr.Tr.Get("--verify option can't be combined with --local"))
		}
	}

	var locks []locking.Lock
	var locksOwned map[locking.Lock]bool
	var jsonWriteFunc func(io.Writer) error
	if locksCmdFlags.Verify {
		var ourLocks, theirLocks []locking.Lock
		ourLocks, theirLocks, err = lockClient.SearchLocksVerifiable(locksCmdFlags.Limit, locksCmdFlags.Cached)
		jsonWriteFunc = func(writer io.Writer) error {
			return lockClient.EncodeLocksVerifiable(ourLocks, theirLocks, writer)
		}

		locks = append(ourLocks, theirLocks...)
		locksOwned = make(map[locking.Lock]bool)
		for _, lock := range ourLocks {
			locksOwned[lock] = true
		}
	} else {
		locks, err = lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local, locksCmdFlags.Cached)
		jsonWriteFunc = func(writer io.Writer) error {
			return lockClient.EncodeLocks(locks, writer)
		}
	}

	// Print any we got before exiting

	if locksCmdFlags.JSON {
		if err := jsonWriteFunc(os.Stdout); err != nil {
			Error(err.Error())
		}
		return
	}

	var maxPathLen int
	var maxNameLen int
	lockPaths := make([]string, 0, len(locks))
	locksByPath := make(map[string]locking.Lock)
	for _, lock := range locks {
		lockPaths = append(lockPaths, lock.Path)
		locksByPath[lock.Path] = lock
		maxPathLen = tools.MaxInt(maxPathLen, len(lock.Path))
		if lock.Owner != nil {
			maxNameLen = tools.MaxInt(maxNameLen, len(lock.Owner.Name))
		}
	}

	sort.Strings(lockPaths)
	for _, lockPath := range lockPaths {
		var ownerName string
		lock := locksByPath[lockPath]
		if lock.Owner != nil {
			ownerName = lock.Owner.Name
		}

		pathPadding := tools.MaxInt(maxPathLen-len(lock.Path), 0)
		namePadding := tools.MaxInt(maxNameLen-len(ownerName), 0)
		kind := ""
		if locksOwned != nil {
			if locksOwned[lock] {
				kind = "O "
			} else {
				kind = "  "
			}
		}

		Print("%s%s%s\t%s%s\tID:%s", kind, lock.Path, strings.Repeat(" ", pathPadding),
			ownerName, strings.Repeat(" ", namePadding),
			lock.Id,
		)
	}

	if err != nil {
		Exit(tr.Tr.Get("Error while retrieving locks: %v", errors.Cause(err)))
	}
}

// locksFlags wraps up and holds all of the flags that can be given to the
// `git lfs locks` command.
type locksFlags struct {
	// Path is an optional filter parameter to filter against the lock's
	// path
	Path string
	// Id is an optional filter parameter used to filtere against the lock's
	// ID.
	Id string
	// limit is an optional request parameter sent to the server used to
	// limit the
	Limit int
	// local limits the scope of lock reporting to the locally cached record
	// of locks for the current user & doesn't query the server
	Local bool
	// JSON is an optional parameter to output data in json format.
	JSON bool
	// for non-local queries, report cached query results from the last query
	// instead of actually querying the server again
	Cached bool
	// for non-local queries, verify lock owner on server and
	// denote our locks in output
	Verify bool
}

// Filters produces a filter based on locksFlags instance.
func (l *locksFlags) Filters(data *lockData) (map[string]string, error) {
	filters := make(map[string]string)

	if l.Path != "" {
		path, err := lockPath(data, l.Path)
		if err != nil {
			return nil, err
		}

		filters["path"] = path
	}
	if l.Id != "" {
		filters["id"] = l.Id
	}

	return filters, nil
}

func init() {
	RegisterCommand("locks", locksCommand, func(cmd *cobra.Command) {
		cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", "specify which remote to use when interacting with locks")
		cmd.Flags().StringVarP(&locksCmdFlags.Path, "path", "p", "", "filter locks results matching a particular path")
		cmd.Flags().StringVarP(&locksCmdFlags.Id, "id", "i", "", "filter locks results matching a particular ID")
		cmd.Flags().IntVarP(&locksCmdFlags.Limit, "limit", "l", 0, "optional limit for number of results to return")
		cmd.Flags().BoolVarP(&locksCmdFlags.Local, "local", "", false, "only list cached local record of own locks")
		cmd.Flags().BoolVarP(&locksCmdFlags.Cached, "cached", "", false, "list cached lock information from the last remote query, instead of actually querying the server")
		cmd.Flags().BoolVarP(&locksCmdFlags.Verify, "verify", "", false, "verify lock owner on server and mark own locks by 'O'")
		cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "", false, "print output in json")
	})
}