10000 Adding support for message formatting in exception c'tors by whoisj · Pull Request #1095 · libgit2/libgit2sharp · GitHub
[go: up one dir, main page]

Skip to content

Adding support for message formatting in exception c'tors #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions LibGit2Sharp/AmbiguousSpecificationException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Runtime.Serialization;

namespace LibGit2Sharp
Expand All @@ -25,6 +26,17 @@ public AmbiguousSpecificationException(string message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AmbiguousSpecificationException"/> class with a specified error message.
/// </summary>
/// <param name="cultureInfo">An object that supplies culture-specific formatting information.</param>
/// <param name="format">A composite format string for use in <see cref="String.Format(IFormatProvider, string, object[])"/>.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public AmbiguousSpecificationException(CultureInfo cultureInfo, string format, params object[] args)
: base(String.Format(cultureInfo, format, args))
{
}

/// <summary>
/// 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.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public virtual Branch Rename(Branch branch, string newName, bool allowOverwrite)

if (branch.IsRemote)
{
throw new LibGit2SharpException(
string.Format(CultureInfo.InvariantCulture,
"Cannot rename branch '{0}'. It's a remote tracking branch.", branch.FriendlyName));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Cannot rename branch '{0}'. It's a remote tracking branch.",
branch.FriendlyName);
}

using (ReferenceSafeHandle referencePtr = repo.Refs.RetrieveReferencePtr(Reference.LocalBranchPrefix + branch.FriendlyName))
Expand Down
13 changes: 7 additions & 6 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,8 +2334,9 @@ public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(R
return null;

case (int)GitErrorCode.Ambiguous:
throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture,
"Provided abbreviated ObjectId '{0}' is too 8000 short.", objectish));
throw new AmbiguousSpecificationException(CultureInfo.InvariantCulture,
"Provided abbreviated ObjectId '{0}' is too short.",
objectish);

default:
Ensure.ZeroResult(res);
Expand Down Expand Up @@ -2541,10 +2542,10 @@ public static FileStatus git_status_file(RepositorySafeHandle repo, FilePath pat
return FileStatus.Nonexistent;

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

default:
Ensure.ZeroResult(res);
Expand Down
24 changes: 15 additions & 9 deletions LibGit2Sharp/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path

if (!ChangesBuilders.TryGetValue(typeof (T), out builder))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance you could move the beautifications in a separate commit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@#$%! I thought I got that all cleaned up 🐼

{
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.", typeof (T),
typeof (TreeChanges), typeof (Patch)));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.",
typeof(T),
typeof(TreeChanges),
typeof(Patch));
}

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

if (!ChangesBuilders.TryGetValue(typeof (T), out builder))
{
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.", typeof (T),
typeof (TreeChanges), typeof (Patch)));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.",
typeof(T),
typeof(TreeChanges),
typeof(Patch));
}

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

if (!ChangesBuilders.TryGetValue(typeof (T), out builder))
{
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.", typeof (T),
typeof (TreeChanges), typeof (Patch)));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'.",
typeof(T),
typeof(TreeChanges),
typeof(Patch));
}

var comparer = WorkdirToIndex(repo);
Expand Down
5 changes: 4 additions & 1 deletion LibGit2Sharp/GitObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType
case GitObjectType.Blob:
return new Blob(repo, id);
default:
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Unexpected type '{0}' for object '{1}'.",
type,
id);
}
}

Expand Down
12 changes: 12 additions & 0 deletions LibGit2Sharp/LibGit2SharpException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Runtime.Serialization;
using LibGit2Sharp.Core;

Expand Down Expand Up @@ -36,6 +37,17 @@ public LibGit2SharpException(string message, Exception innerException)
{
}

/// <summary>
/// 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.
/// </summary>
/// <param name="cultureInfo">An object that supplies culture-specific formatting information.</param>
/// <param name="format">A composite format string for use in <see cref="String.Format(IFormatProvider, string, object[])"/>.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public LibGit2SharpException(CultureInfo cultureInfo, string format, params object[] args)
: base(String.Format(cultureInfo, format, args))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="LibGit2SharpException"/> class with a serialized data.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static T BuildFromPtr<T>(ReferenceSafeHandle handle, Repository repo) w
break;

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

return reference as T;
Expand Down
5 changes: 4 additions & 1 deletion LibGit2Sharp/ReferenceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ public static Reference UpdateTarget(this ReferenceCollection refsColl, string n
return refsColl.UpdateTarget(symbolicReference, targetRef, logMessage);
}

throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Reference '{0}' has an unexpected type ('{1}').", name, reference.GetType()));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Reference '{0}' has an unexpected type ('{1}').",
name,
reference.GetType());
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions LibGit2Sharp/RemoveFromIndexException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Runtime.Serialization;

namespace LibGit2Sharp
Expand All @@ -25,6 +26,17 @@ public RemoveFromIndexException(string message)
{
}

/// <summary>
/// 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.
/// </summary>
/// <param name="cultureInfo">An object that supplies culture-specific formatting information.</param>
/// <param name="format">A composite format string for use in <see cref="String.Format(IFormatProvider, string, object[])"/>.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public RemoveFromIndexException(CultureInfo cultureInfo, string format, params object[] args)
: base(cultureInfo, format, args)
{
}

/// <summary>
/// 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.
/// </summary>
Expand Down
43 changes: 26 additions & 17 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,7 @@ public Branch Checkout(Branch branch, CheckoutOptions options)
// Make sure this is not an unborn branch.
if (branch.Tip == null)
{
throw new UnbornBranchException(
string.Format(CultureInfo.InvariantCulture,
"The tip of branch '{0}' is null. There's nothing to checkout.", branch.FriendlyName));
throw new UnbornBranchException(CultureInfo.InvariantCulture, "The tip of branch '{0}' is null. There's nothing to checkout.", branch.FriendlyName);
}

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

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

throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unable to overwrite file '{0}'. Its current status is '{1}'.", destPath, desStatus));
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"Unable to overwrite file '{0}'. Its current status is '{1}'.",
destPath,
desStatus);
}

string wd = Info.WorkingDirectory;
Expand Down Expand Up @@ -2090,29 +2094,34 @@ private IEnumerable<string> RemoveStagedItems(IEnumerable<string> paths, bool re
status.HasFlag(FileStatus.ModifiedInIndex) ||
status.HasFlag(FileStatus.NewInIndex) ))
{
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.",
treeEntryChanges.Path));
throw new RemoveFromIndexException(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.",
treeEntryChanges.Path);
}
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
continue;

case ChangeKind.Modified:
if (status.HasFlag(FileStatus.ModifiedInWorkdir) && status.HasFlag(FileStatus.ModifiedInIndex))
{
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.",
treeEntryChanges.Path));
throw new RemoveFromIndexException(CultureInfo.InvariantCulture,
"Unable to remove file '{0}', as it has staged content different from both the working directory and the HEAD.",
treeEntryChanges.Path);
}
if (removeFromWorkingDirectory)
{
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.",
treeEntryChanges.Path));
throw new RemoveFromIndexException(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.",
treeEntryChanges.Path);
}
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
continue;

default:
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}'. Its current status is '{1}'.",
treeEntryChanges.Path, treeEntryChanges.Status));
throw new RemoveFromIndexException(CultureInfo.InvariantCulture,
"Unable to remove file '{0}'. Its current status is '{1}'.",
treeEntryChanges.Path,
treeEntryChanges.Status);
}
}

Expand Down Expand Up @@ -2148,9 +2157,9 @@ private string DebuggerDisplay
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0} = \"{1}\"",
Info.IsBare ? "Gitdir" : "Workdir",
Info.IsBare ? Info.Path : Info.WorkingDirectory);
"{0} = \"{1}\"",
Info.IsBare ? "Gitdir" : "Workdir",
Info.IsBare ? Info.Path : Info.WorkingDirectory);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/RepositoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ internal static IEnumerable<ObjectId> Committishes(this Repository repo, object

if (throwIfNotFound)
{
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier));
throw new LibGit2SharpException(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier);
}

yield return null;
Expand Down
4 changes: 1 addition & 3 deletions LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ internal T Lookup<T>(string name, Func<SubmoduleSafeHandle, T> selector, bool th

if (throwIfNotFound)
{
throw new LibGit2SharpException(string.Format(
CultureInfo.InvariantCulture,
"Submodule lookup failed for '{0}'.", name));
throw new LibGit2SharpException(CultureInfo.InvariantCulture, "Submodule lookup failed for '{0}'.", name);
}

return default(T);
Expand Down
Loading
0