Skip to content

Commit ccf47bf

Browse files
committed
wip2
Signed-off-by: David Pordomingo <[email protected]>
1 parent 11b19ac commit ccf47bf

File tree

4 files changed

+30
-48
lines changed

4 files changed

+30
-48
lines changed

_examples/common_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var args = map[string][]string{
2828
"showcase": {defaultURL, tempFolder()},
2929
"tag": {cloneRepository(defaultURL, tempFolder())},
3030
"pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
31+
"merge": {cloneRepository(defaultURL, tempFolder()), "HEAD~2", "HEAD"},
3132
"merge_base": {cloneRepository(defaultURL, tempFolder()), "--is-ancestor", "HEAD~3", "HEAD^"},
3233
"log_local": {cloneRepository(defaultURL, tempFolder())},
3334
}

_examples/merge/help-long.msg.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ NAME:
55
%_COMMAND_NAME_% - Join two commits
66
77
SYNOPSIS:
8-
usage: %_COMMAND_NAME_% <path> <baseCommitRev> <commitRev> [-m <msg>] [--ff-only]
8+
usage: %_COMMAND_NAME_% <path> <baseCommitRev> <targetCommitRev> [-m <msg>] [--ff-only]
99
[--no-ff] [--no-commit] [--allow-unrelated-histories]
1010
or: %_COMMAND_NAME_% --help
1111
1212
params:
13-
<path> Path to the git repository
14-
<baseCommitRev> Git revision of the commit that will be the base of the merge
15-
<commitRev> Git revision of the commit that will be merged over the base
13+
<path> Path to the git repository
14+
<baseCommitRev> Git revision of the commit that will be the base of the merge
15+
<targetCommitRev> Git revision of the commit that will be merged over the base
1616
1717
DESCRIPTION:
18-
%_COMMAND_NAME_% Incorporates changes from the passed <commitRev> over <baseCommitRev> and moves the HEAD of the repo to the merge commit.
18+
%_COMMAND_NAME_% Incorporates changes from the passed <targetCommitRev> over <baseCommitRev> and moves the HEAD of the repo to the merge commit.
1919
2020
OPTIONS:
2121
If no options passed, the merge commit will be avoided if it could be considered as a fast-forward, and if needed, it will be used a default merge commit message.
@@ -32,16 +32,11 @@ OPTIONS:
3232
[NOT IMPLEMENTED]
3333
Create a merge commit even when the merge resolves as a fast-forward.
3434
35-
--no-commit
36-
[NOT IMPLEMENTED]
37-
With --no-commit perform the merge but pretend the merge failed and do not autocommit, to give the user a chance to
38-
inspect and further tweak the merge result before committing.
39-
4035
--allow-unrelated-histories
4136
[NOT IMPLEMENTED]
4237
By default, git merge command refuses to merge histories that do not share a common ancestor. This option can be
4338
used to override this safety when merging histories of two projects that started their lives independently.
44-
39+
4540
DISCUSSION:
4641
Assume the following history exists and the current branch is "master":
4742

_examples/merge/main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ const (
2121
cmdDesc = "Performs the merge between two commits, and moves HEAD into the merge commit:"
2222

2323
helpShortMsg = `
24-
usage: %_COMMAND_NAME_% <path> <baseCommitRev> <commitRev> [-m <msg>] [--ff-only]
25-
[--no-ff] [--no-commit] [--allow-unrelated-histories]
24+
usage: %_COMMAND_NAME_% <path> <baseCommitRev> <targetCommitRev> [-m <msg>]
25+
[--ff-only] [--no-ff] [--allow-unrelated-histories]
2626
or: %_COMMAND_NAME_% --help
2727
2828
params:
29-
<path> Path to the git repository
30-
<baseCommitRev> Git revision of the commit that will be the base of the merge
31-
<commitRev> Git revision of the commit that will be merged over the base
29+
<path> Path to the git repository
30+
<baseCommitRev> Git revision of the commit that will be the base of the merge
31+
<targetCommitRev> Git revision of the commit that will be merged over the base
3232
3333
options:
3434
(no options) Performs the regular merge, as fast-forward if possible
@@ -38,7 +38,6 @@ options:
3838
[NOT IMPLEMENTED]
3939
-m <msg> Uses the passed <msg> for the merge commit message
4040
--no-ff Create a merge commit even when it could be a fast-forward
41-
--no-commit Performs the merge in the worktree and do not create a commit
4241
--allow-unrelated-histories
4342
Lets the merge to operate with commits not sharing its history
4443
`
@@ -74,8 +73,6 @@ func main() {
7473
mergeOptions.NoFF = true
7574
case "--ff-only":
7675
mergeOptions.FFOnly = true
77-
case "--no-commit":
78-
mergeOptions.NoCommit = true
7976
case "--allow-unrelated-histories":
8077
mergeOptions.AllowUnrelated = true
8178
case "-m":
@@ -91,6 +88,10 @@ func main() {
9188
}
9289
}
9390

91+
if mergeOptions.NoFF && mergeOptions.FFOnly {
92+
utils.WrongSyntaxAndExit("--no-ff is not compatible with --ff-only")
93+
}
94+
9495
// Open a git repository from current directory
9596
repo, err := git.PlainOpen(path)
9697
utils.ExitIfError(err, utils.ExitCodeCouldNotOpenRepository)
@@ -120,7 +121,6 @@ func main() {
120121
fmt.Println("Already up to date.")
121122
return
122123
case git.ErrNotImplementedUnrelated,
123-
git.ErrNotImplementedNoCommit,
124124
git.ErrNotImplementedNoFF,
125125
git.ErrNotImplementedMessage:
126126
utils.ExitIfError(err, exitCodeFeatureNotImplemented, "unimplemented feature")

merge.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const (
2121
var (
2222
// ErrSameCommit returned when passed commits are the same
2323
ErrSameCommit = errors.New("passed commits are the same")
24-
// ErrAlreadyUpToDate returned when the second is behind first
25-
ErrAlreadyUpToDate = errors.New("second is behind first")
24+
// ErrAlreadyUpToDate returned when the target is behind base
25+
ErrAlreadyUpToDate = errors.New("target is behind base")
2626
// ErrHasConflicts returned when conflicts found
2727
ErrHasConflicts = errors.New("conflicts found")
2828
// ErrNoCommonHistory returned when no shared history
@@ -34,8 +34,6 @@ var (
3434

3535
// ExitCodeUnexpected returned when commit merge is required
3636
ErrNotImplementedNoFF = errors.New("no fast-forward merge is not implemented")
37-
// ErrNotImplementedNoCommit returned when no-commit is required
38-
ErrNotImplementedNoCommit = errors.New("no commit merge is not implemented")
3937
// ErrNotImplementedUnrelated returned
4038
ErrNotImplementedUnrelated = errors.New("unrelated merge is not implemented")
4139
// ErrNotImplementedMessage returned
@@ -46,20 +44,17 @@ var (
4644
type MergeOptions struct {
4745
NoFF bool // NoFF when set to true, Merge will always create a merge commit
4846
FFOnly bool // FFOnly causes the Merge fail if it is not a fast forward
49-
NoCommit bool // NoCommit leaves the changes in the worktree without commit them
5047
AllowUnrelated bool // AllowUnrelated performs the merge even with unrelated histories
5148
Message string // Message text to be used for the message
5249
}
5350

54-
// Merge merges the second commit over the first one, and moves `HEAD` to the merge.
55-
// If `NoCommit` option was passed, the changes required for the merge will be
56-
// left in the worktree, and the merge commit won't be created.
51+
// Merge merges the target commit over the base one, and moves `HEAD` to the merge.
5752
// It returns the merge commit, and an error if the HEAD was not moved or
5853
// when the merge operation could not be done.
5954
func Merge(
6055
repo *Repository,
61-
first *object.Commit,
62-
second *object.Commit,
56+
base *object.Commit,
57+
target *object.Commit,
6358
options *MergeOptions,
6459
) (*object.Commit, error) {
6560
if options == nil {
@@ -80,35 +75,35 @@ func Merge(
8075
return nil, ErrWorktreeNotClean
8176
}
8277

83-
if first.Hash == second.Hash {
78+
if base.Hash == target.Hash {
8479
return nil, ErrSameCommit
8580
}
8681

87-
ancestors, err := MergeBase(first, second)
82+
ancestors, err := MergeBase(base, target)
8883
if err != nil {
8984
return nil, err
9085
}
9186

9287
if len(ancestors) == 0 {
9388
if options.AllowUnrelated {
94-
return merge(first, second, nil, options.NoCommit, options.Message)
89+
return merge(base, target, nil, options.Message)
9590
}
9691

9792
return nil, ErrNoCommonHistory
9893
}
9994

10095
for _, ancestor := range ancestors {
101-
if ancestor.Hash == first.Hash {
96+
if ancestor.Hash == base.Hash {
10297
if options.NoFF {
10398
// TODO(dpordomingo): there is a special case;
10499
// if asked with `--no-ff` it should be created an empty merge-commit.
105100
return nil, ErrNotImplementedNoFF
106101
}
107102

108-
return second, nil
103+
return target, nil
109104
}
110105

111-
if ancestor.Hash == second.Hash {
106+
if ancestor.Hash == target.Hash {
112107
return nil, ErrAlreadyUpToDate
113108
}
114109
}
@@ -119,22 +114,18 @@ func Merge(
119114
return nil, ErrNonFastForwardUpdate
120115
}
121116

122-
return merge(first, second, mergeBase, options.NoCommit, options.Message)
117+
return merge(base, target, mergeBase, options.Message)
123118
}
124119

125-
func merge(
126-
first, second, mergeBase *object.Commit,
127-
noCommit bool,
128-
msg string,
129-
) (*object.Commit, error) {
120+
func merge(base, target, mergeBase *object.Commit, msg string) (*object.Commit, error) {
130121

131122
if mergeBase == nil {
132123
// TODO(dpordomingo): handle --no-commit flag
133124
return nil, ErrNotImplementedUnrelated
134125
}
135126

136127
var trees []*object.Tree
137-
for _, commit := range []*object.Commit{first, second} {
128+
for _, commit := range []*object.Commit{base, target} {
138129
tree, err := commit.Tree()
139130
if err != nil {
140131
return nil, err
@@ -149,11 +140,6 @@ func merge(
149140
}
150141
fmt.Println(changes)
151142

152-
if noCommit {
153-
// TODO(dpordomingo): handle --no-commit flag
154-
return nil, ErrNotImplementedNoCommit
155-
}
156-
157143
if msg != "" {
158144
// TODO(dpordomingo): handle -m option
159145
return nil, ErrNotImplementedMessage

0 commit comments

Comments
 (0)