10000 Fix issues raised by Code Analysis · freevoid/libgit2sharp@b62438b · GitHub
[go: up one dir, main page]

Skip to content

Commit b62438b

Browse files
committed
Fix issues raised by Code Analysis
1 parent cda8d3f commit b62438b

29 files changed

+76
-49
lines changed

LibGit2Sharp.Tests/AttributesFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void StagingHonorsTheAttributesFiles()
2121
}
2222
}
2323

24-
private void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha)
24+
private static void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha)
2525
{
2626
var sb = new StringBuilder();
2727
sb.Append("I'm going to be dynamically processed\r\n");
@@ -44,7 +44,7 @@ private void AssertNormalization(Repository repo, string filename, bool shouldHa
4444
Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r"));
4545
}
4646

47-
private void CreateAttributesFile(Repository repo)
47+
private static void CreateAttributesFile(Repository repo)
4848
{
4949
const string relativePath = ".gitattributes";
5050
string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ private static void CreateAndStageANewFile(Repository repo)
631631
repo.Index.Stage(relativeFilepath);
632632
}
633633

634-
private void AssertCommitHasBeenAmended(Repository repo, Commit amendedCommit, Commit originalCommit)
634+
private static void AssertCommitHasBeenAmended(Repository repo, Commit amendedCommit, Commit originalCommit)
635635
{
636636
Commit headCommit = repo.Head.Tip;
637637
Assert.Equal(amendedCommit, headCommit);

LibGit2Sharp.Tests/LazyFixture.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using LibGit2Sharp.Core.Compat;
3-
using LibGit2Sharp.Tests.TestHelpers;
43
using Xunit;
54

65
namespace LibGit2Sharp.Tests

LibGit2Sharp.Tests/ObjectIdFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Linq;
3-
using LibGit2Sharp.Tests.TestHelpers;
42
using Xunit;
53
using Xunit.Extensions;
64

@@ -11,7 +9,7 @@ public class ObjectIdFixture
119
private const string validSha1 = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
1210
private const string validSha2 = "de08fe4884650f067bd5703b6a59a8b3b3c99a09";
1311

14-
private static byte[] bytes = new byte[] { 206, 8, 254, 72, 132, 101, 15, 6, 123, 213, 112, 59, 106, 89, 168, 179, 179, 201, 154, 9 };
12+
private static readonly byte[] bytes = new byte[] { 206, 8, 254, 72, 132, 101, 15, 6, 123, 213, 112, 59, 106, 89, 168, 179, 179, 201, 154, 9 };
1513

1614
[Theory]
1715
[InlineData("Dummy", typeof(ArgumentException))]

LibGit2Sharp.Tests/ResetIndexFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void ResettingInABareRepositoryThrows()
3030
}
3131
}
3232

33-
private bool IsStaged(StatusEntry entry)
33+
private static bool IsStaged(StatusEntry entry)
3434
{
3535
if ((entry.State & FileStatus.Added) == FileStatus.Added)
3636
{

LibGit2Sharp.Tests/StatusFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void RetrievingTheStatusOfAnEmptyRepositoryHonorsTheGitIgnoreDirectives()
133133

134134
using (Repository repo = Repository.Init(scd.DirectoryPath))
135135
{
136-
string relativePath = "look-ma.txt";
136+
const string relativePath = "look-ma.txt";
137137
string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);
138138
File.WriteAllText(fullFilePath, "I'm going to be ignored!");
139139

LibGit2Sharp.Tests/TupleFixture.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using LibGit2Sharp.Core.Compat;
2-
using LibGit2Sharp.Tests.TestHelpers;
32
using Xunit;
43

54
namespace LibGit2Sharp.Tests

LibGit2Sharp/Blob.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.IO;
33
using System.Runtime.InteropServices;
4-
using System.Text;
54
using LibGit2Sharp.Core;
65
using LibGit2Sharp.Core.Handles;
76

LibGit2Sharp/BlobExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using LibGit2Sharp.Core;
23

34
namespace LibGit2Sharp
45
{
@@ -14,6 +15,8 @@ public static class BlobExtensions
1415
/// <returns>Blob content as UTF-8</returns>
1516
public static string ContentAsUtf8(this Blob blob)
1617
{
18+
Ensure.ArgumentNotNull(blob, "blob");
19+
1720
return Encoding.UTF8.GetString(blob.Content);
1821
}
1922

@@ -24,6 +27,8 @@ public static string ContentAsUtf8(this Blob blob)
2427
/// <returns>Blob content as unicode.</returns>
2528
public static string ContentAsUnicode(this Blob blob)
2629
{
30+
Ensure.ArgumentNotNull(blob, "blob");
31+
2732
return Encoding.Unicode.GetString(blob.Content);
2833
}
2934
}

LibGit2Sharp/Changes.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ public abstract class Changes
99
{
1010
private readonly StringBuilder patchBuilder = new StringBuilder();
1111

12-
/// <summary>
13-
/// Needed for mocking purposes.
14-
/// </summary>
15-
protected Changes()
16-
{ }
17-
1812
internal void AppendToPatch(string patch)
1913
{
2014
patchBuilder.Append(patch);

LibGit2Sharp/CommitLog.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public virtual Commit FindCommonAncestor(Commit first, Commit second)
166166
public virtual Commit FindCommonAncestor(IEnumerable<Commit> commits)
167167
{
168168
Ensure.ArgumentNotNull(commits, "commits");
169+
169170
Commit ret = null;
170171
int count = 0;
171172

LibGit2Sharp/Core/Handles/SafeHandleBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Globalization;
34
using System.Runtime.InteropServices;
45

56
namespace LibGit2Sharp.Core.Handles
@@ -23,7 +24,7 @@ protected override void Dispose(bool disposing)
2324
#if DEBUG
2425
if (!disposing && !IsInvalid)
2526
{
26-
Trace.WriteLine(string.Format("A {0} handle wrapper has not been properly disposed.", GetType().Name));
27+
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "A {0} handle wrapper has not been properly disposed.", GetType().Name));
2728
#if LEAKS
2829
Trace.WriteLine(trace);
2930
#endif

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,6 @@ public static extern int git_treebuilder_insert(
718718
public static extern int git_treebuilder_write(out GitOid oid, RepositorySafeHandle repo, TreeBuilderSafeHandle bld);
719719

720720
[DllImport(libgit2)]
721-
public static extern int git_treebuilder_free(IntPtr bld);
721+
public static extern void git_treebuilder_free(IntPtr bld);
722722
}
723723
}

LibGit2Sharp/Core/UnSafeNativeMethods.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Runtime.InteropServices;
1+
using System.Runtime.InteropServices;
32
using LibGit2Sharp.Core.Handles;
43

54
namespace LibGit2Sharp.Core

LibGit2Sharp/Core/Utf8Marshaler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public virtual IntPtr MarshalManagedToNative(object managedObj)
3131
return StringToNative((string)managedObj);
3232
}
3333

34-
protected unsafe IntPtr StringToNative(string value)
34+
protected static unsafe IntPtr StringToNative(string value)
3535
{
3636
// not null terminated
3737
byte[] strbuf = Encoding.UTF8.GetBytes(value);
@@ -50,7 +50,7 @@ public virtual object MarshalNativeToManaged(IntPtr pNativeData)
5050
return NativeToString(pNativeData);
5151
}
5252

53-
protected unsafe string NativeToString(IntPtr pNativeData)
53+
protected static unsafe string NativeToString(IntPtr pNativeData)
5454
{
5555
var walk = (byte*)pNativeData;
5656

LibGit2Sharp/CustomDictionary.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<Word>sha</Word>
99
<Word>unstage</Word>
1010
<Word>unstaged</Word>
11+
<Word>compat</Word>
12+
<Word>oid</Word>
1113
</Recognized>
1214
<Deprecated>
1315
<Term PreferredAlternate=""></Term>

LibGit2Sharp/Diff.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ internal Diff(Repository repo)
7070
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <paramref name = "oldTree"/> and the <paramref name = "newTree"/>.</returns>
7171
public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<string> paths = null)
7272
{
73+
Ensure.ArgumentNotNull(oldTree, "oldTree");
74+
Ensure.ArgumentNotNull(oldTree, "newTree");
75+
7376
using(GitDiffOptions options = BuildOptions(paths))
7477
using (DiffListSafeHandle diff = BuildDiffListFromTrees(oldTree.Id, newTree.Id, options))
7578
{
@@ -122,6 +125,8 @@ private static IDictionary<DiffTarget, Func<Repository, TreeComparisonHandleRetr
122125
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
123126
public virtual TreeChanges Compare(Tree oldTree, DiffTarget diffTarget, IEnumerable<string> paths = null)
124127
{
128+
Ensure.ArgumentNotNull(oldTree, "oldTree");
129+
125130
var comparer = handleRetrieverDispatcher[diffTarget](repo);
126131

127132
using (GitDiffOptions options = BuildOptions(paths))

LibGit2Sharp/Index.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public virtual void Stage(IEnumerable<string> paths)
133133
//TODO: Stage() should support following use cases:
134134
// - Recursively staging the content of a directory
135135

136-
IDictionary<string, FileStatus> batch = PrepareBatch(paths);
136+
IEnumerable<KeyValuePair<string, FileStatus>> batch = PrepareBatch(paths);
137137

138138
foreach (KeyValuePair<string, FileStatus> kvp in batch)
139139
{
@@ -289,7 +289,7 @@ public virtual void Remove(IEnumerable<string> paths)
289289
//TODO: Remove() should support following use cases:
290290
// - Removing a directory and its content
291291

292-
IDictionary<string, FileStatus> batch = PrepareBatch(paths);
292+
IEnumerable<KeyValuePair<string, FileStatus>> batch = PrepareBatch(paths);
293293

294294
foreach (KeyValuePair<string, FileStatus> keyValuePair in batch)
295295
{
@@ -320,7 +320,7 @@ public virtual void Remove(IEnumerable<string> paths)
320320
UpdatePhysicalIndex();
321321
}
322322

323-
private IDictionary<string, FileStatus> PrepareBatch(IEnumerable<string> paths)
323+
private IEnumerable<KeyValuePair<string, FileStatus>> PrepareBatch(IEnumerable<string> paths)
324324
{
325325
Ensure.ArgumentNotNull(paths, "paths");
326326

@@ -370,7 +370,7 @@ private Tuple<string, FileStatus> BuildFrom(string path)
370370
return new Tuple<string, FileStatus>(relativePath, RetrieveStatus(relativePath));
371371
}
372372

373-
private bool Enumerate(IEnumerator<string> leftEnum, IEnumerator<string> rightEnum)
373+
private static bool Enumerate(IEnumerator<string> leftEnum, IEnumerator<string> rightEnum)
374374
{
375375
bool isLeftEoF = leftEnum.MoveNext();
376376
bool isRightEoF = rightEnum.MoveNext();
@@ -484,7 +484,7 @@ internal void Reset(TreeChanges changes)
484484
continue;
485485

486486
default:
487-
throw new InvalidOperationException(string.Format("Entry '{0}' bears an unexpected ChangeKind '{1}'", treeEntryChanges.Path, treeEntryChanges.Status));
487+
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Entry '{0}' bears an unexpected ChangeKind '{1}'", treeEntryChanges.Path, treeEntryChanges.Status));
488488
}
489489
}
490490

LibGit2Sharp/LibGit2SharpException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace LibGit2Sharp
45
{
@@ -38,6 +39,7 @@ public LibGit2Exception(string message, Exception innerException)
3839
/// <summary>
3940
/// The exception that is thrown when an error occurs during application execution.
4041
/// </summary>
42+
[Serializable]
4143
public class LibGit2SharpException : Exception
4244
{
4345
/// <summary>
@@ -65,5 +67,15 @@ public LibGit2SharpException(string message, Exception innerException)
6567
: base(message, innerException)
6668
{
6769
}
70+
71+
/// <summary>
72+
/// Initializes a new instance of the <see cref = "LibGit2SharpException" /> class with a serialized data.
73+
/// </summary>
74+
/// <param name = "info">The <see cref="SerializationInfo "/> that holds the serialized object data about the exception being thrown.</param>
75+
/// <param name = "context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
76+
protected LibGit2SharpException(SerializationInfo info, StreamingContext context)
77+
: base(info, context)
78+
{
79+
}
6880
}
6981
}

LibGit2Sharp/Mode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum Mode
2525
/// <summary>
2626
/// 100664 file mode
2727
/// </summary>
28-
NonExecutableGroupWriteableFile = 0x81B4,
28+
NonExecutableGroupWritableFile = 0x81B4,
2929

3030
/// <summary>
3131
/// 100755 file mode

LibGit2Sharp/Note.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private Note(ObjectId blobId, string message, ObjectId targetObjectId, string @n
4444
/// </summary>
4545
public virtual ObjectId TargetObjectId { get; private set; }
4646

47-
internal static Note BuildFromPtr(Repository repo, string @namespace, ObjectId targetObjectId, NoteSafeHandle note)
47+
internal static Note BuildFromPtr(string @namespace, ObjectId targetObjectId, NoteSafeHandle note)
4848
{
4949
ObjectId oid = NativeMethods.git_note_oid(note).MarshalAsObjectId();
5050
string message = NativeMethods.git_note_message(note);

LibGit2Sharp/NoteCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal IEnumerable<string> NamespaceRefs
7878
return new[] { NormalizeToCanonicalName(DefaultNamespace) }.Concat(
7979
from reference in repo.Refs
8080
select reference.CanonicalName into refCanonical
81-
where refCanonical.StartsWith(refsNotesPrefix) && refCanonical != NormalizeToCanonicalName(DefaultNamespace)
81+
where refCanonical.StartsWith(refsNotesPrefix, StringComparison.Ordinal) && refCanonical != NormalizeToCanonicalName(DefaultNamespace)
8282
select refCanonical);
8383
}
8484
}
@@ -124,7 +124,7 @@ internal Note RetrieveNote(ObjectId targetObjectId, string canonicalNamespace)
124124
return null;
125125
}
126126

127-
return Note.BuildFromPtr(repo, UnCanonicalizeName(canonicalNamespace), targetObjectId, noteHandle);
127+
return Note.BuildFromPtr(UnCanonicalizeName(canonicalNamespace), targetObjectId, noteHandle);
128128
}
129129
}
130130

@@ -153,7 +153,7 @@ private NoteSafeHandle BuildNoteSafeHandle(ObjectId id, string canonicalNamespac
153153
return noteHandle;
154154
}
155155

156-
internal string NormalizeToCanonicalName(string name)
156+
internal static string NormalizeToCanonicalName(string name)
157157
{
158158
Ensure.ArgumentNotNullOrEmptyString(name, "name");
159159

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ internal ObjectDatabase(Repository repo)
3737
/// <returns>True if the object has been found; false otherwise.</returns>
3838
public virtual bool Contains(ObjectId objectId)
3939
{
40+
Ensure.ArgumentNotNull(objectId, "objectId");
41+
4042
var oid = objectId.Oid;
4143

4244
return NativeMethods.git_odb_exists(handle, ref oid) != (int)GitErrorCode.Ok;
@@ -72,6 +74,8 @@ public virtual Blob CreateBlob(string path)
7274
/// <returns>The created <see cref = "Tree"/>.</returns>
7375
public virtual Tree CreateTree(TreeDefinition treeDefinition)
7476
{
77+
Ensure.ArgumentNotNull(treeDefinition, "treeDefinition");
78+
7579
return treeDefinition.Build(repo);
7680
}
7781

LibGit2Sharp/Reference.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ public abstract class Reference : IEquatable<Reference>
1414
private static readonly LambdaEqualityHelper<Reference> equalityHelper =
1515
new LambdaEqualityHelper<Reference>(new Func<Reference, object>[] { x => x.CanonicalName, x => x.TargetIdentifier });
1616

17-
/// <summary>
18-
/// Needed for mocking purposes.
19-
/// </summary>
20-
protected Reference()
21-
{ }
22-
2317
/// <summary>
2418
/// Gets the full name of this reference.
2519
/// </summary>

LibGit2Sharp/ReferenceWrapper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TO
1919
private static readonly LambdaEqualityHelper<ReferenceWrapper<TObject>> equalityHelper =
2020
new LambdaEqualityHelper<ReferenceWrapper<TObject>>(new Func<ReferenceWrapper<TObject>, object>[] { x => x.CanonicalName, x => x.TargetObject });
2121

22+
private readonly string canonicalName;
23+
2224
/// <summary>
2325
/// Needed for mocking purposes.
2426
/// </summary>
@@ -35,14 +37,17 @@ protected internal ReferenceWrapper(Repository repo, Reference reference, Func<R
3537
Ensure.ArgumentNotNull(canonicalNameSelector, "canonicalNameSelector");
3638

3739
this.repo = repo;
38-
CanonicalName = canonicalNameSelector(reference);
40+
canonicalName = canonicalNameSelector(reference);
3941
objectBuilder = new Lazy<TObject>(() => RetrieveTargetObject(reference));
4042
}
4143

4244
/// <summary>
4345
/// Gets the full name of this reference.
4446
/// </summary>
45-
public virtual string CanonicalName { get; protected set; }
47+
public virtual string CanonicalName
48+
{
49+
get { return canonicalName; }
50+
}
4651

4752
/// <summary>
4853
/// Gets the name of this reference.

0 commit comments

Comments
 (0)
0