summaryrefslogtreecommitdiff
path: root/core/command/commit.go
blob: bf81a2511e32362108cff056d7641e166dacaa21 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package command

import (
	"errors"
	"time"

	"github.com/isacikgoz/gitbatch/core/git"
	log "github.com/sirupsen/logrus"
	gogit "gopkg.in/src-d/go-git.v4"
	"gopkg.in/src-d/go-git.v4/plumbing/object"
)

var (
	commitCmdMode string

	commitCommand       = "commit"
	commitCmdModeLegacy = "git"
	commitCmdModeNative = "go-git"
)

// CommitOptions defines the rules for commit operation
type CommitOptions struct {
	// CommitMsg
	CommitMsg string
	// User
	User string
	// Email
	Email string
}

// CommitCommand defines which commit command to use.
func CommitCommand(r *git.Repository, options CommitOptions) (err error) {
	// here we configure commit operation
	// default mode is go-git (this may be configured)
	commitCmdMode = commitCmdModeNative

	switch commitCmdMode {
	case commitCmdModeLegacy:
		return commitWithGit(r, options)
	case commitCmdModeNative:
		return commitWithGoGit(r, options)
	}
	return errors.New("Unhandled commit operation")
}

// commitWithGit is simply a bare git commit -m <msg> command which is flexible
func commitWithGit(r *git.Repository, options CommitOptions) (err error) {
	args := make([]string, 0)
	args = append(args, commitCommand)
	args = append(args, "-m")
	// parse options to command line arguments
	if len(options.CommitMsg) > 0 {
		args = append(args, options.CommitMsg)
	}
	if err := GenericGitCommand(r.AbsPath, args); err != nil {
		log.Warn("Error at git command (commit)")
		r.Refresh()
		return err
	}
	// till this step everything should be ok
	return r.Refresh()
}

// commitWithGoGit is the primary commit method
func commitWithGoGit(r *git.Repository, options CommitOptions) (err error) {
	config, err := r.Repo.Config()
	if err != nil {
		return err
	}
	name := config.Raw.Section("user").Option("name")
	email := config.Raw.Section("user").Option("email")
	opt := &gogit.CommitOptions{
		Author: &object.Signature{
			Name:  name,
			Email: email,
			When:  time.Now(),
		},
	}

	w, err := r.Repo.Worktree()
	if err != nil {
		return err
	}

	_, err = w.Commit(options.CommitMsg, opt)
	if err != nil {
		r.Refresh()
		return err
	}
	// till this step everything should be ok
	return r.Refresh()
}