Permalink
Browse files

Fix issues raised by Code Analysis

  • Loading branch information...
1 parent cda8d3f commit b62438b78d9676f6ca6d7c73440a80e3eb2bbc72 @nulltoken nulltoken committed Jun 12, 2012
@@ -21,7 +21,7 @@ public void StagingHonorsTheAttributesFiles()
}
}
- private void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha)
+ private static void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha)
{
var sb = new StringBuilder();
sb.Append("I'm going to be dynamically processed\r\n");
@@ -44,7 +44,7 @@ private void AssertNormalization(Repository repo, string filename, bool shouldHa
Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r"));
}
- private void CreateAttributesFile(Repository repo)
+ private static void CreateAttributesFile(Repository repo)
{
const string relativePath = ".gitattributes";
string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);
@@ -631,7 +631,7 @@ private static void CreateAndStageANewFile(Repository repo)
repo.Index.Stage(relativeFilepath);
}
- private void AssertCommitHasBeenAmended(Repository repo, Commit amendedCommit, Commit originalCommit)
+ private static void AssertCommitHasBeenAmended(Repository repo, Commit amendedCommit, Commit originalCommit)
{
Commit headCommit = repo.Head.Tip;
Assert.Equal(amendedCommit, headCommit);
@@ -1,6 +1,5 @@
using System;
using LibGit2Sharp.Core.Compat;
-using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
@@ -1,6 +1,4 @@
using System;
-using System.Linq;
-using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;
@@ -11,7 +9,7 @@ public class ObjectIdFixture
private const string validSha1 = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
private const string validSha2 = "de08fe4884650f067bd5703b6a59a8b3b3c99a09";
- 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 };
+ 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 };
[Theory]
[InlineData("Dummy", typeof(ArgumentException))]
@@ -30,7 +30,7 @@ public void ResettingInABareRepositoryThrows()
}
}
- private bool IsStaged(StatusEntry entry)
+ private static bool IsStaged(StatusEntry entry)
{
if ((entry.State & FileStatus.Added) == FileStatus.Added)
{
@@ -133,7 +133,7 @@ public void RetrievingTheStatusOfAnEmptyRepositoryHonorsTheGitIgnoreDirectives()
using (Repository repo = Repository.Init(scd.DirectoryPath))
{
- string relativePath = "look-ma.txt";
+ const string relativePath = "look-ma.txt";
string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);
File.WriteAllText(fullFilePath, "I'm going to be ignored!");
@@ -1,5 +1,4 @@
using LibGit2Sharp.Core.Compat;
-using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
-using System.Text;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
@@ -1,4 +1,5 @@
using System.Text;
+using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
@@ -14,6 +15,8 @@ public static class BlobExtensions
/// <returns>Blob content as UTF-8</returns>
public static string ContentAsUtf8(this Blob blob)
{
+ Ensure.ArgumentNotNull(blob, "blob");
+
return Encoding.UTF8.GetString(blob.Content);
}
@@ -24,6 +27,8 @@ public static string ContentAsUtf8(this Blob blob)
/// <returns>Blob content as unicode.</returns>
public static string ContentAsUnicode(this Blob blob)
{
+ Ensure.ArgumentNotNull(blob, "blob");
+
return Encoding.Unicode.GetString(blob.Content);
}
}
@@ -9,12 +9,6 @@ public abstract class Changes
{
private readonly StringBuilder patchBuilder = new StringBuilder();
- /// <summary>
- /// Needed for mocking purposes.
- /// </summary>
- protected Changes()
- { }
-
internal void AppendToPatch(string patch)
{
patchBuilder.Append(patch);
@@ -166,6 +166,7 @@ public virtual Commit FindCommonAncestor(Commit first, Commit second)
public virtual Commit FindCommonAncestor(IEnumerable<Commit> commits)
{
Ensure.ArgumentNotNull(commits, "commits");
+
Commit ret = null;
int count = 0;
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
+using System.Globalization;
using System.Runtime.InteropServices;
namespace LibGit2Sharp.Core.Handles
@@ -23,7 +24,7 @@ protected override void Dispose(bool disposing)
#if DEBUG
if (!disposing && !IsInvalid)
{
- Trace.WriteLine(string.Format("A {0} handle wrapper has not been properly disposed.", GetType().Name));
+ Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "A {0} handle wrapper has not been properly disposed.", GetType().Name));
#if LEAKS
Trace.WriteLine(trace);
#endif
@@ -718,6 +718,6 @@ public static bool RepositoryStateChecker(RepositorySafeHandle repositoryPtr, Fu
public static extern int git_treebuilder_write(out GitOid oid, RepositorySafeHandle repo, TreeBuilderSafeHandle bld);
[DllImport(libgit2)]
- public static extern int git_treebuilder_free(IntPtr bld);
+ public static extern void git_treebuilder_free(IntPtr bld);
}
}
@@ -1,5 +1,4 @@
-using System;
-using System.Runtime.InteropServices;
+using System.Runtime.InteropServices;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp.Core
@@ -31,7 +31,7 @@ public virtual IntPtr MarshalManagedToNative(object managedObj)
return StringToNative((string)managedObj);
}
- protected unsafe IntPtr StringToNative(string value)
+ protected static unsafe IntPtr StringToNative(string value)
{
// not null terminated
byte[] strbuf = Encoding.UTF8.GetBytes(value);
@@ -50,7 +50,7 @@ public virtual object MarshalNativeToManaged(IntPtr pNativeData)
return NativeToString(pNativeData);
}
- protected unsafe string NativeToString(IntPtr pNativeData)
+ protected static unsafe string NativeToString(IntPtr pNativeData)
{
var walk = (byte*)pNativeData;
@@ -8,6 +8,8 @@
<Word>sha</Word>
<Word>unstage</Word>
<Word>unstaged</Word>
+ <Word>compat</Word>
+ <Word>oid</Word>
</Recognized>
<Deprecated>
<Term PreferredAlternate=""></Term>
@@ -70,6 +70,9 @@ internal Diff(Repository repo)
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <paramref name = "oldTree"/> and the <paramref name = "newTree"/>.</returns>
public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<string> paths = null)
{
+ Ensure.ArgumentNotNull(oldTree, "oldTree");
+ Ensure.ArgumentNotNull(oldTree, "newTree");
+
using(GitDiffOptions options = BuildOptions(paths))
using (DiffListSafeHandle diff = BuildDiffListFromTrees(oldTree.Id, newTree.Id, options))
{
@@ -122,6 +125,8 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob)
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
public virtual TreeChanges Compare(Tree oldTree, DiffTarget diffTarget, IEnumerable<string> paths = null)
{
+ Ensure.ArgumentNotNull(oldTree, "oldTree");
+
var comparer = handleRetrieverDispatcher[diffTarget](repo);
using (GitDiffOptions options = BuildOptions(paths))
View
@@ -133,7 +133,7 @@ public virtual void Stage(IEnumerable<string> paths)
//TODO: Stage() should support following use cases:
// - Recursively staging the content of a directory
- IDictionary<string, FileStatus> batch = PrepareBatch(paths);
+ IEnumerable<KeyValuePair<string, FileStatus>> batch = PrepareBatch(paths);
foreach (KeyValuePair<string, FileStatus> kvp in batch)
{
@@ -289,7 +289,7 @@ public virtual void Remove(IEnumerable<string> paths)
//TODO: Remove() should support following use cases:
// - Removing a directory and its content
- IDictionary<string, FileStatus> batch = PrepareBatch(paths);
+ IEnumerable<KeyValuePair<string, FileStatus>> batch = PrepareBatch(paths);
foreach (KeyValuePair<string, FileStatus> keyValuePair in batch)
{
@@ -320,7 +320,7 @@ public virtual void Remove(IEnumerable<string> paths)
UpdatePhysicalIndex();
}
- private IDictionary<string, FileStatus> PrepareBatch(IEnumerable<string> paths)
+ private IEnumerable<KeyValuePair<string, FileStatus>> PrepareBatch(IEnumerable<string> paths)
{
Ensure.ArgumentNotNull(paths, "paths");
@@ -370,7 +370,7 @@ public virtual void Remove(IEnumerable<string> paths)
return new Tuple<string, FileStatus>(relativePath, RetrieveStatus(relativePath));
}
- private bool Enumerate(IEnumerator<string> leftEnum, IEnumerator<string> rightEnum)
+ private static bool Enumerate(IEnumerator<string> leftEnum, IEnumerator<string> rightEnum)
{
bool isLeftEoF = leftEnum.MoveNext();
bool isRightEoF = rightEnum.MoveNext();
@@ -484,7 +484,7 @@ internal void Reset(TreeChanges changes)
continue;
default:
- throw new InvalidOperationException(string.Format("Entry '{0}' bears an unexpected ChangeKind '{1}'", treeEntryChanges.Path, treeEntryChanges.Status));
+ throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Entry '{0}' bears an unexpected ChangeKind '{1}'", treeEntryChanges.Path, treeEntryChanges.Status));
}
}
@@ -1,4 +1,5 @@
using System;
+using System.Runtime.Serialization;
namespace LibGit2Sharp
{
@@ -38,6 +39,7 @@ public LibGit2Exception(string message, Exception innerException)
/// <summary>
/// The exception that is thrown when an error occurs during application execution.
/// </summary>
+ [Serializable]
public class LibGit2SharpException : Exception
{
/// <summary>
@@ -65,5 +67,15 @@ public LibGit2SharpException(string message, Exception innerException)
: base(message, innerException)
{
}
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref = "LibGit2SharpException" /> class with a serialized data.
+ /// </summary>
+ /// <param name = "info">The <see cref="SerializationInfo "/> that holds the serialized object data about the exception being thrown.</param>
+ /// <param name = "context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
+ protected LibGit2SharpException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
}
}
@@ -25,7 +25,7 @@ public enum Mode
/// <summary>
/// 100664 file mode
/// </summary>
- NonExecutableGroupWriteableFile = 0x81B4,
+ NonExecutableGroupWritableFile = 0x81B4,
/// <summary>
/// 100755 file mode
@@ -44,7 +44,7 @@ private Note(ObjectId blobId, string message, ObjectId targetObjectId, string @n
/// </summary>
public virtual ObjectId TargetObjectId { get; private set; }
- internal static Note BuildFromPtr(Repository repo, string @namespace, ObjectId targetObjectId, NoteSafeHandle note)
+ internal static Note BuildFromPtr(string @namespace, ObjectId targetObjectId, NoteSafeHandle note)
{
ObjectId oid = NativeMethods.git_note_oid(note).MarshalAsObjectId();
string message = NativeMethods.git_note_message(note);
@@ -78,7 +78,7 @@ internal IEnumerable<string> NamespaceRefs
return new[] { NormalizeToCanonicalName(DefaultNamespace) }.Concat(
from reference in repo.Refs
select reference.CanonicalName into refCanonical
- where refCanonical.StartsWith(refsNotesPrefix) && refCanonical != NormalizeToCanonicalName(DefaultNamespace)
+ where refCanonical.StartsWith(refsNotesPrefix, StringComparison.Ordinal) && refCanonical != NormalizeToCanonicalName(DefaultNamespace)
select refCanonical);
}
}
@@ -124,7 +124,7 @@ internal Note RetrieveNote(ObjectId targetObjectId, string canonicalNamespace)
return null;
}
- return Note.BuildFromPtr(repo, UnCanonicalizeName(canonicalNamespace), targetObjectId, noteHandle);
+ return Note.BuildFromPtr(UnCanonicalizeName(canonicalNamespace), targetObjectId, noteHandle);
}
}
@@ -153,7 +153,7 @@ private NoteSafeHandle BuildNoteSafeHandle(ObjectId id, string canonicalNamespac
return noteHandle;
}
- internal string NormalizeToCanonicalName(string name)
+ internal static string NormalizeToCanonicalName(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
@@ -37,6 +37,8 @@ internal ObjectDatabase(Repository repo)
/// <returns>True if the object has been found; false otherwise.</returns>
public virtual bool Contains(ObjectId objectId)
{
+ Ensure.ArgumentNotNull(objectId, "objectId");
+
var oid = objectId.Oid;
return NativeMethods.git_odb_exists(handle, ref oid) != (int)GitErrorCode.Ok;
@@ -72,6 +74,8 @@ public virtual Blob CreateBlob(string path)
/// <returns>The created <see cref = "Tree"/>.</returns>
public virtual Tree CreateTree(TreeDefinition treeDefinition)
{
+ Ensure.ArgumentNotNull(treeDefinition, "treeDefinition");
+
return treeDefinition.Build(repo);
}
@@ -15,12 +15,6 @@ public abstract class Reference : IEquatable<Reference>
new LambdaEqualityHelper<Reference>(new Func<Reference, object>[] { x => x.CanonicalName, x => x.TargetIdentifier });
/// <summary>
- /// Needed for mocking purposes.
- /// </summary>
- protected Reference()
- { }
-
- /// <summary>
/// Gets the full name of this reference.
/// </summary>
public virtual string CanonicalName { get; protected set; }
@@ -19,6 +19,8 @@ public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TO
private static readonly LambdaEqualityHelper<ReferenceWrapper<TObject>> equalityHelper =
new LambdaEqualityHelper<ReferenceWrapper<TObject>>(new Func<ReferenceWrapper<TObject>, object>[] { x => x.CanonicalName, x => x.TargetObject });
+ private readonly string canonicalName;
+
/// <summary>
/// Needed for mocking purposes.
/// </summary>
@@ -35,14 +37,17 @@ protected internal ReferenceWrapper(Repository repo, Reference reference, Func<R
Ensure.ArgumentNotNull(canonicalNameSelector, "canonicalNameSelector");
this.repo = repo;
- CanonicalName = canonicalNameSelector(reference);
+ canonicalName = canonicalNameSelector(reference);
objectBuilder = new Lazy<TObject>(() => RetrieveTargetObject(reference));
}
/// <summary>
/// Gets the full name of this reference.
/// </summary>
- public virtual string CanonicalName { get; protected set; }
+ public virtual string CanonicalName
+ {
+ get { return canonicalName; }
+ }
/// <summary>
/// Gets the name of this reference.
Oops, something went wrong.

0 comments on commit b62438b

Please sign in to comment.