Debian Codegraph @

commands/command_unlock.go

raw symbols SCIP
1 package commands
2
3 import (
4 "encoding/json"
5 "os"
6
7 "github.com/git-lfs/git-lfs/v3/errors"
8 "github.com/git-lfs/git-lfs/v3/git"
9 "github.com/git-lfs/git-lfs/v3/locking"
10 "github.com/git-lfs/git-lfs/v3/tr"
11 "github.com/spf13/cobra"
12 )
13
14 var (
15 unlockCmdFlags unlockFlags
16 )
17
18 // unlockFlags holds the flags given to the `git lfs unlock` command
19 type unlockFlags struct {
20 // Id is the Id of the lock that is being unlocked.
21 Id string
22 // Force specifies whether or not the `lfs unlock` command was invoked
23 // with "--force", signifying the user's intent to break another
24 // individual's lock(s).
25 Force bool
26 }
27
28 type unlockResponse struct {
29 Id string `json:"id,omitempty"`
30 Path string `json:"path,omitempty"`
31 Unlocked bool `json:"unlocked"`
32 Reason string `json:"reason,omitempty"`
33 }
34
35 func handleUnlockError(locks []unlockResponse, id string, path string, err error) []unlockResponse {
36 Error(err.Error())
37 if locksCmdFlags.JSON {
38 locks = append(locks, unlockResponse{
39 Id: id,
40 Path: path,
41 Unlocked: false,
42 Reason: err.Error(),
43 })
44 }
45 return locks
46 }
47
48 func unlockCommand(cmd *cobra.Command, args []string) {
49 hasPath := len(args) > 0
50 hasId := len(unlockCmdFlags.Id) > 0
51 if hasPath == hasId {
52 // If there is both an `--id` AND a `<path>`, or there is
53 // neither, print the usage and quit.
54 Exit(tr.Tr.Get("Exactly one of --id or a set of paths must be provided"))
55 }
56
57 if len(lockRemote) > 0 {
58 cfg.SetRemote(lockRemote)
59 cfg.SetPushRemote(lockRemote)
60 }
61
62 lockData, err := computeLockData()
63 if err != nil {
64 ExitWithError(err)
65 }
66
67 refUpdate := git.NewRefUpdate(cfg.Git, cfg.PushRemote(), cfg.CurrentRef(), nil)
68 lockClient := newLockClient()
69 lockClient.RemoteRef = refUpdate.RemoteRef()
70 defer lockClient.Close()
71
72 locks := make([]unlockResponse, 0, len(args))
73 success := true
74 if hasPath {
75 for _, pathspec := range args {
76 path, err := lockPath(lockData, pathspec)
77 if err != nil {
78 if !unlockCmdFlags.Force {
79 locks = handleUnlockError(locks, "", path, errors.New(tr.Tr.Get("Unable to determine path: %v", err.Error())))
80 success = false
81 continue
82 }
83 path = pathspec
84 }
85
86 if err := unlockAbortIfFileModified(path); err != nil {
87 locks = handleUnlockError(locks, "", path, err)
88 success = false
89 continue
90 }
91
92 err = lockClient.UnlockFile(path, unlockCmdFlags.Force)
93 if err != nil {
94 locks = handleUnlockError(locks, "", path, errors.Cause(err))
95 success = false
96 continue
97 }
98
99 if !locksCmdFlags.JSON {
100 Print(tr.Tr.Get("Unlocked %s", path))
101 continue
102 }
103 locks = append(locks, unlockResponse{
104 Path: path,
105 Unlocked: true,
106 })
107 }
108 } else if unlockCmdFlags.Id != "" {
109 // This call can early-out
110 unlockAbortIfFileModifiedById(unlockCmdFlags.Id, lockClient)
111
112 err := lockClient.UnlockFileById(unlockCmdFlags.Id, unlockCmdFlags.Force)
113 if err != nil {
114 locks = handleUnlockError(locks, unlockCmdFlags.Id, "", errors.New(tr.Tr.Get("Unable to unlock %v: %v", unlockCmdFlags.Id, errors.Cause(err))))
115 success = false
116 } else if !locksCmdFlags.JSON {
117 Print(tr.Tr.Get("Unlocked Lock %s", unlockCmdFlags.Id))
118 } else {
119 locks = append(locks, unlockResponse{
120 Id: unlockCmdFlags.Id,
121 Unlocked: true,
122 })
123 }
124 } else {
125 Exit(tr.Tr.Get("Exactly one of --id or a set of paths must be provided"))
126 }
127
128 if locksCmdFlags.JSON {
129 if err := json.NewEncoder(os.Stdout).Encode(locks); err != nil {
130 Error(err.Error())
131 }
132 }
133 if !success {
134 lockClient.Close()
135 ExitWithCode(2)
136 }
137 }
138
139 func unlockAbortIfFileModified(path string) error {
140 modified, err := git.IsFileModified(path)
141
142 if err != nil {
143 if unlockCmdFlags.Force {
144 // Since git/git@b9a7d55, `git-status(1)` causes an
145 // error when asked about files that don't exist,
146 // causing `err != nil`, as above.
147 //
148 // Unlocking a files that does not exist with
149 // --force is OK.
150 return nil
151 }
152 return err
153 }
154
155 if modified {
156 if unlockCmdFlags.Force {
157 // Only a warning
158 Error(tr.Tr.Get("warning: unlocking with uncommitted changes because --force"))
159 } else {
160 return errors.New(tr.Tr.Get("Cannot unlock file with uncommitted changes"))
161 }
162
163 }
164 return nil
165 }
166
167 func unlockAbortIfFileModifiedById(id string, lockClient *locking.Client) error {
168 // Get the path so we can check the status
169 filter := map[string]string{"id": id}
170 // try local cache first
171 locks, _ := lockClient.SearchLocks(filter, 0, true, false)
172 if len(locks) == 0 {
173 // Fall back on calling server
174 locks, _ = lockClient.SearchLocks(filter, 0, false, false)
175 }
176
177 if len(locks) == 0 {
178 // Don't block if we can't determine the path, may be cleaning up old data
179 return nil
180 }
181
182 return unlockAbortIfFileModified(locks[0].Path)
183 }
184
185 func init() {
186 RegisterCommand("unlock", unlockCommand, func(cmd *cobra.Command) {
187 cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", "specify which remote to use when interacting with locks")
188 cmd.Flags().StringVarP(&unlockCmdFlags.Id, "id", "i", "", "unlock a lock by its ID")
189 cmd.Flags().BoolVarP(&unlockCmdFlags.Force, "force", "f", false, "forcibly break another user's lock(s)")
190 cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "j", false, "Give the output in a stable JSON format for scripts")
191 })
192 }