LockFile attempts to lock a file on the current remote path must be relative to the root of the repository Returns the lock id if successful, or an error
(path string)
| 102 | // path must be relative to the root of the repository |
| 103 | // Returns the lock id if successful, or an error |
| 104 | func (c *Client) LockFile(path string) (Lock, error) { |
| 105 | lockRes, _, err := c.client.Lock(c.Remote, &lockRequest{ |
| 106 | Path: path, |
| 107 | Ref: &lockRef{Name: c.RemoteRef.Refspec()}, |
| 108 | }) |
| 109 | if err != nil { |
| 110 | return Lock{}, errors.Wrap(err, tr.Tr.Get("locking API")) |
| 111 | } |
| 112 | |
| 113 | if len(lockRes.Message) > 0 { |
| 114 | if len(lockRes.RequestID) > 0 { |
| 115 | tracerx.Printf("Server Request ID: %s", lockRes.RequestID) |
| 116 | } |
| 117 | return Lock{}, errors.New(tr.Tr.Get("server unable to create lock: %s", lockRes.Message)) |
| 118 | } |
| 119 | |
| 120 | lock := *lockRes.Lock |
| 121 | if err := c.cache.Add(lock); err != nil { |
| 122 | return Lock{}, errors.Wrap(err, tr.Tr.Get("lock cache")) |
| 123 | } |
| 124 | |
| 125 | abs, err := c.getAbsolutePath(path) |
| 126 | if err != nil { |
| 127 | return Lock{}, errors.Wrap(err, tr.Tr.Get("make lock path absolute")) |
| 128 | } |
| 129 | |
| 130 | // If the file exists, ensure that it's writeable on return |
| 131 | if tools.FileExists(abs) { |
| 132 | if err := tools.SetFileWriteFlag(abs, true); err != nil { |
| 133 | return Lock{}, errors.Wrap(err, tr.Tr.Get("set file write flag")) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return lock, nil |
| 138 | } |
| 139 | |
| 140 | // getAbsolutePath takes a repository-relative path and makes it absolute. |
| 141 | // |
no test coverage detected