-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Skip unstaged changes for pre-commit hook (#402)
- Loading branch information
Showing
21 changed files
with
735 additions
and
296 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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package git | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type Exec interface { | ||
Cmd(cmd string) (string, error) | ||
CmdArgs(args ...string) (string, error) | ||
CmdLines(cmd string) ([]string, error) | ||
RawCmd(cmd string) (string, error) | ||
} | ||
|
||
type OsExec struct{} | ||
|
||
// NewOsExec returns an object that executes given commands | ||
// in the OS. | ||
func NewOsExec() *OsExec { | ||
return &OsExec{} | ||
} | ||
|
||
// Cmd runs plain string command. Trims spaces around output. | ||
func (o *OsExec) Cmd(cmd string) (string, error) { | ||
args := strings.Split(cmd, " ") | ||
return o.CmdArgs(args...) | ||
} | ||
|
||
// CmdLines runs plain string command, returns its output split by newline. | ||
func (o *OsExec) CmdLines(cmd string) ([]string, error) { | ||
out, err := o.RawCmd(cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return strings.Split(out, "\n"), nil | ||
} | ||
|
||
// CmdArgs runs a command provided with separted words. Trims spaces around output. | ||
func (o *OsExec) CmdArgs(args ...string) (string, error) { | ||
out, err := o.rawExecArgs(args...) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return strings.TrimSpace(out), nil | ||
} | ||
|
||
// RawCmd runs a plain string command returning unprocessed output as string. | ||
func (o *OsExec) RawCmd(cmd string) (string, error) { | ||
args := strings.Split(cmd, " ") | ||
return o.rawExecArgs(args...) | ||
} | ||
|
||
// rawExecArgs executes git command with LEFTHOOK=0 in order | ||
// to prevent calling subsequent lefthook hooks. | ||
func (o *OsExec) rawExecArgs(args ...string) (string, error) { | ||
cmd := exec.Command(args[0], args[1:]...) | ||
cmd.Env = append(os.Environ(), "LEFTHOOK=0") | ||
|
||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(out), 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
Oops, something went wrong.