forked from toolshed/abra
@ -45,3 +45,39 @@ func Commit(repoPath, commitMessage string, dryRun bool) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommitFile commits a specific file.
|
||||
func CommitFile(repoPath, filePath, commitMessage string, dryRun bool) error {
|
||||
if commitMessage == "" {
|
||||
return fmt.Errorf("no commit message specified?")
|
||||
}
|
||||
|
||||
commitRepo, err := git.PlainOpen(repoPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commitWorktree, err := commitRepo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
if _, err := commitWorktree.Add(filePath); err != nil {
|
||||
return fmt.Errorf("unable to add %s: %s", filePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
opts := &git.CommitOptions{}
|
||||
if !dryRun {
|
||||
_, err = commitWorktree.Commit(commitMessage, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debug("git changes commited")
|
||||
} else {
|
||||
log.Debug("dry run: no changes commited")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user