diff --git a/commands/command_locks.go b/commands/command_locks.go index 6b8e8958..423b92ad 100644 --- a/commands/command_locks.go +++ b/commands/command_locks.go @@ -1,7 +1,7 @@ package commands import ( - "encoding/json" + "io" "os" "sort" "strings" @@ -44,11 +44,44 @@ func locksCommand(cmd *cobra.Command, args []string) { } } - locks, err := lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local, locksCmdFlags.Cached) + if locksCmdFlags.Verify { + if len(filters) > 0 { + Exit("--verify option can't be combined with filters") + } + if locksCmdFlags.Local { + Exit("--verify option can't be combined with --local") + } + if locksCmdFlags.Cached { + Exit("--verify option can't be combined with --cached") + } + } + + 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) + 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 := json.NewEncoder(os.Stdout).Encode(locks); err != nil { + if err := jsonWriteFunc(os.Stdout); err != nil { Error(err.Error()) } return @@ -77,7 +110,16 @@ func locksCommand(cmd *cobra.Command, args []string) { pathPadding := tools.MaxInt(maxPathLen-len(lock.Path), 0) namePadding := tools.MaxInt(maxNameLen-len(ownerName), 0) - Print("%s%s\t%s%s\tID:%s", lock.Path, strings.Repeat(" ", pathPadding), + 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, ) @@ -108,6 +150,9 @@ type locksFlags struct { // 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. @@ -137,6 +182,7 @@ func init() { 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") }) } diff --git a/docs/man/git-lfs-locks.1.ronn b/docs/man/git-lfs-locks.1.ronn index 49d96562..99ffd6d9 100644 --- a/docs/man/git-lfs-locks.1.ronn +++ b/docs/man/git-lfs-locks.1.ronn @@ -29,6 +29,16 @@ Lists current locks from the Git LFS server. last known locks in case you are offline. There is no guarantee that locks on the server have not changed in the meanwhile. +* `--verify`: + Verifies the lock owner on the server and marks our own locks by 'O'. + Own locks are actually held by us and corresponding files can be updated for + the next push. All other locks are held by someone else. + Contrary to --local, this option will also detect locks which are held by us + despite no local lock information being available (e.g. because the file had + been locked from a different clone); + it will also detect 'broken' locks (e.g. if someone else has forcefully + unlocked our files). + * `-l ` `--limit=`: Specifies number of results to return. diff --git a/locking/locks.go b/locking/locks.go index f0ba09c1..0660b825 100644 --- a/locking/locks.go +++ b/locking/locks.go @@ -3,6 +3,7 @@ package locking import ( "encoding/json" "fmt" + "io" "net/http" "os" "path/filepath" @@ -468,13 +469,24 @@ func (c *Client) readLocksFromCacheFile(path string) ([]Lock, error) { return locks, nil } +func (c *Client) EncodeLocks(locks []Lock, writer io.Writer) error { + return json.NewEncoder(writer).Encode(locks) +} + +func (c *Client) EncodeLocksVerifiable(ourLocks, theirLocks []Lock, writer io.Writer) error { + return json.NewEncoder(writer).Encode(&lockVerifiableList{ + Ours: ourLocks, + Theirs: theirLocks, + }) +} + func (c *Client) writeLocksToCacheFile(path string, locks []Lock) error { file, err := os.Create(path) if err != nil { return err } - err = json.NewEncoder(file).Encode(locks) + err = c.EncodeLocks(locks, file) if err != nil { file.Close() return err