Skip to content

Commit

Permalink
Show local status by default in stack status command (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
geofflamrock authored Dec 30, 2024
1 parent 0906f7e commit e53ba99
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 170 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ OPTIONS:

### `stack status`

Shows the status of a stack, including commits compared to other branches and the status of any associated pull requests.
Shows the status of a stack, including commits compared to other branches and optionally the status of any associated pull requests.

```shell
USAGE:
Expand All @@ -154,6 +154,7 @@ OPTIONS:
--working-dir The path to the directory containing the git repository. Defaults to the current directory
-n, --name The name of the stack to show the status of
--all Show status of all stacks
--full Show full status including pull requests
```

### `stack delete`
Expand Down
199 changes: 119 additions & 80 deletions src/Stack.Tests/Commands/Stack/StackStatusCommandHandlerTests.cs

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions src/Stack.Tests/Git/GitBranchStatusParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using FluentAssertions;
using Stack.Git;

namespace Stack.Tests.Git;

public class GitBranchStatusParserTests
{
[Fact]
public void WhenBranchIsCurrentBranch_ReturnsCorrectStatus()
{
// Arrange
var branchStatus = "* main 1234567 [origin/main] Some message";

// Act
var result = GitBranchStatusParser.Parse(branchStatus);

// Assert
result.Should().Be(new GitBranchStatus("main", "origin/main", true, true, 0, 0, new Commit("1234567", "Some message")));
}

[Fact]
public void WhenBranchIsAheadAndBehindItsRemoteTrackingBranch_ReturnsCorrectStatus()
{
// Arrange
var branchStatus = " main 1234567 [origin/main: ahead 1, behind 2] Some message";

// Act
var result = GitBranchStatusParser.Parse(branchStatus);

// Assert
result.Should().Be(new GitBranchStatus("main", "origin/main", true, false, 1, 2, new Commit("1234567", "Some message")));
}

[Fact]
public void WhenBranchIsAheadOfItsRemoteTrackingBranch_ReturnsCorrectStatus()
{
// Arrange
var branchStatus = " main 1234567 [origin/main: ahead 1] Some message";

// Act
var result = GitBranchStatusParser.Parse(branchStatus);

// Assert
result.Should().Be(new GitBranchStatus("main", "origin/main", true, false, 1, 0, new Commit("1234567", "Some message")));
}

[Fact]
public void WhenBranchIsBehindItsRemoteTrackingBranch_ReturnsCorrectStatus()
{
// Arrange
var branchStatus = " main 1234567 [origin/main: behind 2] Some message";

// Act
var result = GitBranchStatusParser.Parse(branchStatus);

// Assert
result.Should().Be(new GitBranchStatus("main", "origin/main", true, false, 0, 2, new Commit("1234567", "Some message")));
}

[Fact]
public void WhenBranchIsNotTracked_ReturnsCorrectStatus()
{
// Arrange
var branchStatus = " main 1234567 Some message";

// Act
var result = GitBranchStatusParser.Parse(branchStatus);

// Assert
result.Should().Be(new GitBranchStatus("main", null, false, false, 0, 0, new Commit("1234567", "Some message")));
}

[Fact]
public void WhenRemoteTrackingBranchIsGone_ReturnsCorrectStatus()
{
// Arrange
var branchStatus = " main 1234567 [origin/main: gone] Some message";

// Act
var result = GitBranchStatusParser.Parse(branchStatus);

// Assert
result.Should().Be(new GitBranchStatus("main", "origin/main", false, false, 0, 0, new Commit("1234567", "Some message")));
}
}
8 changes: 4 additions & 4 deletions src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Build(Repository repository, string defaultBranchName)
}
}

private static Commit CreateEmptyCommit(Repository repository, Branch branch, string message)
private static LibGit2Sharp.Commit CreateEmptyCommit(Repository repository, Branch branch, string message)
{
repository.Refs.UpdateTarget("HEAD", branch.CanonicalName);
var signature = new Signature(Some.Name(), Some.Name(), DateTimeOffset.Now);
Expand Down Expand Up @@ -238,7 +238,7 @@ public TestGitRepository Build()
return new TestGitRepository(localDirectory, remoteDirectory, localRepo);
}

private static Commit CreateInitialCommit(Repository repository)
private static LibGit2Sharp.Commit CreateInitialCommit(Repository repository)
{
var message = $"Initial commit";
var signature = new Signature(Some.Name(), Some.Name(), DateTimeOffset.Now);
Expand All @@ -252,12 +252,12 @@ public class TestGitRepository(TemporaryDirectory LocalDirectory, TemporaryDirec
public string RemoteUri => RemoteDirectory.DirectoryPath;
public GitOperationSettings GitOperationSettings => new GitOperationSettings(false, false, LocalDirectory.DirectoryPath);

public Commit GetTipOfBranch(string branchName)
public LibGit2Sharp.Commit GetTipOfBranch(string branchName)
{
return LocalRepository.Branches[branchName].Tip;
}

public List<Commit> GetCommitsReachableFromBranch(string branchName)
public List<LibGit2Sharp.Commit> GetCommitsReachableFromBranch(string branchName)
{
return [.. LocalRepository.Branches[branchName].Commits];
}
Expand Down
Loading

0 comments on commit e53ba99

Please sign in to comment.