-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement add command and refactor some other files and tests
Signed-off-by: Valentin Kiselev <[email protected]>
- Loading branch information
Showing
16 changed files
with
391 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,5 @@ linters: | |
- unused | ||
- varcheck | ||
- whitespace | ||
- godot | ||
- godox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
package git | ||
|
||
// Repository is an interface to work with git repo. | ||
// It's realization might change | ||
type Repository interface { | ||
HooksPath() (string, error) | ||
RootPath() string | ||
GitPath() string | ||
OperationInProgress() bool | ||
type Repository struct { | ||
HooksPath string | ||
RootPath string | ||
GitPath string | ||
} | ||
|
||
func NewRepository() (Repository, error) { | ||
return NewGit2GoRepository() | ||
func NewRepository() (*Repository, error) { | ||
repo, err := NewGit2GoRepository() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hooksPath, err := repo.HooksPath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Repository{ | ||
HooksPath: hooksPath, | ||
RootPath: repo.RootPath(), | ||
GitPath: repo.GitPath(), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package lefthook | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/evilmartians/lefthook/pkg/config" | ||
) | ||
|
||
type AddArgs struct { | ||
Hook string | ||
|
||
CreateDirs, Force bool | ||
} | ||
|
||
func Add(opts *Options, args *AddArgs) error { | ||
lefthook, err := initialize(opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return lefthook.Add(args) | ||
} | ||
|
||
// Creates a hook, given in args. The hook is a Lefthook hook. | ||
func (l *Lefthook) Add(args *AddArgs) error { | ||
if !config.HookAvailable(args.Hook) { | ||
return fmt.Errorf("Skip adding, hook is unavailable: %s", args.Hook) | ||
} | ||
|
||
err := l.cleanHook(args.Hook, args.Force || l.Options.Force) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = l.addHook(args.Hook, "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if args.CreateDirs { | ||
global, local := l.getSourceDirs() | ||
|
||
sourceDir := filepath.Join(l.repo.RootPath, global, args.Hook) | ||
sourceDirLocal := filepath.Join(l.repo.RootPath, local, args.Hook) | ||
println("HI", sourceDir, sourceDirLocal) | ||
if err = l.Fs.MkdirAll(sourceDir, 0755); err != nil { | ||
return err | ||
} | ||
if err = l.Fs.MkdirAll(sourceDirLocal, 0755); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (l *Lefthook) getSourceDirs() (global, local string) { | ||
global = config.DefaultSourceDir | ||
local = config.DefaultSourceDirLocal | ||
|
||
cfg, err := config.Load(l.Fs, l.repo.RootPath) | ||
if err == nil { | ||
if len(cfg.SourceDir) > 0 { | ||
global = cfg.SourceDir | ||
} | ||
if len(cfg.SourceDirLocal) > 0 { | ||
local = cfg.SourceDirLocal | ||
} | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.