Skip to content

Commit 98e6e34

Browse files
committed
Merge pull request libgit2#1031 from libgit2/ntk/optionals
Ensure lack of optional parameters
2 parents e1e5fe6 + 62cc73e commit 98e6e34

21 files changed

+1128
-83
lines changed

LibGit2Sharp.Tests/ConfigurationFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public void CanSetBooleanValue()
256256
[Fact]
257257
public void SettingLocalConfigurationOutsideAReposThrows()
258258
{
259-
using (var config = new Configuration())
259+
using (var config = new Configuration(null, null, null))
260260
{
261261
Assert.Throws<LibGit2SharpException>(() => config.Set("unittests.intsetting", 3));
262262
}

LibGit2Sharp.Tests/MetaFixture.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,59 @@ public void NoPublicTypesUnderLibGit2SharpCoreNamespace()
299299
Assert.True(false, Environment.NewLine + sb.ToString());
300300
}
301301
}
302+
303+
[Fact]
304+
public void NoOptionalParametersinMethods()
305+
{
306+
IEnumerable<string> mis =
307+
from t in Assembly.GetAssembly(typeof(IRepository))
308+
.GetExportedTypes()
309+
from m in t.GetMethods()
310+
where !m.IsObsolete()
311+
from p in m.GetParameters()
312+
where p.IsOptional
313+
select m.DeclaringType + "." + m.Name;
314+
315+
var sb = new StringBuilder();
316+
317+
foreach (var method in mis.Distinct())
318+
{
319+
sb.AppendFormat("At least one overload of method '{0}' accepts an optional parameter.{1}",
320+
method, Environment.NewLine);
321+
}
322+
323+
Assert.Equal("", sb.ToString());
324+
}
325+
326+
[Fact]
327+
public void NoOptionalParametersinConstructors()
328+
{
329+
IEnumerable<string> mis =
330+
from t in Assembly.GetAssembly(typeof(IRepository))
331+
.GetExportedTypes()
332+
from c in t.GetConstructors()
333+
from p in c.GetParameters()
334+
where p.IsOptional
335+
select c.DeclaringType.Name;
336+
337+
var sb = new StringBuilder();
338+
339+
foreach (var method in mis.Distinct())
340+
{
341+
sb.AppendFormat("At least one constructor of type '{0}' accepts an optional parameter.{1}",
342+
method, Environment.NewLine);
343+
}
344+
345+
Assert.Equal("", sb.ToString());
346+
}
347+
}
348+
349+
internal static class TypeExtensions
350+
{
351+
internal static bool IsObsolete(this MethodInfo methodInfo)
352+
{
353+
var attributes = methodInfo.GetCustomAttributes(false);
354+
return attributes.Any(a => a is ObsoleteAttribute);
355+
}
302356
}
303357
}

LibGit2Sharp.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
1111
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHING_EMPTY_BRACES/@EntryValue">True</s:Boolean>
1212
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
13+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
14+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
1315
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
1416
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
1517
</wpf:ResourceDictionary>

LibGit2Sharp/BlobExtensions.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,44 @@ namespace LibGit2Sharp
99
/// </summary>
1010
public static class BlobExtensions
1111
{
12+
/// <summary>
13+
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected from the byte order mark
14+
/// </summary>
15+
/// <param name="blob">The blob for which the content will be returned.</param>
16+
/// <returns>Blob content as text.</returns>
17+
public static string GetContentText(this Blob blob)
18+
{
19+
Ensure.ArgumentNotNull(blob, "blob");
20+
21+
return ReadToEnd(blob.GetContentStream(), null);
22+
}
23+
1224
/// <summary>
1325
/// Gets the blob content decoded with the specified encoding,
14-
/// or according to byte order marks, with UTF8 as fallback,
15-
/// if <paramref name="encoding"/> is null.
26+
/// or according to byte order marks, or the specified encoding as a fallback
1627
/// </summary>
1728
/// <param name="blob">The blob for which the content will be returned.</param>
18-
/// <param name="encoding">The encoding of the text. (default: detected or UTF8)</param>
29+
/// <param name="encoding">The encoding of the text to use, if it cannot be detected</param>
1930
/// <returns>Blob content as text.</returns>
20-
public static string GetContentText(this Blob blob, Encoding encoding = null)
31+
public static string GetContentText(this Blob blob, Encoding encoding)
2132
{
2233
Ensure.ArgumentNotNull(blob, "blob");
34+
Ensure.ArgumentNotNull(encoding, "encoding");
2335

2436
return ReadToEnd(blob.GetContentStream(), encoding);
2537
}
2638

39+
/// <summary>
40+
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected
41+
/// </summary>
42+
/// <param name="blob">The blob for which the content will be returned.</param>
43+
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
44+
/// <returns>Blob content as text.</returns>
45+
public static string GetContentText(this Blob blob, FilteringOptions filteringOptions)
46+
{
47+
return blob.GetContentText(filteringOptions, null);
48+
}
49+
2750
/// <summary>
2851
/// Gets the blob content as it would be checked out to the
2952
/// working directory, decoded with the specified encoding,
@@ -34,7 +57,7 @@ public static string GetContentText(this Blob blob, Encoding encoding = null)
3457
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
3558
/// <param name="encoding">The encoding of the text. (default: detected or UTF8)</param>
3659
/// <returns>Blob content as text.</returns>
37-
public static string GetContentText(this Blob blob, FilteringOptions filteringOptions, Encoding encoding = null)
60+
public static string GetContentText(this Blob blob, FilteringOptions filteringOptions, Encoding encoding)
3861
{
3962
Ensure.ArgumentNotNull(blob, "blob");
4063
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");

LibGit2Sharp/BranchCollection.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,25 @@ IEnumerator IEnumerable.GetEnumerator()
106106

107107
#endregion
108108

109+
/// <summary>
110+
/// Create a new local branch with the specified name
111+
/// </summary>
112+
/// <param name="name">The name of the branch.</param>
113+
/// <param name="committish">Revparse spec for the target commit.</param>
114+
/// <returns>A new <see cref="Branch"/>.</returns>
115+
public virtual Branch Add(string name, string committish)
116+
{
117+
return Add(name, committish, false);
118+
}
119+
109120
/// <summary>
110121
/// Create a new local branch with the specified name
111122
/// </summary>
112123
/// <param name="name">The name of the branch.</param>
113124
/// <param name="committish">Revparse spec for the target commit.</param>
114125
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
115126
/// <returns>A new <see cref="Branch"/>.</returns>
116-
public virtual Branch Add(string name, string committish, bool allowOverwrite = false)
127+
public virtual Branch Add(string name, string committish, bool allowOverwrite)
117128
{
118129
Ensure.ArgumentNotNullOrEmptyString(name, "name");
119130
Ensure.ArgumentNotNullOrEmptyString(committish, "committish");
@@ -138,14 +149,25 @@ public virtual void Remove(Branch branch)
138149
}
139150
}
140151

152+
/// <summary>
153+
/// Rename an existing local branch
154+
/// </summary>
155+
/// <param name="branch">The current local branch.</param>
156+
/// <param name="newName">The new name the existing branch should bear.</param>
157+
/// <returns>A new <see cref="Branch"/>.</returns>
158+
public virtual Branch Rename(Branch branch, string newName)
159+
{
160+
return Rename(branch, newName, false);
161+
}
162+
141163
/// <summary>
142164
/// Rename an existing local branch
143165
/// </summary>
144166
/// <param name="branch">The current local branch.</param>
145167
/// <param name="newName">The new name the existing branch should bear.</param>
146168
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
147169
/// <returns>A new <see cref="Branch"/>.</returns>
148-
public virtual Branch Rename(Branch branch, string newName, bool allowOverwrite = false)
170+
public virtual Branch Rename(Branch branch, string newName, bool allowOverwrite)
149171
{
150172
Ensure.ArgumentNotNull(branch, "branch");
151173
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");

LibGit2Sharp/BranchCollectionExtensions.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ namespace LibGit2Sharp
77
/// </summary>
88
public static class BranchCollectionExtensions
99
{
10+
/// <summary>
11+
/// Create a new local branch with the specified name
12+
/// </summary>
13+
/// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
14+
/// <param name="name">The name of the branch.</param>
15+
/// <param name="commit">The target commit.</param>
16+
/// <returns>A new <see cref="Branch"/>.</returns>
17+
public static Branch Add(this BranchCollection branches, string name, Commit commit)
18+
{
19+
return branches.Add(name, commit, false);
20+
}
21+
1022
/// <summary>
1123
/// Create a new local branch with the specified name
1224
/// </summary>
@@ -15,21 +27,30 @@ public static class BranchCollectionExtensions
1527
/// <param name="commit">The target commit.</param>
1628
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
1729
/// <returns>A new <see cref="Branch"/>.</returns>
18-
public static Branch Add(this BranchCollection branches, string name, Commit commit, bool allowOverwrite = false)
30+
public static Branch Add(this BranchCollection branches, string name, Commit commit, bool allowOverwrite)
1931
{
2032
Ensure.ArgumentNotNull(commit, "commit");
2133

2234
return branches.Add(name, commit.Sha, allowOverwrite);
2335
}
2436

37+
/// <summary>
38+
/// Deletes the branch with the specified name.
39+
/// </summary>
40+
/// <param name="name">The name of the branch to delete.</param>
41+
/// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
42+
public static void Remove(this BranchCollection branches, string name)
43+
{
44+
branches.Remove(name, false);
45+
}
2546

2647
/// <summary>
2748
/// Deletes the branch with the specified name.
2849
/// </summary>
2950
/// <param name="name">The name of the branch to delete.</param>
3051
/// <param name="isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
3152
/// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
32-
public static void Remove(this BranchCollection branches, string name, bool isRemote = false)
53+
public static void Remove(this BranchCollection branches, string name, bool isRemote)
3354
{
3455
Ensure.ArgumentNotNullOrEmptyString(name, "name");
3556

@@ -45,6 +66,18 @@ public static void Remove(this BranchCollection branches, string name, bool isRe
4566
branches.Remove(branch);
4667
}
4768

69+
/// <summary>
70+
/// Rename an existing local branch, using the default reflog message
71+
/// </summary>
72+
/// <param name="currentName">The current branch name.</param>
73+
/// <param name="newName">The new name the existing branch should bear.</param>
74+
/// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
75+
/// <returns>A new <see cref="Branch"/>.</returns>
76+
public static Branch Rename(this BranchCollection branches, string currentName, string newName)
77+
{
78+
return branches.Rename(currentName, newName, false);
79+
}
80+
4881
/// <summary>
4982
/// Rename an existing local branch, using the default reflog message
5083
/// </summary>
@@ -53,7 +86,7 @@ public static void Remove(this BranchCollection branches, string name, bool isRe
5386
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
5487
/// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
5588
/// <returns>A new <see cref="Branch"/>.</returns>
56-
public static Branch Rename(this BranchCollection branches, string currentName, string newName, bool allowOverwrite = false)
89+
public static Branch Rename(this BranchCollection branches, string currentName, string newName, bool allowOverwrite)
5790
{
5891
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
5992
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");

LibGit2Sharp/CommitRewriteInfo.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,46 @@ public static CommitRewriteInfo From(Commit commit)
3535
};
3636
}
3737

38+
/// <summary>
39+
/// Build a <see cref="CommitRewriteInfo"/> from the <see cref="Commit"/> passed in,
40+
/// optionally overriding some of its properties
41+
/// </summary>
42+
/// <param name="commit">The <see cref="Commit"/> whose information is to be copied</param>
43+
/// <param name="author">Optional override for the author</param>
44+
/// <returns>A new <see cref="CommitRewriteInfo"/> object that matches the info for the
45+
/// <paramref name="commit"/> with the optional parameters replaced..</returns>
46+
public static CommitRewriteInfo From(Commit commit, Signature author)
47+
{
48+
return From(commit, author, null, null);
49+
}
50+
51+
/// <summary>
52+
/// Build a <see cref="CommitRewriteInfo"/> from the <see cref="Commit"/> passed in,
53+
/// optionally overriding some of its properties
54+
/// </summary>
55+
/// <param name="commit">The <see cref="Commit"/> whose information is to be copied</param>
56+
/// <param name="message">Optional override for the message</param>
57+
/// <returns>A new <see cref="CommitRewriteInfo"/> object that matches the info for the
58+
/// <paramref name="commit"/> with the optional parameters replaced..</returns>
59+
public static CommitRewriteInfo From(Commit commit, string message)
60+
{
61+
return From(commit, null, null, message);
62+
}
63+
64+
/// <summary>
65+
/// Build a <see cref="CommitRewriteInfo"/> from the <see cref="Commit"/> passed in,
66+
/// optionally overriding some of its properties
67+
/// </summary>
68+
/// <param name="commit">The <see cref="Commit"/> whose information is to be copied</param>
69+
/// <param name="author">Optional override for the author</param>
70+
/// <param name="committer">Optional override for the committer</param>
71+
/// <returns>A new <see cref="CommitRewriteInfo"/> object that matches the info for the
72+
/// <paramref name="commit"/> with the optional parameters replaced..</returns>
73+
public static CommitRewriteInfo From(Commit commit, Signature author, Signature committer)
74+
{
75+
return From(commit, author, committer, null);
76+
}
77+
3878
/// <summary>
3979
/// Build a <see cref="CommitRewriteInfo"/> from the <see cref="Commit"/> passed in,
4080
/// optionally overriding some of its properties
@@ -46,9 +86,9 @@ public static CommitRewriteInfo From(Commit commit)
4686
/// <returns>A new <see cref="CommitRewriteInfo"/> object that matches the info for the
4787
/// <paramref name="commit"/> with the optional parameters replaced..</returns>
4888
public static CommitRewriteInfo From(Commit commit,
49-
Signature author = null,
50-
Signature committer = null,
51-
string message = null)
89+
Signature author,
90+
Signature committer,
91+
string message)
5292
{
5393
var cri = From(commit);
5494
cri.Author = author ?? cri.Author;

0 commit comments

Comments
 (0)