locking: remove dependency on config.Configuration

This commit is contained in:
risk danger olson 2017-01-03 13:38:15 -07:00
parent d1efa4d69e
commit f1da0fb8e2
6 changed files with 13 additions and 15 deletions

@ -17,7 +17,6 @@ var (
) )
func lockCommand(cmd *cobra.Command, args []string) { func lockCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
Print("Usage: git lfs lock <path>") Print("Usage: git lfs lock <path>")
return return
@ -32,7 +31,7 @@ func lockCommand(cmd *cobra.Command, args []string) {
cfg.CurrentRemote = lockRemote cfg.CurrentRemote = lockRemote
} }
lockClient, err := locking.NewClient(lockRemote, APIClient(), cfg) lockClient, err := locking.NewClient(lockRemote, APIClient())
if err != nil { if err != nil {
Exit("Unable to create lock system: %v", err.Error()) Exit("Unable to create lock system: %v", err.Error())
} }

@ -10,7 +10,6 @@ var (
) )
func locksCommand(cmd *cobra.Command, args []string) { func locksCommand(cmd *cobra.Command, args []string) {
filters, err := locksCmdFlags.Filters() filters, err := locksCmdFlags.Filters()
if err != nil { if err != nil {
Exit("Error building filters: %v", err) Exit("Error building filters: %v", err)
@ -19,7 +18,7 @@ func locksCommand(cmd *cobra.Command, args []string) {
if len(lockRemote) > 0 { if len(lockRemote) > 0 {
cfg.CurrentRemote = lockRemote cfg.CurrentRemote = lockRemote
} }
lockClient, err := locking.NewClient(lockRemote, APIClient(), cfg) lockClient, err := locking.NewClient(lockRemote, APIClient())
if err != nil { if err != nil {
Exit("Unable to create lock system: %v", err.Error()) Exit("Unable to create lock system: %v", err.Error())
} }

@ -21,12 +21,11 @@ type unlockFlags struct {
} }
func unlockCommand(cmd *cobra.Command, args []string) { func unlockCommand(cmd *cobra.Command, args []string) {
if len(lockRemote) > 0 { if len(lockRemote) > 0 {
cfg.CurrentRemote = lockRemote cfg.CurrentRemote = lockRemote
} }
lockClient, err := locking.NewClient(lockRemote, APIClient(), cfg) lockClient, err := locking.NewClient(lockRemote, APIClient())
if err != nil { if err != nil {
Exit("Unable to create lock system: %v", err.Error()) Exit("Unable to create lock system: %v", err.Error())
} }

@ -161,6 +161,12 @@ func (c *Client) httpClient(host string) *http.Client {
return httpClient return httpClient
} }
func (c *Client) CurrentUser() (string, string) {
userName, _ := c.gitEnv.Get("user.name")
userEmail, _ := c.gitEnv.Get("user.email")
return userName, userEmail
}
func newRequestForRetry(req *http.Request, location string) (*http.Request, error) { func newRequestForRetry(req *http.Request, location string) (*http.Request, error) {
newReq, err := http.NewRequest(req.Method, location, nil) newReq, err := http.NewRequest(req.Method, location, nil)
if err != nil { if err != nil {

@ -27,14 +27,13 @@ var (
type Client struct { type Client struct {
Remote string Remote string
client *lockClient client *lockClient
cfg *config.Configuration
cache *LockCache cache *LockCache
} }
// NewClient creates a new locking client with the given configuration // NewClient creates a new locking client with the given configuration
// You must call the returned object's `Close` method when you are finished with // You must call the returned object's `Close` method when you are finished with
// it // it
func NewClient(remote string, lfsClient *lfsapi.Client, cfg *config.Configuration) (*Client, error) { func NewClient(remote string, lfsClient *lfsapi.Client) (*Client, error) {
lockDir := filepath.Join(config.LocalGitStorageDir, "lfs") lockDir := filepath.Join(config.LocalGitStorageDir, "lfs")
err := os.MkdirAll(lockDir, 0755) err := os.MkdirAll(lockDir, 0755)
if err != nil { if err != nil {
@ -49,7 +48,6 @@ func NewClient(remote string, lfsClient *lfsapi.Client, cfg *config.Configuratio
return &Client{ return &Client{
Remote: remote, Remote: remote,
client: &lockClient{Client: lfsClient}, client: &lockClient{Client: lfsClient},
cfg: cfg,
cache: cache, cache: cache,
}, nil }, nil
} }
@ -72,7 +70,7 @@ func (c *Client) LockFile(path string) (Lock, error) {
lockReq := &lockRequest{ lockReq := &lockRequest{
Path: path, Path: path,
LatestRemoteCommit: latest.Sha, LatestRemoteCommit: latest.Sha,
Committer: newCommitter(c.cfg.CurrentCommitter()), Committer: newCommitter(c.client.CurrentUser()),
} }
lockRes, _, err := c.client.Lock(c.Remote, lockReq) lockRes, _, err := c.client.Lock(c.Remote, lockReq)
@ -96,7 +94,6 @@ func (c *Client) LockFile(path string) (Lock, error) {
// path must be relative to the root of the repository // path must be relative to the root of the repository
// Force causes the file to be unlocked from other users as well // Force causes the file to be unlocked from other users as well
func (c *Client) UnlockFile(path string, force bool) error { func (c *Client) UnlockFile(path string, force bool) error {
id, err := c.lockIdFromPath(path) id, err := c.lockIdFromPath(path)
if err != nil { if err != nil {
return fmt.Errorf("Unable to get lock id: %v", err) return fmt.Errorf("Unable to get lock id: %v", err)
@ -262,7 +259,7 @@ func (c *Client) refreshLockCache() error {
// We're going to overwrite the entire local cache // We're going to overwrite the entire local cache
c.cache.Clear() c.cache.Clear()
_, email := c.cfg.CurrentCommitter() _, email := c.client.CurrentUser()
for _, l := range locks { for _, l := range locks {
if l.Email == email { if l.Email == email {
c.cache.Add(l) c.cache.Add(l)

@ -51,8 +51,6 @@ func TestRefreshCache(t *testing.T) {
config.LocalGitStorageDir = oldStore config.LocalGitStorageDir = oldStore
}() }()
cfg := config.NewFrom(config.Values{
Git: map[string]string{"user.name": "Fred", "user.email": "fred@bloggs.com"}})
lfsclient, err := lfsapi.NewClient(nil, lfsapi.Env(map[string]string{ lfsclient, err := lfsapi.NewClient(nil, lfsapi.Env(map[string]string{
"lfs.url": srv.URL + "/api", "lfs.url": srv.URL + "/api",
"user.name": "Fred", "user.name": "Fred",
@ -60,7 +58,7 @@ func TestRefreshCache(t *testing.T) {
})) }))
require.Nil(t, err) require.Nil(t, err)
client, err := NewClient("", lfsclient, cfg) client, err := NewClient("", lfsclient)
assert.Nil(t, err) assert.Nil(t, err)
// Should start with no cached items // Should start with no cached items