-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from akm/features/add_skip_guard_option
Add --skip-guard option
- Loading branch information
Showing
7 changed files
with
142 additions
and
116 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
func add() error { | ||
uncommittedChanges, err := uncommittedChanges() | ||
if err != nil { | ||
return fmt.Errorf("git diff failed: %+v", err) | ||
} | ||
untrackedFiles, err := untrackedFiles() | ||
if err != nil { | ||
return fmt.Errorf("git ls-files failed: %+v", err) | ||
} | ||
|
||
if len(uncommittedChanges) == 0 && len(untrackedFiles) == 0 { | ||
return fmt.Errorf("No changes to commit and No untracked files") | ||
} | ||
|
||
if err := exec.Command("git", "add", ".").Run(); err != nil { | ||
return fmt.Errorf("git add failed: %+v", err) | ||
} | ||
|
||
return 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,25 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
func commit(commitMessage *commitMessage) error { | ||
// 3. "git commit" を以下のオプションと標準力を指定して実行する。 | ||
msg, err := commitMessage.Build() | ||
if err != nil { | ||
return fmt.Errorf("Failed to build commit message: %+v", err) | ||
} | ||
|
||
// See https://tracpath.com/docs/git-commit/ | ||
commitCmd := exec.Command("git", "commit", "--file", "-") | ||
commitCmd.Stdin = bytes.NewBufferString(msg) | ||
|
||
if err := commitCmd.Run(); err != nil { | ||
return fmt.Errorf("git commit failed: %+v", err) | ||
} | ||
|
||
return 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
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,62 +1,62 @@ | ||
package main | ||
|
||
type guardError struct { | ||
message string | ||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type guardResult struct { | ||
uncommittedChanges string | ||
untrackedFiles string | ||
skipped bool | ||
} | ||
|
||
func (e *guardError) Error() string { | ||
return e.message | ||
} | ||
|
||
func isGuardError(err error) bool { | ||
_, ok := err.(*guardError) | ||
return ok | ||
} | ||
|
||
func guard() error { | ||
// 環境変数 GIT_EXEC_SKIP_GUARD, GIT_EXEC_SKIP_GUARD_UNCOMMITTED_CHANGES, GIT_EXEC_SKIP_GUARD_UNTRACKED_FILES には | ||
// 以下の値で真偽値を表す文字列を想定する | ||
// true: "true", "1", "yes", "on" | ||
// false: "false", "0", "no", "off" | ||
// 空文字列 あるいは それ以外の文字列は false として扱う | ||
// | ||
// GIT_EXEC_SKIP_GUARD あるいは GIT_EXEC_SKIP_GUARD_UNCOMMITTED_CHANGES のいずれかが true でなければ、コミットされていない変更があればエラーを返す | ||
// GIT_EXEC_SKIP_GUARD あるいは GIT_EXEC_SKIP_GUARD_UNTRACKED_FILES のいずれかが true でなければ、追跡されていないファイルがあればエラーを返す | ||
|
||
if err := guardUncommittedChanges(); err != nil { | ||
return err | ||
func (g *guardResult) Message() string { | ||
var r string | ||
if len(g.uncommittedChanges) > 0 && len(g.untrackedFiles) > 0 { | ||
r = "There are uncommitted changes and untracked files" | ||
} else if len(g.untrackedFiles) > 0 { | ||
r = "There are untracked files" | ||
} else { | ||
r = "There are uncommitted changes" | ||
} | ||
if err := guardUntrackedFiles(); err != nil { | ||
return err | ||
if g.skipped { | ||
r += " but guard was skipped by options" | ||
} | ||
|
||
return nil | ||
return r | ||
} | ||
|
||
func guardUncommittedChanges() error { | ||
if getEnvBool("GIT_EXEC_SKIP_GUARD") || getEnvBool("GIT_EXEC_SKIP_GUARD_UNCOMMITTED_CHANGES") { | ||
return nil | ||
func (g *guardResult) Format() string { | ||
parts := []string{g.Message()} | ||
if len(g.uncommittedChanges) > 0 { | ||
parts = append(parts, fmt.Sprintf("Uncommitted changes:\n%s", g.uncommittedChanges)) | ||
} | ||
diff, err := hasUncommittedChanges() | ||
if err != nil { | ||
return err | ||
if len(g.untrackedFiles) > 0 { | ||
parts = append(parts, fmt.Sprintf("Untracked files:\n%s", g.untrackedFiles)) | ||
} | ||
if diff { | ||
return &guardError{"There are uncommitted changes"} | ||
} | ||
return nil | ||
return strings.Join(parts, "\n\n") | ||
} | ||
|
||
func guardUntrackedFiles() error { | ||
if getEnvBool("GIT_EXEC_SKIP_GUARD") || getEnvBool("GIT_EXEC_SKIP_GUARD_UNTRACKED_FILES") { | ||
return nil | ||
func guard(opts *Options) (*guardResult, error) { | ||
diff, err := uncommittedChanges() | ||
if err != nil { | ||
return nil, err | ||
} | ||
r, err := hasUntrackedFiles() | ||
|
||
untrackedFiles, err := untrackedFiles() | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
if r { | ||
return &guardError{"There are untracked files"} | ||
|
||
if len(diff) == 0 && len(untrackedFiles) == 0 { | ||
return nil, nil | ||
} | ||
return nil | ||
|
||
return &guardResult{ | ||
uncommittedChanges: diff, | ||
untrackedFiles: untrackedFiles, | ||
skipped: opts.SkipGuard || | ||
(opts.SkipGuardUncommittedChanges && len(diff) > 0) || | ||
(opts.SkipGuardUntrackedFiles && len(untrackedFiles) > 0), | ||
}, 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
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