10000 Merge pull request #1095 from whoisj/add-format-exceptions · libgit2/libgit2sharp@687b4e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 687b4e0

Browse files
committed
Merge pull request #1095 from whoisj/add-format-exceptions
Adding support for message formatting in exception c'tors
2 parents 325ddfc + 409a4a2 commit 687b4e0

File tree

13 files changed

+110
-42
lines changed

13 files changed

+110
-42
lines changed

LibGit2Sharp/AmbiguousSpecificationException.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.Globalization;
23
using System.Runtime.Serialization;
34

45
namespace LibGit2Sharp
@@ -25,6 +26,17 @@ public AmbiguousSpecificationException(string message)
2526
{
2627
}
2728

29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="AmbiguousSpecificationException"/> class with a specified error message.
31+
/// </summary>
32+
/// <param name="cultureInfo">An object that supplies culture-specific formatting information.</param>
33+
/// <param name="format">A composite format string for use in <see cref="String.Format(IFormatProvider, string, object[])"/>.</param>
34+
/// <param name="args">An object array that contains zero or more objects to format.</param>
35+
public AmbiguousSpecificationException(CultureInfo cultureInfo, string format, params object[] args)
36+
: base(String.Format(cultureInfo, format, args))
37+
{
38+
}
39+
2840
/// <summary>
2941
/// Initializes a new instance of the <see cref="AmbiguousSpecificationException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
3042
/// </summary>

LibGit2Sharp/BranchCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public virtual Branch Rename(Branch branch, string newName, bool allowOverwrite)
174174

175175
if (branch.IsRemote)
176176
{
177-
throw new LibGit2SharpException(
178-
string.Format(CultureInfo.InvariantCulture,
179-
"Cannot rename branch '{0}'. It's a remote tracking branch.", branch.FriendlyName));
177+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
178+
"Cannot rename branch '{0}'. It's a remote tracking branch.",
179+
branch.FriendlyName);
180180
}
181181

182182
using (ReferenceSafeHandle referencePtr = repo.Refs.RetrieveReferencePtr(Reference.LocalBranchPrefix + branch.FriendlyName))

LibGit2Sharp/Core/Proxy.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,8 +2334,9 @@ public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(R
23342334
return null;
23352335

23362336
case (int)GitErrorCode.Ambiguous:
2337-
throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture,
2338-
"Provided abbreviated ObjectId '{0}' is too short.", objectish));
2337+
throw new AmbiguousSpecificationException(CultureInfo.InvariantCulture,
2338+
"Provided abbreviated ObjectId '{0}' is too short.",
2339+
objectish);
23392340

23402341
default:
23412342
Ensure.ZeroResult(res);
@@ -2541,10 +2542,10 @@ public static FileStatus git_status_file(RepositorySafeHandle repo, FilePath pat
25412542
return FileStatus.Nonexistent;
25422543

25432544
case (int)GitErrorCode.Ambiguous:
2544-
throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture,
2545-
"More than one file matches the pathspec '{0}'. " +
2546-
"You can either force a literal path evaluation (GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH), or use git_status_foreach().",
2547-
path));
2545+
throw new AmbiguousSpecificationException(CultureInfo.InvariantCulture,
2546+
"More than one file matches the pathspec '{0}'. " +
2547+
"You can either force a literal path evaluation (GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH), or use git_status_foreach().",
2548+
path);
25482549

25492550
default:
25502551
Ensure.ZeroResult(res);

LibGit2Sharp/Diff.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
212212

213213
if (!ChangesBuilders.TryGetValue(typeof (T), out builder))
214214
{
215-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
216-
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.", typeof (T),
217-
typeof (TreeChanges), typeof (Patch)));
215+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
216+
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.",
217+
typeof(T),
218+
typeof(TreeChanges),
219+
typeof(Patch));
218220
}
219221

220222
var comparer = TreeToTree(repo);
@@ -323,9 +325,11 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
323325

324326
if (!ChangesBuilders.TryGetValue(typeof (T), out builder))
325327
{
326-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
327-
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.", typeof (T),
328-
typeof (TreeChanges), typeof (Patch)));
328+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
329+
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.",
330+
typeof(T),
331+
typeof(TreeChanges),
332+
typeof(Patch));
329333
}
330334

331335
var comparer = HandleRetrieverDispatcher[diffTargets](repo);
@@ -453,9 +457,11 @@ internal virtual T Compare<T>(DiffModifiers diffOptions, IEnumerable<string> pat
453457

454458
if (!ChangesBuilders.TryGetValue(typeof (T), out builder))
455459
{
456-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
457-
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.", typeof (T),
458-
typeof (TreeChanges), typeof (Patch)));
460+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
461+
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.",
462+
typeof(T),
463+
typeof(TreeChanges),
464+
typeof(Patch));
459465
}
460466

461467
var comparer = WorkdirToIndex(repo);

LibGit2Sharp/GitObject.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType
7373
case GitObjectType.Blob:
7474
return new Blob(repo, id);
7575
default:
76-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id));
76+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
77+
"Unexpected type '{0}' for object '{1}'.",
78+
type,
79+
id);
7780
}
7881
}
7982

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.Globalization;
23
using System.Runtime.Serialization;
34
using LibGit2Sharp.Core;
45

@@ -36,6 +37,17 @@ public LibGit2SharpException(string message, Exception innerException)
3637
{
3738
}
3839

40+
/// <summary>
41+
/// Initializes a new instance of the <see cref="LibGit2SharpException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
42+
/// </summary>
43+
/// <param name="cultureInfo">An object that supplies culture-specific formatting information.</param>
44+
/// <param name="format">A composite format string for use in <see cref="String.Format(IFormatProvider, string, object[])"/>.</param>
45+
/// <param name="args">An object array that contains zero or more objects to format.</param>
46+
public LibGit2SharpException(CultureInfo cultureInfo, string format, params object[] args)
47+
: base(String.Format(cultureInfo, format, args))
48+
{
49+
}
50+
3951
/// <summary>
4052
/// Initializes a new instance of the <see cref="LibGit2SharpException"/> class with a serialized data.
4153
/// </summary>

LibGit2Sharp/Reference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal static T BuildFromPtr<T>(ReferenceSafeHandle handle, Repository repo) w
5959
break;
6060

6161
default:
62-
throw new LibGit2SharpException(String.Format(CultureInfo.InvariantCulture, "Unable to build a new reference from a type '{0}'.", type));
62+
throw new LibGit2SharpException(CultureInfo.InvariantCulture, "Unable to build a new reference from a type '{0}'.", type);
6363
}
6464

6565
return reference as T;

LibGit2Sharp/ReferenceCollectionExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ public static Reference UpdateTarget(this ReferenceCollection refsColl, string n
256256
return refsColl.UpdateTarget(symbolicReference, targetRef, logMessage);
257257
}
258258

259-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Reference '{0}' has an unexpected type ('{1}').", name, reference.GetType()));
259+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
260+
"Reference '{0}' has an unexpected type ('{1}').",
261+
name,
262+
reference.GetType());
260263
}
261264

262265
/// <summary>

LibGit2Sharp/RemoveFromIndexException.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.Globalization;
23
using System.Runtime.Serialization;
34

45
namespace LibGit2Sharp
@@ -25,6 +26,17 @@ public RemoveFromIndexException(string message)
2526
{
2627
}
2728

29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="LibGit2SharpException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
31+
/// </summary>
32+
/// <param name="cultureInfo">An object that supplies culture-specific formatting information.</param>
33+
/// <param name="format">A composite format string for use in <see cref="String.Format(IFormatProvider, string, object[])"/>.</param>
34+
/// <param name="args">An object array that contains zero or more objects to format.</param>
35+
public RemoveFromIndexException(CultureInfo cultureInfo, string format, params object[] args)
36+
: base(cultureInfo, format, args)
37+
{
38+
}
39+
2840
/// <summary>
2941
/// Initializes a new instance of the <see cref="UnmatchedPathException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
3042
/// </summary>

LibGit2Sharp/Repository.cs

Lines changed: 26 additions & 17 deletions
< 57AE td data-grid-cell-id="diff-48c9ee988bd8966b3a596f35dc7687d6b9aa8b4731e9235961d05996a774dc7a-2118-2127-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">2127
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,7 @@ public Branch Checkout(Branch branch, CheckoutOptions options)
889889
// Make sure this is not an unborn branch.
890890
if (branch.Tip == null)
891891
{
892-
throw new UnbornBranchException(
893-
string.Format(CultureInfo.InvariantCulture,
894-
"The tip of branch '{0}' is null. There's nothing to checkout.", branch.FriendlyName));
892+
throw new UnbornBranchException(CultureInfo.InvariantCulture, "The tip of branch '{0}' is null. There's nothing to checkout.", branch.FriendlyName);
895893
}
896894

897895
if (!branch.IsRemote && !(branch is DetachedHead) &&
@@ -1550,7 +1548,7 @@ private MergeResult NormalMerge(GitAnnotatedCommitHandle[] annotatedCommits, Sig
15501548
Version = 1,
15511549
MergeFileFavorFlags = options.MergeFileFavor,
15521550
MergeTreeFlags = options.FindRenames ? GitMergeTreeFlags.GIT_MERGE_TREE_FIND_RENAMES :
1553-
GitMergeTreeFlags.GIT_MERGE_TREE_NORMAL,
1551+
GitMergeTreeFlags.GIT_MERGE_TREE_NORMAL,
15541552
RenameThreshold = (uint) options.RenameThreshold,
15551553
TargetLimit = (uint) options.TargetLimit,
15561554
};
@@ -1848,7 +1846,10 @@ public void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinatio
18481846
FileStatus sourceStatus = keyValuePair.Key.Item2;
18491847
if (sourceStatus.HasAny(new Enum[] { FileStatus.Nonexistent, FileStatus.DeletedFromIndex, FileStatus.NewInWorkdir, FileStatus.DeletedFromWorkdir }))
18501848
{
1851-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unable to move file '{0}'. Its current status is '{1}'.", sourcePath, sourceStatus));
1849+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
1850+
"Unable to move file '{0}'. Its current status is '{1}'.",
1851+
sourcePath,
1852+
sourceStatus);
18521853
}
18531854

18541855
FileStatus desStatus = keyValuePair.Value.Item2;
@@ -1857,7 +1858,10 @@ public void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinatio
18571858
continue;
18581859
}
18591860

1860-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unable to overwrite file '{0}'. Its current status is '{1}'.", destPath, desStatus));
1861+
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
1862+
"Unable to overwrite file '{0}'. Its current status is '{1}'.",
1863+
destPath,
1864+
desStatus);
18611865
}
18621866

18631867
string wd = Info.WorkingDirectory;
@@ -2090,29 +2094,34 @@ private IEnumerable<string> RemoveStagedItems(IEnumerable<string> paths, bool re
20902094
status.HasFlag(FileStatus.ModifiedInIndex) ||
20912095
status.HasFlag(FileStatus.NewInIndex) ))
20922096
{
2093-
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}', as it has changes staged in the index. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
2094-
treeEntryChanges.Path));
2097+
throw new RemoveFromIndexException(CultureInfo.InvariantCulture,
2098+
"Unable to remove file '{0}', as it has changes staged in the index. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
2099+
treeEntryChanges.Path);
20952100
}
20962101
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
20972102
continue;
20982103

20992104
case ChangeKind.Modified:
21002105
if (status.HasFlag(FileStatus.ModifiedInWorkdir) && status.HasFlag(FileStatus.ModifiedInIndex))
21012106
{
2102-
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}', as it has staged content different from both the working directory and the HEAD.",
2103-
treeEntryChanges.Path));
2107+
throw new RemoveFromIndexException(CultureInfo.InvariantCulture,
2108+
"Unable to remove file '{0}', as it has staged content different from both the working directory and the HEAD.",
2109+
treeEntryChanges.Path);
21042110
}
21052111
if (removeFromWorkingDirectory)
21062112
{
2107-
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}', as it has local modifications. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
2108-
treeEntryChanges.Path));
2113+
throw new RemoveFromIndexException(CultureInfo.InvariantCulture,
2114+
"Unable to remove file '{0}', as it has local modifications. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
2115+
treeEntryChanges.Path);
21092116
}
21102117
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
21112118
continue;
21122119

21132120
default:
2114-
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}'. Its current status is '{1}'.",
2115-
treeEntryChanges.Path, treeEntryChanges.Status));
2121+
throw new RemoveFromIndexException(CultureInfo.InvariantCulture,
2122+
"Unable to remove file '{0}'. Its current status is '{1}'.",
2123+
treeEntryChanges.Path,
2124+
treeEntryChanges.Status);
21162125
}
21172126
}
2118

@@ -2148,9 +2157,9 @@ private string DebuggerDisplay
21482157
get
21492158
{
21502159
return string.Format(CultureInfo.InvariantCulture,
2151-
"{0} = \"{1}\"",
2152-
Info.IsBare ? "Gitdir" : "Workdir",
2153-
Info.IsBare ? Info.Path : Info.WorkingDirectory);
2160+
"{0} = \"{1}\"",
2161+
Info.IsBare ? "Gitdir" : "Workdir",
2162+
Info.IsBare ? Info.Path : Info.WorkingDirectory);
21542163
}
21552164
}
21562165
}

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ internal static IEnumerable<ObjectId> Committishes(this Repository repo, object
466466

467467
if (throwIfNotFound)
468468
{
469-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier));
469+
throw new LibGit2SharpException(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier);
470470
}
471471

472472
yield return null;

LibGit2Sharp/SubmoduleCollection.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ internal T Lookup<T>(string name, Func<SubmoduleSafeHandle, T> selector, bool th
160160

161161
if (throwIfNotFound)
162162
{
163-
throw new LibGit2SharpException(string.Format(
164-
CultureInfo.InvariantCulture,
165-
"Submodule lookup failed for '{0}'.", name));
163+
throw new LibGit2SharpException(CultureInfo.InvariantCulture, "Submodule lookup failed for '{0}'.", name);
166164
}
167165

168166
return default(T);

0 commit comments

Comments
 (0)
0