Skip to content
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
18 changes: 18 additions & 0 deletions GitIntegration.Test/Builders/GitCommitBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@
.ConfigureAwait(false);
}

[TestMethod]
public async Task ThrowsNothingToCommitWhenTrackedChangesAreUnstagedAsync()
{
// The third phrasing, and the most common case: a tracked file is modified but nothing is
// staged. It contains neither of the other two phrases.
ScriptedGitProcessRunner runner = new ScriptedGitProcessRunner()
.Then(
standardOutput:
"On branch main\nChanges not staged for commit:\n\tmodified: f\n\n" +
"no changes added to commit (use \"git add\" and/or \"git commit -a\")\n",
exitCode: 1);
GitCommitBuilder builder = new(runner, TestPaths.Root, Message);

await Assert.ThrowsExactlyAsync<GitNothingToCommitException>(
async () => await builder.ExecuteAsync(TestContext.CancellationTokenSource.Token).ConfigureAwait(false))

Check warning on line 220 in GitIntegration.Test/Builders/GitCommitBuilderTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'TestContext.CancellationToken' instead of 'TestContext.CancellationTokenSource.Token'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_GitIntegration&issues=AaEeRtY_s2OjFVrUtqDO&open=AaEeRtY_s2OjFVrUtqDO&pullRequest=199
.ConfigureAwait(false);
}

[TestMethod]
public async Task AnOrdinaryCommitFailureStaysAGenericCommandExceptionAsync()
{
Expand Down
10 changes: 6 additions & 4 deletions GitIntegration/Builders/GitCommitBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ protected override string GetDiagnostic(GitProcessResult result) =>
/// </summary>
/// <remarks>
/// Overridden because the base class inspects standard error and git reports "nothing to
/// commit" on standard <em>output</em>, leaving standard error empty. Both of git's phrasings
/// are matched: the tree may be clean, or it may hold only untracked files, and neither message
/// contains the other. The match depends on the <c>LC_ALL=C</c> that
/// commit" on standard <em>output</em>, leaving standard error empty. All three of git's
/// phrasings are matched: the tree may be clean ("nothing to commit"), it may hold only untracked
/// files ("nothing added to commit"), or tracked files may be modified with nothing staged ("no
/// changes added to commit"). None of these messages contains another. The match depends on the <c>LC_ALL=C</c> that
/// <c>RunCommandGitProcessRunner</c> forces on every invocation. A failing pre-commit hook whose
/// own standard output happened to contain one of these phrases would be misclassified as
/// <see cref="GitNothingToCommitException"/>; that risk is accepted as narrow.
Expand All @@ -232,7 +233,8 @@ protected override GitCommandException CreateException(GitProcessResult result)
Ensure.NotNull(result);

if (result.StandardOutput.Contains("nothing to commit", StringComparison.Ordinal) ||
result.StandardOutput.Contains("nothing added to commit", StringComparison.Ordinal))
result.StandardOutput.Contains("nothing added to commit", StringComparison.Ordinal) ||
result.StandardOutput.Contains("no changes added to commit", StringComparison.Ordinal))
{
return new GitNothingToCommitException(
$"There is nothing staged to commit: {result.StandardOutput.Trim()}",
Expand Down
Loading