8000 Fix some issues pinpointed by Code Analysis · dotjosh/libgit2sharp@8e14827 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e14827

Browse files
committed
Fix some issues pinpointed by Code Analysis
1 parent 35d1425 commit 8e14827

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

LibGit2Sharp/AbbreviatedObjectId.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 LibGit2Sharp.Core;
34

45
namespace LibGit2Sharp
@@ -9,7 +10,7 @@ internal AbbreviatedObjectId(GitOid oid, int length) : base(oid)
910
{
1011
if (length < MinHexSize || length > HexSize)
1112
{
12-
throw new ArgumentException(string.Format("Expected length should be comprised between {0} and {1}.", MinHexSize, HexSize), "length");
13+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Expected length should be comprised between {0} and {1}.", MinHexSize, HexSize), "length");
1314
}
1415

1516
Length = length;

LibGit2Sharp/BranchCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void Delete(string name)
116116

117117
if (canonicalName == repo.Head.CanonicalName)
118118
{
119-
throw new LibGit2Exception(string.Format("Branch '{0}' can not be deleted as it is the current HEAD.", canonicalName));
119+
throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Branch '{0}' can not be deleted as it is the current HEAD.", canonicalName));
120120
}
121121

122122
//TODO: To be replaced by native libgit2 git_branch_delete() when available.

LibGit2Sharp/Configuration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public T Get<T>(string key, T defaultValue)
196196

197197
if (!configurationTypedRetriever.ContainsKey(typeof(T)))
198198
{
199-
throw new ArgumentException(string.Format("Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
199+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
200200
}
201201

202202
ConfigurationSafeHandle handle = (LocalHandle ?? globalHandle) ?? systemHandle;
@@ -394,7 +394,7 @@ public void Set<T>(string key, T value, ConfigurationLevel level = Configuration
394394

395395
if (!configurationTypedUpdater.ContainsKey(typeof(T)))
396396
{
397-
throw new ArgumentException(string.Format("Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
397+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
398398
}
399399

400400
configurationTypedUpdater[typeof(T)](key, value, h);

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 5 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.IO;
34
using System.Reflection;
45
using System.Runtime.InteropServices;
@@ -23,7 +24,7 @@ static NativeMethods()
2324

2425
const string pathEnvVariable = "PATH";
2526
Environment.SetEnvironmentVariable(pathEnvVariable,
26-
String.Format("{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
27+
String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
2728
}
2829

2930
git_threads_init();
@@ -109,6 +110,7 @@ public static extern int git_commit_create(
109110
public static extern int git_config_get_bool(
110111
ConfigurationSafeHandle cfg,
111112
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name,
113+
[MarshalAs(UnmanagedType.Bool)]
112114
out bool value);
113115

114116
[DllImport(libgit2)]
@@ -397,6 +399,7 @@ public static extern int git_tag_create(
397399
IntPtr target,
398400
SignatureSafeHandle signature,
399401
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message,
402+
[MarshalAs(UnmanagedType.Bool)]
400403
bool force);
401404

402405
[DllImport(libgit2)]
@@ -405,6 +408,7 @@ public static extern int git_tag_create_lightweight(
405408
RepositorySafeHandle repo,
406409
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name,
407410
IntPtr target,
411+
[MarshalAs(UnmanagedType.Bool)]
408412
bool force);
409413

410414
[DllImport(libgit2)]

LibGit2Sharp/Index.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void Stage(IEnumerable<string> paths)
157157
continue;
158158
}
159159

160-
throw new LibGit2Exception(string.Format("Can not stage '{0}'. The file does not exist.", kvp.Key));
160+
throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Can not stage '{0}'. The file does not exist.", kvp.Key));
161161
}
162162

163163
foreach (KeyValuePair<string, FileStatus> kvp in batch)
@@ -274,7 +274,7 @@ public void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinatio
274274
FileStatus sourceStatus = keyValuePair.Key.Item2;
275275
if (sourceStatus.HasAny(new[] { FileStatus.Nonexistent, FileStatus.Removed, FileStatus.Untracked, FileStatus.Missing }))
276276
{
277-
throw new LibGit2Exception(string.Format("Unable to move file '{0}'. Its current status is '{1}'.", sourcePath, Enum.GetName(typeof(FileStatus), sourceStatus)));
277+
throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unable to move file '{0}'. Its current status is '{1}'.", sourcePath, Enum.GetName(typeof(FileStatus), sourceStatus)));
278278
}
279279

280280
FileStatus desStatus = keyValuePair.Value.Item2;
@@ -283,7 +283,7 @@ public void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinatio
283283
continue;
284284
}
285285

286-
throw new LibGit2Exception(string.Format("Unable to overwrite file '{0}'. Its current status is '{1}'.", destPath, Enum.GetName(typeof(FileStatus), desStatus)));
286+
throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unable to overwrite file '{0}'. Its current status is '{1}'.", destPath, Enum.GetName(typeof(FileStatus), desStatus)));
287287
}
288288

289289
string wd = repo.Info.WorkingDirectory;
@@ -347,7 +347,7 @@ public void Remove(IEnumerable<string> paths)
347347
continue;
348348
}
349349

350-
throw new LibGit2Exception(string.Format("Unable to remove file '{0}'. Its current status is '{1}'.", keyValuePair.Key, Enum.GetName(typeof(FileStatus), keyValuePair.Value)));
350+
throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}'. Its current status is '{1}'.", keyValuePair.Key, Enum.GetName(typeof(FileStatus), keyValuePair.Value)));
351351
}
352352

353353
string wd = repo.Info.WorkingDirectory;
@@ -449,7 +449,7 @@ private void RestorePotentialPreviousVersionOfHeadIntoIndex(string relativePath)
449449
Path = utf8Marshaler.MarshalManagedToNative(relativePath),
450450
};
451451

452-
NativeMethods.git_index_add2(handle, indexEntry);
452+
Ensure.Success(NativeMethods.git_index_add2(handle, indexEntry));
453453
utf8Marshaler.CleanUpNativeData(indexEntry.Path);
454454
}
455455

LibGit2Sharp/ObjectId.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public ObjectId(string sha)
6262

6363
if (!parsedOid.HasValue)
6464
{
65-
throw new ArgumentException(string.Format("'{0}' is not a valid Sha-1.", sha));
65+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid Sha-1.", sha));
6666
}
6767

6868
oid = parsedOid.Value;
@@ -234,7 +234,7 @@ private static string ToString(byte[] id)
234234
{
235235
if (id == null || id.Length != rawSize)
236236
{
237-
throw new ArgumentException("id");
237+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A non null array of {0} bytes is expected.", rawSize), "id");
238238
}
239239

240240
// Inspired from http://stackoverflow.com/questions/623104/c-byte-to-hex-string/3974535#3974535
@@ -294,13 +294,12 @@ private static bool LooksValid(string objectId, bool throwIfInvalid, bool allowS
294294
return false;
295295
}
296296

297-
string additionalErrorInformation =
298-
!allowShortIdentifier ?
299-
string.Format("Its length should be {0}", HexSize) :
300-
string.Format("Its length should be comprised between {0} and {1}", MinHexSize, HexSize);
297+
string additionalErrorInformation = !allowShortIdentifier ?
298+
string.Format(CultureInfo.InvariantCulture, "Its length should be {0}", HexSize) :
299+
string.Format(CultureInfo.InvariantCulture, "Its length should be comprised between {0} and {1}", MinHexSize, HexSize);
301300

302301
throw new ArgumentException(
303-
string.Format("'{0}' is not a valid object identifier. {1}.", objectId, additionalErrorInformation),
302+
string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid object identifier. {1}.", objectId, additionalErrorInformation),
304303
"objectId");
305304
}
306305

0 commit comments

Comments
 (0)
0