Skip to content

Commit 534d4e7

Browse files
author
Edward Thomson
committed
Introduce MergeOptionsBase
Introduce a MergeOptionsBase and a MergeAndCheckoutOptionsBase so that there's less duplication of the various options that go to merge-like things.
1 parent 63da0d3 commit 534d4e7

7 files changed

+166
-319
lines changed

LibGit2Sharp/CherryPickOptions.cs

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,108 +6,26 @@ namespace LibGit2Sharp
66
/// <summary>
77
/// Options controlling CherryPick behavior.
88
/// </summary>
9-
public sealed class CherryPickOptions : IConvertableToGitCheckoutOpts
9+
public sealed class CherryPickOptions : MergeAndCheckoutOptionsBase
1010
{
1111
/// <summary>
1212
/// Initializes a new instance of the <see cref="CherryPickOptions"/> class.
1313
/// By default the cherry pick will be committed if there are no conflicts.
1414
/// </summary>
1515
public CherryPickOptions()
1616
{
17-
CommitOnSuccess = true;
18-
19-
FindRenames = true;
20-
21-
// TODO: libgit2 should provide reasonable defaults for these
22-
// values, but it currently does not.
23-
RenameThreshold = 50;
24-
TargetLimit = 200;
2517
}
2618

27-
/// <summary>
28-
/// The Flags specifying what conditions are
29-
/// reported through the OnCheckoutNotify delegate.
30-
/// </summary>
31-
public CheckoutNotifyFlags CheckoutNotifyFlags { get; set; }
32-
33-
/// <summary>
34-
/// Delegate that checkout progress will be reported through.
35-
/// </summary>
36-
public CheckoutProgressHandler OnCheckoutProgress { get; set; }
37-
38-
/// <summary>
39-
/// Delegate that checkout will notify callers of
40-
/// certain conditions. The conditions that are reported is
41-
/// controlled with the CheckoutNotifyFlags property.
42-
/// </summary>
43-
public CheckoutNotifyHandler OnCheckoutNotify { get; set; }
44-
45-
/// <summary>
46-
/// Commit the cherry pick if the cherry pick is successful.
47-
/// </summary>
48-
public bool CommitOnSuccess { get; set; }
49-
5019
/// <summary>
5120
/// When cherry picking a merge commit, the parent number to consider as
5221
/// mainline, starting from offset 1.
5322
/// <para>
5423
/// As a merge commit has multiple parents, cherry picking a merge commit
55-
/// will reverse all the changes brought in by the merge except for
56-
/// one parent's line of commits. The parent to preserve is called the
57-
/// mainline, and must be specified by its number (i.e. offset).
24+
/// will take only the changes relative to the given parent. The parent
25+
/// to consider changes based on is called the mainline, and must be
26+
/// specified by its number (i.e. offset).
5827
/// </para>
5928
/// </summary>
6029
public int Mainline { get; set; }
61-
62-
/// <summary>
63-
/// How to handle conflicts encountered during a merge.
64-
/// </summary>
65-
public MergeFileFavor MergeFileFavor { get; set; }
66-
67-
/// <summary>
68-
/// How Checkout should handle writing out conflicting index entries.
69-
/// </summary>
70-
public CheckoutFileConflictStrategy FileConflictStrategy { get; set; }
71-
72-
/// <summary>
73-
/// Find renames. Default is true.
74-
/// </summary>
75-
public bool FindRenames { get; set; }
76-
77-
/// <summary>
78-
/// Similarity to consider a file renamed (default 50). If
79-
/// `FindRenames` is enabled, added files will be compared
80-
/// with deleted files to determine their similarity. Files that are
81-
/// more similar than the rename threshold (percentage-wise) will be
82-
/// treated as a rename.
83-
/// </summary>
84-
public int RenameThreshold;
85-
86-
/// <summary>
87-
/// Maximum similarity sources to examine for renames (default 200).
88-
/// If the number of rename candidates (add / delete pairs) is greater
89-
/// than this value, inexact rename detection is aborted.
90-
///
91-
/// This setting overrides the `merge.renameLimit` configuration value.
92-
/// </summary>
93-
public int TargetLimit;
94-
95-
#region IConvertableToGitCheckoutOpts
96-
97-
CheckoutCallbacks IConvertableToGitCheckoutOpts.GenerateCallbacks()
98-
{
99-
return CheckoutCallbacks.From(OnCheckoutProgress, OnCheckoutNotify);
100-
}
101-
102-
CheckoutStrategy IConvertableToGitCheckoutOpts.CheckoutStrategy
103-
{
104-
get
105-
{
106-
return CheckoutStrategy.GIT_CHECKOUT_SAFE |
107-
GitCheckoutOptsWrapper.CheckoutStrategyFromFileConflictStrategy(FileConflictStrategy);
108-
}
109-
}
110-
111-
#endregion IConvertableToGitCheckoutOpts
11230
}
11331
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@
100100
<Compile Include="IndexReucEntry.cs" />
101101
<Compile Include="IndexReucEntryCollection.cs" />
102102
<Compile Include="IndexNameEntry.cs" />
103+
<Compile Include="MergeAndCheckoutOptionsBase.cs" />
103104
<Compile Include="MergeOptions.cs" />
105+
<Compile Include="MergeOptionsBase.cs" />
104106
<Compile Include="MergeResult.cs" />
105107
<Compile Include="MergeTreeOptions.cs" />
106108
<Compile Include="MergeTreeResult.cs" />
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using LibGit2Sharp.Core;
2+
using LibGit2Sharp.Handlers;
3+
using System;
4+
5+
namespace LibGit2Sharp
6+
{
7+
/// <summary>
8+
/// Options controlling the behavior of things that do a merge and then
9+
/// check out the merge results (eg: merge, revert, cherry-pick).
10+
/// </summary>
11+
public abstract class MergeAndCheckoutOptionsBase : MergeOptionsBase, IConvertableToGitCheckoutOpts
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="MergeOptions"/> class.
15+
/// <para>
16+
/// Default behavior:
17+
/// A fast-forward merge will be performed if possible, unless the merge.ff configuration option is set.
18+
/// A merge commit will be committed, if one was created.
19+
/// Merge will attempt to find renames.
20+
/// </para>
21+
/// </summary>
22+
public MergeAndCheckoutOptionsBase()
23+
{
24+
CommitOnSuccess = true;
25+
}
26+
27+
/// <summary>
28+
/// The Flags specifying what conditions are
29+
/// reported through the OnCheckoutNotify delegate.
30+
/// </summary>
31+
public CheckoutNotifyFlags CheckoutNotifyFlags { get; set; }
32+
33+
/// <summary>
34+
/// Commit the merge if the merge is successful and this is a non-fast-forward merge.
35+
/// If this is a fast-forward merge, then there is no merge commit and this option
36+
/// will not affect the merge.
37+
/// </summary>
38+
public bool CommitOnSuccess { get; set; }
39+
40+
/// <summary>
41+
/// How conflicting index entries should be written out during checkout.
42+
/// </summary>
43+
public CheckoutFileConflictStrategy FileConflictStrategy { get; set; }
44+
45+
/// <summary>
46+
/// Delegate that the checkout will report progress through.
47+
/// </summary>
48+
public CheckoutProgressHandler OnCheckoutProgress { get; set; }
49+
50+
/// <summary>
51+
/// Delegate that checkout will notify callers of
52+
/// certain conditions. The conditions that are reported is
53+
/// controlled with the CheckoutNotifyFlags property.
54+
/// </summary>
55+
public CheckoutNotifyHandler OnCheckoutNotify { get; set; }
56+
57+
#region IConvertableToGitCheckoutOpts
58+
59+
CheckoutCallbacks IConvertableToGitCheckoutOpts.GenerateCallbacks()
60+
{
61+
return CheckoutCallbacks.From(OnCheckoutProgress, OnCheckoutNotify);
62+
}
63+
64+
CheckoutStrategy IConvertableToGitCheckoutOpts.CheckoutStrategy
65+
{
66+
get
67+
{
68+
return CheckoutStrategy.GIT_CHECKOUT_SAFE |
69+
GitCheckoutOptsWrapper.CheckoutStrategyFromFileConflictStrategy(FileConflictStrategy);
70+
}
71+
}
72+
73+
#endregion
74+
}
75+
}

LibGit2Sharp/MergeOptions.cs

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace LibGit2Sharp
77
/// <summary>
88
/// Options controlling Merge behavior.
99
/// </summary>
10-
public sealed class MergeOptions : IConvertableToGitCheckoutOpts
10+
public sealed class MergeOptions : MergeAndCheckoutOptionsBase
1111
{
1212
/// <summary>
1313
/// Initializes a new instance of the <see cref="MergeOptions"/> class.
@@ -20,88 +20,12 @@ public sealed class MergeOptions : IConvertableToGitCheckoutOpts
2020
/// </summary>
2121
public MergeOptions()
2222
{
23-
CommitOnSuccess = true;
24-
25-
FindRenames = true;
26-
// TODO: libgit2 should provide reasonable defaults for these
27-
// values, but it currently does not.
28-
RenameThreshold = 50;
29-
TargetLimit = 200;
3023
}
3124

32-
/// <summary>
33-
/// The Flags specifying what conditions are
34-
/// reported through the OnCheckoutNotify delegate.
35-
/// </summary>
36-
public CheckoutNotifyFlags CheckoutNotifyFlags { get; set; }
37-
38-
/// <summary>
39-
/// Commit the merge if the merge is successful and this is a non-fast-forward merge.
40-
/// If this is a fast-forward merge, then there is no merge commit and this option
41-
/// will not affect the merge.
42-
/// </summary>
43-
public bool CommitOnSuccess { get; set; }
44-
4525
/// <summary>
4626
/// The type of merge to perform.
4727
/// </summary>
4828
public FastForwardStrategy FastForwardStrategy { get; set; }
49-
50-
/// <summary>
51-
/// How conflicting index entries should be written out during checkout.
52-
/// </summary>
53-
public CheckoutFileConflictStrategy FileConflictStrategy { get; set; }
54-
55-
/// <summary>
56-
/// Find renames. Default is true.
57-
/// </summary>
58-
public bool FindRenames { get; set; }
59-
60-
/// <summary>
61-
/// Similarity to consider a file renamed.
62-
/// </summary>
63-
public int RenameThreshold;
64-
65-
/// <summary>
66-
/// Maximum similarity sources to examine (overrides
67-
/// 'merge.renameLimit' config (default 200)
68-
/// </summary>
69-
public int TargetLimit;
70-
71-
/// <summary>
72-
/// How to handle conflicts encountered during a merge.
73-
/// </summary>
74-
public MergeFileFavor MergeFileFavor { get; set; }
75-
76-
/// <summary>
77-
/// Delegate that the checkout will report progress through.
78-
/// </summary>
79-
public CheckoutProgressHandler OnCheckoutProgress { get; set; }
80-
81-
/// <summary>
82-
/// Delegate that checkout will notify callers of
83-
/// certain conditions. The conditions that are reported is
84-
/// controlled with the CheckoutNotifyFlags property.
85-
/// </summary>
86-
public CheckoutNotifyHandler OnCheckoutNotify { get; set; }
87-
88-
#region IConvertableToGitCheckoutOpts
89-
90-
CheckoutCallbacks IConvertableToGitCheckoutOpts.GenerateCallbacks()
91-
{
92-
return CheckoutCallbacks.From(OnCheckoutProgress, OnCheckoutNotify);
93-
}
94-
95-
CheckoutStrategy IConvertableToGitCheckoutOpts.CheckoutStrategy
96-
{
97-
get
98-
{
99-
return CheckoutStrategy.GIT_CHECKOUT_SAFE |
100-
GitCheckoutOptsWrapper.CheckoutStrategyFromFileConflictStrategy(FileConflictStrategy);
101-
}
102-
}
103-
104-
#endregion
10529
}
10630

10731
/// <summary>

LibGit2Sharp/MergeOptionsBase.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using LibGit2Sharp.Core;
2+
using LibGit2Sharp.Handlers;
3+
using System;
4+
5+
namespace LibGit2Sharp
6+
{
7+
/// <summary>
8+
/// Options controlling the behavior of actions that use merge (merge
9+
/// proper, cherry-pick, revert)
10+
/// </summary>
11+
public abstract class MergeOptionsBase
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="MergeOptionsBase"/> class.
15+
/// The default behavior is to attempt to find renames.
16+
/// </summary>
17+
public MergeOptionsBase()
18+
{
19+
FindRenames = true;
20+
RenameThreshold = 50;
21+
TargetLimit = 200;
22+
}
23+
24+
/// <summary>
25+
/// Find renames. Default is true.
26+
/// </summary>
27+
public bool FindRenames { get; set; }
28+
29+
/// <summary>
30+
/// Similarity to consider a file renamed.
31+
/// </summary>
32+
public int RenameThreshold;
33+
34+
/// <summary>
35+
/// Maximum similarity sources to examine (overrides
36+
/// 'merge.renameLimit' config (default 200)
37+
/// </summary>
38+
public int TargetLimit;
39+
40+
/// <summary>
41+
/// How to handle conflicts encountered during a merge.
42+
/// </summary>
43+
public MergeFileFavor MergeFileFavor { get; set; }
44+
}
45+
46+
/// <summary>
47+
/// Enum specifying how merge should deal with conflicting regions
48+
/// of the files.
49+
/// </summary>
50+
public enum MergeFileFavor
51+
{
52+
/// <summary>
53+
/// When a region of a file is changed in both branches, a conflict
54+
/// will be recorded in the index so that the checkout operation can produce
55+
/// a merge file with conflict markers in the working directory.
56+
/// This is the default.
57+
/// </summary>
58+
Normal = 0,
59+
60+
/// <summary>
61+
/// When a region of a file is changed in both branches, the file
62+
/// created in the index will contain the "ours" side of any conflicting
63+
/// region. The index will not record a conflict.
64+
/// </summary>
65+
Ours = 1,
66+
67+
/// <summary>
68+
/// When a region of a file is changed in both branches, the file
69+
/// created in the index will contain the "theirs" side of any conflicting
70+
/// region. The index will not record a conflict.
71+
/// </summary>
72+
Theirs = 2,
73+
74+
/// <summary>
75+
/// When a region of a file is changed in both branches, the file
76+
/// created in the index will contain each unique line from each side,
77+
/// which has the result of combining both files. The index will not
78+
/// record a conflict.
79+
/// </summary>
80+
Union = 3,
81+
}
82+
}

0 commit comments

Comments
 (0)