-
Notifications
You must be signed in to change notification settings - Fork 22
/
run.go
160 lines (120 loc) · 4.18 KB
/
run.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package run
import (
"bytes"
"errors"
"github.com/lesfurets/git-octopus/config"
"github.com/lesfurets/git-octopus/git"
"log"
)
type OctopusContext struct {
Repo *git.Repository
Logger *log.Logger
}
const VERSION = "2.0"
func Run(context *OctopusContext, args ...string) error {
octopusConfig, err := config.GetOctopusConfig(context.Repo, args)
if err != nil {
return err
}
if octopusConfig.PrintVersion {
context.Logger.Println(VERSION)
return nil
}
if len(octopusConfig.Patterns) == 0 {
context.Logger.Println("Nothing to merge. No pattern given")
return nil
}
status, _ := context.Repo.Git("status", "--porcelain")
// This is not formally required but it would be an ambiguous behaviour to let git-octopus run on unclean state.
if len(status) != 0 {
return errors.New("The repository has to be clean.")
}
branchList := resolveBranchList(context.Repo, context.Logger, octopusConfig.Patterns, octopusConfig.ExcludedPatterns)
if len(branchList) == 0 {
return nil
}
initialHeadCommit, _ := context.Repo.Git("rev-parse", "HEAD")
if !octopusConfig.DoCommit {
defer context.Repo.Git("reset", "-q", "--hard", initialHeadCommit)
}
context.Logger.Println()
parents, err := mergeHeads(context, branchList)
if err != nil {
return err
}
if !octopusConfig.DoCommit {
return nil
}
if len(parents) == 1 {
// This is either a fast-forward update or a no op
context.Repo.Git("update-ref", "HEAD", parents[0])
} else {
// We need at least 2 parents to create a merge commit
tree, _ := context.Repo.Git("write-tree")
args := []string{"commit-tree"}
for _, parent := range parents {
args = append(args, "-p", parent)
}
args = append(args, "-m", octopusCommitMessage(branchList), tree)
commit, _ := context.Repo.Git(args...)
context.Repo.Git("update-ref", "HEAD", commit)
}
return nil
}
// The logic of this function is copied directly from git-merge-octopus.sh
func mergeHeads(context *OctopusContext, remotes []git.LsRemoteEntry) ([]string, error) {
head, _ := context.Repo.Git("rev-parse", "--verify", "-q", "HEAD")
// The list of commits that has already merged in the current tree. Originally called 'MRC' in git-merge-octopus.sh
mergedHeads := []string{head}
// currentTree originally called 'MRT' in git-merge-octopus.sh
currentTree, _ := context.Repo.Git("write-tree")
isFfMerge := true
for _, lsRemoteEntry := range remotes {
common, err := context.Repo.Git(append([]string{"merge-base", "--all", lsRemoteEntry.Sha1}, mergedHeads...)...)
if err != nil {
return nil, errors.New("Unable to find common commit with " + lsRemoteEntry.Ref)
}
if common == lsRemoteEntry.Sha1 {
context.Logger.Println("Already up-to-date with " + lsRemoteEntry.Ref)
continue
}
if len(mergedHeads) == 1 && common == mergedHeads[0] && isFfMerge {
context.Logger.Println("Fast-forwarding to: " + lsRemoteEntry.Ref)
_, err := context.Repo.Git("read-tree", "-u", "-m", mergedHeads[0], lsRemoteEntry.Sha1)
if err != nil {
return nil, err
}
mergedHeads[0] = lsRemoteEntry.Sha1
currentTree, _ = context.Repo.Git("write-tree")
continue
}
isFfMerge = false
context.Logger.Println("Trying simple merge with " + lsRemoteEntry.Ref)
_, err = context.Repo.Git("read-tree", "-u", "-m", "--aggressive", common, currentTree, lsRemoteEntry.Sha1)
if err != nil {
return nil, err
}
nextTree, err := context.Repo.Git("write-tree")
if err != nil {
context.Logger.Println("Simple merge did not work, trying automatic merge.")
_, err = context.Repo.Git("merge-index", "-o", "git-merge-one-file", "-a")
if err != nil {
context.Logger.Println("Automated merge did not work.")
context.Logger.Println("Should not be doing an Octopus.")
return nil, errors.New("")
}
nextTree, _ = context.Repo.Git("write-tree")
}
mergedHeads = append(mergedHeads, lsRemoteEntry.Sha1)
currentTree = nextTree
}
return mergedHeads, nil
}
func octopusCommitMessage(remotes []git.LsRemoteEntry) string {
buf := bytes.NewBufferString("Merged branches:\n")
for _, lsRemoteEntry := range remotes {
buf.WriteString(lsRemoteEntry.Ref + "\n")
}
buf.WriteString("\nCommit created by git-octopus " + VERSION + ".\n")
return buf.String()
}