Skip to content

Commit 86ce0c1

Browse files
authored
Merge pull request git-lfs#1978 from git-lfs/lock-messages
Improve lock messages
2 parents 6325aaf + eb304fa commit 86ce0c1

File tree

8 files changed

+89
-77
lines changed

8 files changed

+89
-77
lines changed

commands/command_lock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func lockCommand(cmd *cobra.Command, args []string) {
4242
return
4343
}
4444

45-
Print("\n'%s' was locked (%s)", args[0], lock.Id)
45+
Print("Locked %s", args[0])
4646
}
4747

4848
// lockPaths relativizes the given filepath such that it is relative to the root

commands/command_locks.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package commands
33
import (
44
"encoding/json"
55
"os"
6+
"sort"
7+
"strings"
68

9+
"github.com/git-lfs/git-lfs/locking"
10+
"github.com/git-lfs/git-lfs/tools"
711
"github.com/spf13/cobra"
812
)
913

@@ -20,7 +24,6 @@ func locksCommand(cmd *cobra.Command, args []string) {
2024
lockClient := newLockClient(lockRemote)
2125
defer lockClient.Close()
2226

23-
var lockCount int
2427
locks, err := lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local)
2528
// Print any we got before exiting
2629

@@ -31,15 +34,25 @@ func locksCommand(cmd *cobra.Command, args []string) {
3134
return
3235
}
3336

37+
var maxlen int
38+
lockPaths := make([]string, 0, len(locks))
39+
locksByPath := make(map[string]locking.Lock)
3440
for _, lock := range locks {
35-
Print("%s\t%s", lock.Path, lock.Owner)
36-
lockCount++
41+
lockPaths = append(lockPaths, lock.Path)
42+
locksByPath[lock.Path] = lock
43+
maxlen = tools.MaxInt(maxlen, len(lock.Path))
44+
}
45+
46+
sort.Strings(lockPaths)
47+
for _, lockPath := range lockPaths {
48+
lock := locksByPath[lockPath]
49+
padding := tools.MaxInt(maxlen-len(lock.Path), 0)
50+
Print("%s%s\t%s", lock.Path, strings.Repeat(" ", padding), lock.Owner)
3751
}
3852

3953
if err != nil {
4054
Exit("Error while retrieving locks: %v", err)
4155
}
42-
Print("\n%d lock(s) matched query.", lockCount)
4356
}
4457

4558
// locksFlags wraps up and holds all of the flags that can be given to the

commands/command_unlock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func unlockCommand(cmd *cobra.Command, args []string) {
6161
}
6262
return
6363
}
64-
Print("'%s' was unlocked", args[0])
64+
Print("Unlocked %s", args[0])
6565
}
6666

6767
func unlockAbortIfFileModified(path string) {

test/test-lock.sh

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ begin_test "creating a lock"
99
reponame="lock_create_simple"
1010
setup_remote_repo_with_file "$reponame" "a.dat"
1111

12-
git lfs lock "a.dat" | tee lock.log
13-
grep "'a.dat' was locked" lock.log
14-
15-
id=$(get_lock_id lock.log)
12+
git lfs lock --json "a.dat" | tee lock.json
13+
id=$(assert_lock lock.json a.dat)
1614
assert_server_lock "$reponame" "$id"
1715
)
1816
end_test
@@ -24,25 +22,22 @@ begin_test "create lock with server using client cert"
2422
setup_remote_repo_with_file "$reponame" "cc.dat"
2523

2624
git config lfs.url "$CLIENTCERTGITSERVER/$reponame.git/info/lfs"
27-
git lfs lock "cc.dat" | tee lock.log
28-
grep "'cc.dat' was locked" lock.log
29-
30-
id=$(get_lock_id lock.log)
25+
git lfs lock --json "cc.dat" | tee lock.json
26+
id=$(assert_lock lock.json cc.dat)
3127
assert_server_lock "$reponame" "$id"
3228
)
3329
end_test
3430

35-
begin_test "creating a lock (--json)"
31+
begin_test "creating a lock (with output)"
3632
(
3733
set -e
3834

39-
reponame="lock_create_simple_json"
40-
setup_remote_repo_with_file "$reponame" "a_json.dat"
35+
reponame="lock_create_simple_output"
36+
setup_remote_repo_with_file "$reponame" "a_output.dat"
4137

42-
git lfs lock --json "a_json.dat" | tee lock.log
43-
grep "\"path\":\"a_json.dat\"" lock.log
44-
45-
id=$(grep -o "\"id\":\".*\"" lock.log | cut -d \" -f 4)
38+
git lfs lock "a_output.dat" | tee lock.log
39+
grep "Locked a_output.dat" lock.log
40+
id=$(grep -oh "\((.*)\)" lock.log | tr -d \(\))
4641
assert_server_lock "$reponame" "$id"
4742
)
4843
end_test
@@ -54,10 +49,8 @@ begin_test "locking a previously locked file"
5449
reponame="lock_create_previously_created"
5550
setup_remote_repo_with_file "$reponame" "b.dat"
5651

57-
git lfs lock "b.dat" | tee lock.log
58-
grep "'b.dat' was locked" lock.log
59-
60-
id=$(get_lock_id lock.log)
52+
git lfs lock --json "b.dat" | tee lock.json
53+
id=$(assert_lock lock.json b.dat)
6154
assert_server_lock "$reponame" "$id"
6255

6356
grep "lock already created" <(git lfs lock "b.dat" 2>&1)

test/test-locks.sh

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ begin_test "list a single lock"
99
reponame="locks_list_single"
1010
setup_remote_repo_with_file "$reponame" "f.dat"
1111

12-
git lfs lock "f.dat" | tee lock.log
12+
git lfs lock --json "f.dat" | tee lock.log
1313

14-
id=$(get_lock_id lock.log)
14+
id=$(assert_lock lock.log f.dat)
1515
assert_server_lock "$reponame" "$id"
1616

1717
git lfs locks --path "f.dat" | tee locks.log
18-
grep "1 lock(s) matched query" locks.log
18+
[ $(wc -l < locks.log) -eq 1 ]
1919
grep "f.dat" locks.log
2020
grep "Git LFS Tests" locks.log
2121
)
@@ -28,9 +28,9 @@ begin_test "list a single lock (--json)"
2828
reponame="locks_list_single_json"
2929
setup_remote_repo_with_file "$reponame" "f_json.dat"
3030

31-
git lfs lock "f_json.dat" | tee lock.log
31+
git lfs lock --json "f_json.dat" | tee lock.log
3232

33-
id=$(get_lock_id lock.log)
33+
id=$(assert_lock lock.log f_json.dat)
3434
assert_server_lock "$reponame" "$id"
3535

3636
git lfs locks --json --path "f_json.dat" | tee locks.log
@@ -62,14 +62,14 @@ begin_test "list locks with a limit"
6262
git push origin master 2>&1 | tee push.log
6363
grep "master -> master" push.log
6464

65-
git lfs lock "g_1.dat" | tee lock.log
66-
assert_server_lock "$reponame" "$(get_lock_id "lock.log")"
65+
git lfs lock --json "g_1.dat" | tee lock.log
66+
assert_server_lock "$reponame" "$(assert_log "lock.log" g_1.dat)"
6767

68-
git lfs lock "g_2.dat" | tee lock.log
69-
assert_server_lock "$reponame" "$(get_lock_id "lock.log")"
68+
git lfs lock --json "g_2.dat" | tee lock.log
69+
assert_server_lock "$reponame" "$(assert_lock "lock.log" g_2.dat)"
7070

7171
git lfs locks --limit 1 | tee locks.log
72-
grep "1 lock(s) matched query" locks.log
72+
[ $(wc -l < locks.log) -eq 1 ]
7373
)
7474
end_test
7575

@@ -99,13 +99,13 @@ begin_test "list locks with pagination"
9999
grep "master -> master" push.log
100100

101101
for i in $(seq 1 5); do
102-
git lfs lock "h_$i.dat" | tee lock.log
103-
assert_server_lock "$reponame" "$(get_lock_id "lock.log")"
102+
git lfs lock --json "h_$i.dat" | tee lock.log
103+
assert_server_lock "$reponame" "$(assert_lock "lock.log" "h_$1.dat")"
104104
done
105105

106106
# The server will return, at most, three locks at a time
107107
git lfs locks --limit 4 | tee locks.log
108-
grep "4 lock(s) matched query" locks.log
108+
[ $(wc -l < locks.log) -eq 4 ]
109109
)
110110
end_test
111111

@@ -131,23 +131,23 @@ begin_test "cached locks"
131131
git push origin master 2>&1 | tee push.log
132132
grep "master -> master" push.log
133133

134-
git lfs lock "cached1.dat" | tee lock.log
135-
assert_server_lock "$(get_lock_id "lock.log")"
134+
git lfs lock --json "cached1.dat" | tee lock.log
135+
assert_server_lock "$(assert_lock "lock.log" cached1.dat)"
136136

137-
git lfs lock "cached2.dat" | tee lock.log
138-
assert_server_lock "$(get_lock_id "lock.log")"
137+
git lfs lock --json "cached2.dat" | tee lock.log
138+
assert_server_lock "$(assert_lock "lock.log" cached2.dat)"
139139

140140
git lfs locks --local | tee locks.log
141-
grep "2 lock(s) matched query" locks.log
141+
[ $(wc -l < locks.log) -eq 2 ]
142142

143143
# delete the remote to prove we're using the local records
144144
git remote remove origin
145145

146146
git lfs locks --local --path "cached1.dat" | tee locks.log
147-
grep "1 lock(s) matched query" locks.log
147+
[ $(wc -l < locks.log) -eq 1 ]
148148
grep "cached1.dat" locks.log
149149

150150
git lfs locks --local --limit 1 | tee locks.log
151-
grep "1 lock(s) matched query" locks.log
151+
[ $(wc -l < locks.log) -eq 1 ]
152152
)
153153
end_test

test/test-pre-push.sh

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,9 @@ begin_test "pre-push with our lock"
470470

471471
git push origin master
472472

473-
git lfs lock "locked.dat" | tee lock.log
474-
grep "'locked.dat' was locked" lock.log
473+
git lfs lock --json "locked.dat" | tee lock.log
475474

476-
id=$(get_lock_id lock.log)
475+
id=$(assert_lock lock.log locked.dat)
477476
assert_server_lock $id
478477

479478
printf "authorized changes" >> locked.dat
@@ -509,10 +508,8 @@ begin_test "pre-push with their lock on lfs file"
509508

510509
git push origin master
511510

512-
git lfs lock "locked_theirs.dat" | tee lock.log
513-
grep "'locked_theirs.dat' was locked" lock.log
514-
515-
id=$(get_lock_id lock.log)
511+
git lfs lock --json "locked_theirs.dat" | tee lock.log
512+
id=$(assert_lock lock.log locked_theirs.dat)
516513
assert_server_lock $id
517514

518515
pushd "$TRASHDIR" >/dev/null
@@ -552,14 +549,12 @@ begin_test "pre-push with their lock on non-lfs lockable file"
552549

553550
git push origin master
554551

555-
git lfs lock "tiny_locked_theirs.dat" | tee lock.log
556-
grep "'tiny_locked_theirs.dat' was locked" lock.log
557-
id=$(get_lock_id lock.log)
552+
git lfs lock --json "tiny_locked_theirs.dat" | tee lock.log
553+
id=$(assert_lock lock.log tiny_locked_theirs.dat)
558554
assert_server_lock $id
559555

560-
git lfs lock "large_locked_theirs.dat" | tee lock.log
561-
grep "'large_locked_theirs.dat' was locked" lock.log
562-
id=$(get_lock_id lock.log)
556+
git lfs lock --json "large_locked_theirs.dat" | tee lock.log
557+
id=$(assert_lock lock.log large_locked_theirs.dat)
563558
assert_server_lock $id
564559

565560
pushd "$TRASHDIR" >/dev/null

test/test-unlock.sh

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ begin_test "unlocking a lock by path"
99
reponame="unlock_by_path"
1010
setup_remote_repo_with_file "unlock_by_path" "c.dat"
1111

12-
git lfs lock "c.dat" | tee lock.log
12+
git lfs lock --json "c.dat" | tee lock.log
1313

14-
id=$(get_lock_id lock.log)
14+
id=$(assert_lock lock.log c.dat)
1515
assert_server_lock "$reponame" "$id"
1616

1717
git lfs unlock "c.dat" 2>&1 | tee unlock.log
@@ -26,8 +26,8 @@ begin_test "force unlocking lock with missing file"
2626
reponame="force-unlock-missing-file"
2727
setup_remote_repo_with_file "$reponame" "a.dat"
2828

29-
git lfs lock "a.dat" | tee lock.log
30-
id=$(get_lock_id lock.log)
29+
git lfs lock --json "a.dat" | tee lock.log
30+
id=$(assert_lock lock.log a.dat)
3131
assert_server_lock "$reponame" "$id"
3232

3333
git rm a.dat
@@ -52,9 +52,9 @@ begin_test "unlocking a lock (--json)"
5252
reponame="unlock_by_path_json"
5353
setup_remote_repo_with_file "$reponame" "c_json.dat"
5454

55-
git lfs lock "c_json.dat" | tee lock.log
55+
git lfs lock --json "c_json.dat" | tee lock.log
5656

57-
id=$(get_lock_id lock.log)
57+
id=$(assert_lock lock.log c_json.dat)
5858
assert_server_lock "$reponame" "$id"
5959

6060
git lfs unlock --json "c_json.dat" 2>&1 | tee unlock.log
@@ -71,9 +71,9 @@ begin_test "unlocking a lock by id"
7171
reponame="unlock_by_id"
7272
setup_remote_repo_with_file "unlock_by_id" "d.dat"
7373

74-
git lfs lock "d.dat" | tee lock.log
74+
git lfs lock --json "d.dat" | tee lock.log
7575

76-
id=$(get_lock_id lock.log)
76+
id=$(assert_lock lock.log d.dat)
7777
assert_server_lock "$reponame" "$id"
7878

7979
git lfs unlock --id="$id" 2>&1 | tee unlock.log
@@ -88,9 +88,9 @@ begin_test "unlocking a lock without sufficient info"
8888
reponame="unlock_ambiguous"
8989
setup_remote_repo_with_file "$reponame" "e.dat"
9090

91-
git lfs lock "e.dat" | tee lock.log
91+
git lfs lock --json "e.dat" | tee lock.log
9292

93-
id=$(get_lock_id lock.log)
93+
id=$(assert_lock lock.log e.dat)
9494
assert_server_lock "$reponame" "$id"
9595

9696
git lfs unlock 2>&1 | tee unlock.log
@@ -106,9 +106,9 @@ begin_test "unlocking a lock while uncommitted"
106106
reponame="unlock_modified"
107107
setup_remote_repo_with_file "$reponame" "mod.dat"
108108

109-
git lfs lock "mod.dat" | tee lock.log
109+
git lfs lock --json "mod.dat" | tee lock.log
110110

111-
id=$(get_lock_id lock.log)
111+
id=$(assert_lock lock.log mod.dat)
112112
assert_server_lock "$reponame" "$id"
113113

114114
echo "\nSomething" >> mod.dat
@@ -134,9 +134,9 @@ begin_test "unlocking a lock while uncommitted with --force"
134134
reponame="unlock_modified_force"
135135
setup_remote_repo_with_file "$reponame" "modforce.dat"
136136

137-
git lfs lock "modforce.dat" | tee lock.log
137+
git lfs lock --json "modforce.dat" | tee lock.log
138138

139-
id=$(get_lock_id lock.log)
139+
id=$(assert_lock lock.log modforce.dat)
140140
assert_server_lock "$reponame" "$id"
141141

142142
echo "\nSomething" >> modforce.dat
@@ -159,9 +159,9 @@ begin_test "unlocking a lock while untracked"
159159
# Create file but don't add it to git
160160
# Shouldn't be able to unlock it
161161
echo "something" > untracked.dat
162-
git lfs lock "untracked.dat" | tee lock.log
162+
git lfs lock --json "untracked.dat" | tee lock.log
163163

164-
id=$(get_lock_id lock.log)
164+
id=$(assert_lock lock.log untracked.dat)
165165
assert_server_lock "$reponame" "$id"
166166

167167
git lfs unlock "untracked.dat" 2>&1 | tee unlock.log

test/testhelpers.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,21 @@ assert_server_object() {
120120
}
121121
}
122122

123-
# Parses the Lock ID returned from a single 'git lfs lock <path>' call
124-
get_lock_id() {
123+
# This asserts the lock path and returns the lock ID by parsing the response of
124+
#
125+
# git lfs lock --json <path>
126+
assert_lock() {
125127
local log="$1"
126-
grep -oh "\((.*)\)" "$log" | tr -d \(\)
128+
local path="$2"
129+
130+
if [ $(grep -c "\"path\":\"$path\"" "$log") -eq 0 ]; then
131+
echo "path '$path' not found in:"
132+
cat "$log"
133+
exit 1
134+
fi
135+
136+
local jsonid=$(grep -oh "\"id\":\"\w\+\"" "$log")
137+
echo "${jsonid:3}" | tr -d \"\:
127138
}
128139

129140
# assert that a lock with the given ID exists on the test server

0 commit comments

Comments
 (0)