Skip to content

Commit ac394b4

Browse files
committed
Fix some issues pinpointed by Code Analysis
1 parent 35ea14d commit ac394b4

35 files changed

+120
-153
lines changed

LibGit2Sharp/BranchUpdater.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void SetUpstream(string upstreamBranchName)
125125
/// <param name="mergeBranchName">The merge branch in the upstream remote's namespace.</param>
126126
private void SetUpstreamBranch(string mergeBranchName)
127127
{
128-
string configKey = string.Format("branch.{0}.merge", branch.Name);
128+
string configKey = string.Format(CultureInfo.InvariantCulture, "branch.{0}.merge", branch.Name);
129129

130130
if (string.IsNullOrEmpty(mergeBranchName))
131131
{
@@ -143,7 +143,7 @@ private void SetUpstreamBranch(string mergeBranchName)
143143
/// <param name="remoteName">The name of the remote to set as the upstream branch.</param>
144144
private void SetUpstreamRemote(string remoteName)
145145
{
146-
string configKey = string.Format("branch.{0}.remote", branch.Name);
146+
string configKey = string.Format(CultureInfo.InvariantCulture, "branch.{0}.remote", branch.Name);
147147

148148
if (string.IsNullOrEmpty(remoteName))
149149
{

LibGit2Sharp/Configuration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Globalization;
44
using System.IO;
55
using System.Linq;
6-
using System.Runtime.InteropServices;
76
using LibGit2Sharp.Core;
87
using LibGit2Sharp.Core.Handles;
98

@@ -346,7 +345,8 @@ internal Signature BuildSignature(DateTimeOffset now, bool shouldThrowIfNotFound
346345

347346
return new Signature(
348347
name != null ? name.Value : "unknown",
349-
email != null ? email.Value : string.Format("{0}@{1}", Environment.UserName, Environment.UserDomainName),
348+
email != null ? email.Value : string.Format(
349+
CultureInfo.InvariantCulture, "{0}@{1}", Environment.UserName, Environment.UserDomainName),
350350
now);
351351
}
352352
}

LibGit2Sharp/Core/EncodingMarshaler.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
using System.Text;
56

@@ -43,7 +44,7 @@ public virtual IntPtr MarshalManagedToNative(Object managedObj)
4344
if (str == null)
4445
{
4546
throw new MarshalDirectiveException(
46-
string.Format("{0} must be used on a string.", GetType().Name));
47+
string.Format(CultureInfo.InvariantCulture, "{0} must be used on a string.", GetType().Name));
4748
}
4849

4950
return FromManaged(encoding, str);

LibGit2Sharp/Core/Ensure.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public static void ArgumentDoesNotContainZeroByte(string argumentValue, string a
6767
}
6868

6969
throw new ArgumentException(
70-
string.Format("Zero bytes ('\\0') are not allowed. A zero byte has been found at position {0}.", zeroPos), argumentName);
70+
string.Format(CultureInfo.InvariantCulture,
71+
"Zero bytes ('\\0') are not allowed. A zero byte has been found at position {0}.", zeroPos), argumentName);
7172
}
7273

7374
private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException>>

LibGit2Sharp/Core/FilePathMarshaler.cs

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

@@ -69,7 +70,7 @@ public override IntPtr MarshalManagedToNative(Object managedObj)
6970
if (null == filePath)
7071
{
7172
throw new MarshalDirectiveException(
72-
string.Format("{0} must be used on a FilePath.", GetType().Name));
73+
string.Format(CultureInfo.InvariantCulture, "{0} must be used on a FilePath.", GetType().Name));
7374
}
7475

7576
return FromManaged(filePath);

LibGit2Sharp/Core/GitBlame.cs

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

45
namespace LibGit2Sharp.Core
@@ -78,7 +79,8 @@ public static GitBlameOptionFlags ToGitBlameOptionFlags(this BlameStrategy strat
7879
return GitBlameOptionFlags.GIT_BLAME_NORMAL;
7980

8081
default:
81-
throw new NotSupportedException(string.Format("{0} is not supported at this time", strategy));
82+
throw new NotSupportedException(
83+
string.Format(CultureInfo.InvariantCulture, "{0} is not supported at this time", strategy));
8284
}
8385
}
8486
}

LibGit2Sharp/Core/GitMergeOpts.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ internal struct GitMergeOpts
3333
public GitMergeFileFavorFlags MergeFileFavorFlags;
3434
}
3535

36-
/// <summary>
37-
/// The results of `git_merge_analysis` indicate the merge opportunities.
38-
/// </summary>
36+
/// <summary>
37+
/// The results of `git_merge_analysis` indicate the merge opportunities.
38+
/// </summary>
39+
[Flags]
3940
internal enum GitMergeAnalysis
4041
{
4142
/// <summary>
@@ -44,8 +45,8 @@ internal enum GitMergeAnalysis
4445
GIT_MERGE_ANALYSIS_NONE = 0,
4546

4647
/// <summary>
47-
/// A "normal" merge; both HEAD and the given merge input have diverged
48-
/// from their common ancestor. The divergent commits must be merged.
48+
/// A "normal" merge; both HEAD and the given merge input have diverged
49+
/// from their common ancestor. The divergent commits must be merged.
4950
/// </summary>
5051
GIT_MERGE_ANALYSIS_NORMAL = (1 << 0),
5152

LibGit2Sharp/Core/GitObjectType.cs

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

34
namespace LibGit2Sharp.Core
45
{
@@ -74,7 +75,8 @@ public static TreeEntryTargetType ToTreeEntryTargetType(this GitObjectType type)
7475
return TreeEntryTargetType.Blob;
7576

7677
default:
77-
throw new InvalidOperationException(string.Format("Cannot map {0} to a TreeEntryTargetType.", type));
78+
throw new InvalidOperationException(
79+
string.Format(CultureInfo.InvariantCulture, "Cannot map {0} to a TreeEntryTargetType.", type));
7880
}
7981
}
8082

@@ -95,7 +97,8 @@ public static ObjectType ToObjectType(this GitObjectType type)
9597
return ObjectType.Tag;
9698

9799
default:
98-
throw new InvalidOperationException(string.Format("Cannot map {0} to a ObjectType.", type));
100+
throw new InvalidOperationException(
101+
string.Format(CultureInfo.InvariantCulture, "Cannot map {0} to a ObjectType.", type));
99102
}
100103
}
101104
}

LibGit2Sharp/Core/Handles/StatusEntrySafeHandle.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Runtime.InteropServices;
5-
using System.Text;
62

73
namespace LibGit2Sharp.Core.Handles
84
{

LibGit2Sharp/Core/HistoryRewriter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Globalization;
45
using System.Linq;
56

67
namespace LibGit2Sharp.Core
@@ -157,7 +158,9 @@ private Reference RewriteReference<TRef, TTarget>(
157158
if (repo.Refs.Resolve<Reference>(backupName) != null)
158159
{
159160
throw new InvalidOperationException(
160-
String.Format("Can't back up reference '{0}' - '{1}' already exists", oldRef.CanonicalName, backupName));
161+
String.Format(
162+
CultureInfo.InvariantCulture, "Can't back up reference '{0}' - '{1}' already exists",
163+
oldRef.CanonicalName, backupName));
161164
}
162165

163166
repo.Refs.Add(backupName, oldRef.TargetIdentifier, signature, "filter-branch: backup");

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ internal static extern int git_blame_file(
122122
GitBlameOptions options);
123123

124124
[DllImport(libgit2)]
125-
internal static extern int git_blame_free(IntPtr blame);
125+
internal static extern void git_blame_free(IntPtr blame);
126126

127127
[DllImport(libgit2)]
128128
internal static extern int git_blob_create_fromdisk(
@@ -306,7 +306,7 @@ internal static extern int git_config_add_file_ondisk(
306306
ConfigurationSafeHandle cfg,
307307
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path,
308308
uint level,
309-
bool force);
309+
[MarshalAs(UnmanagedType.Bool)] bool force);
310310

311311
[DllImport(libgit2)]
312312
internal static extern int git_config_new(out ConfigurationSafeHandle cfg);
@@ -455,13 +455,6 @@ internal delegate int git_diff_line_cb(
455455
[In] GitDiffLine line,
456456
IntPtr payload);
457457

458-
[DllImport(libgit2)]
459-
internal static extern int git_diff_print(
460-
DiffSafeHandle diff,
461-
GitDiffFormat format,
462-
git_diff_line_cb printCallback,
463-
IntPtr payload);
464-
465458
[DllImport(libgit2)]
466459
internal static extern int git_diff_blobs(
467460
GitObjectSafeHandle oldBlob,
@@ -540,12 +533,6 @@ internal static extern int git_index_conflict_get(
540533
[DllImport(libgit2)]
541534
internal static extern int git_index_entry_stage(IndexEntrySafeHandle indexentry);
542535

543-
[DllImport(libgit2)]
544-
internal static extern int git_index_find(
545-
out UIntPtr pos,
546-
IndexSafeHandle index,
547-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
548-
549536
[DllImport(libgit2)]
550537
internal static extern void git_index_free(IntPtr index);
551538

@@ -567,7 +554,9 @@ internal static extern int git_index_open(
567554
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath indexpath);
568555

569556
[DllImport(libgit2)]
570-
internal static extern int git_index_read(IndexSafeHandle index, bool force);
557+
internal static extern int git_index_read(
558+
IndexSafeHandle index,
559+
[MarshalAs(UnmanagedType.Bool)] bool force);
571560

572561
[DllImport(libgit2)]
573562
internal static extern int git_index_remove_bypath(
@@ -641,7 +630,7 @@ internal static extern void git_merge_head_free(
641630
internal static extern int git_message_prettify(
642631
GitBuf buf,
643632
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string message,
644-
bool strip_comments);
633+
[MarshalAs(UnmanagedType.Bool)] bool strip_comments);
645634

646635
[DllImport(libgit2)]
647636
internal static extern int git_note_create(
@@ -925,16 +914,6 @@ internal static extern OidSafeHandle git_reflog_entry_id_new(
925914
internal static extern IntPtr git_reflog_entry_committer(
926915
SafeHandle entry);
927916

928-
[DllImport(libgit2)]
929-
internal static extern int git_reflog_append(
930-
ReflogSafeHandle reflog,
931-
ref GitOid id,
932-
SignatureSafeHandle committer,
933-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string msg);
934-
935-
[DllImport(libgit2)]
936-
internal static extern int git_reflog_write(ReflogSafeHandle reflog);
937-
938917
[DllImport(libgit2)]
939918
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
940919
internal static extern string git_reflog_entry_message(SafeHandle entry);
@@ -1104,9 +1083,6 @@ internal static extern int git_repository_init_ext(
11041083
[DllImport(libgit2)]
11051084
internal static extern int git_repository_is_bare(RepositorySafeHandle handle);
11061085

1107-
[DllImport(libgit2)]
1108-
internal static extern int git_repository_is_empty(RepositorySafeHandle repo);
1109-
11101086
[DllImport(libgit2)]
11111087
internal static extern int git_repository_is_shallow(RepositorySafeHandle repo);
11121088

@@ -1161,7 +1137,7 @@ internal static extern void git_repository_set_index(
11611137
internal static extern int git_repository_set_workdir(
11621138
RepositorySafeHandle repository,
11631139
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath workdir,
1164-
bool update_gitlink);
1140+
[MarshalAs(UnmanagedType.Bool)] bool update_gitlink);
11651141

11661142
[DllImport(libgit2)]
11671143
internal static extern int git_repository_set_head_detached(
@@ -1307,7 +1283,7 @@ internal static extern int git_submodule_foreach(
13071283
[DllImport(libgit2)]
13081284
internal static extern int git_submodule_add_to_index(
13091285
SubmoduleSafeHandle submodule,
1310-
bool write_index);
1286+
[MarshalAs(UnmanagedType.Bool)] bool write_index);
13111287

13121288
[DllImport(libgit2)]
13131289
internal static extern int git_submodule_save(

LibGit2Sharp/Core/Proxy.cs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,6 @@ public static UnmanagedMemoryStream git_blob_filtered_content_stream(RepositoryS
114114
new[] { buf });
115115
}
116116

117-
public static byte[] git_blob_rawcontent(RepositorySafeHandle repo, ObjectId id, int size)
118-
{
119-
using (var obj = new ObjectSafeWrapper(id, repo))
120-
{
121-
var arr = new byte[size];
122-
Marshal.Copy(NativeMethods.git_blob_rawcontent(obj.ObjectPtr), arr, 0, size);
123-
return arr;
124-
}
125-
}
126-
127117
public static UnmanagedMemoryStream git_blob_rawcontent_stream(RepositorySafeHandle repo, ObjectId id, Int64 size)
128118
{
129119
var handle = new ObjectSafeWrapper(id, repo).ObjectPtr;
@@ -649,16 +639,6 @@ public static void git_diff_merge(DiffSafeHandle onto, DiffSafeHandle from)
649639
}
650640
}
651641

652-
public static void git_diff_print(DiffSafeHandle diff, NativeMethods.git_diff_line_cb printCallback)
653-
{
654-
using (ThreadAffinity())
655-
{
656-
int res = NativeMethods.git_diff_print(diff, GitDiffFormat.GIT_DIFF_FORMAT_PATCH,
657-
printCallback, IntPtr.Zero);
658-
Ensure.ZeroResult(res);
659-
}
660-
}
661-
662642
public static DiffSafeHandle git_diff_tree_to_tree(
663643
RepositorySafeHandle repo,
664644
ObjectId oldTree,
@@ -1648,21 +1628,6 @@ public static string git_reflog_entry_message(SafeHandle entry)
16481628
return NativeMethods.git_reflog_entry_message(entry);
16491629
}
16501630

1651-
public static void git_reflog_append(ReflogSafeHandle reflog, ObjectId commit_id, Signature committer, string message)
1652-
{
1653-
using (ThreadAffinity())
1654-
using (SignatureSafeHandle sigHandle = committer.BuildHandle())
1655-
{
1656-
var oid = commit_id.Oid;
1657-
1658-
int res = NativeMethods.git_reflog_append(reflog, ref oid, sigHandle, message);
1659-
Ensure.ZeroResult(res);
1660-
1661-
res = NativeMethods.git_reflog_write(reflog);
1662-
Ensure.ZeroResult(res);
1663-
}
1664-
}
1665-
16661631
#endregion
16671632

16681633
#region git_refspec
@@ -2005,11 +1970,6 @@ public static bool git_repository_is_bare(RepositorySafeHandle repo)
20051970
return RepositoryStateChecker(repo, NativeMethods.git_repository_is_bare);
20061971
}
20071972

2008-
public static bool git_repository_is_empty(RepositorySafeHandle repo)
2009-
{
2010-
return RepositoryStateChecker(repo, NativeMethods.git_repository_is_empty);
2011-
}
2012-
20131973
public static bool git_repository_is_shallow(RepositorySafeHandle repo)
20141974
{
20151975
return RepositoryStateChecker(repo, NativeMethods.git_repository_is_shallow);

0 commit comments

Comments
 (0)