Skip to content

Commit

Permalink
Add diff parent command (git-town#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
cirego authored May 15, 2020
1 parent 14f9cd8 commit 62f22fb
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Feature: git town-parent-diff: diffing the current feature branch

As a user running parent-diff
With no arguments
On a feature branch that has no parent branch defined
I should see a prompt to supply a parent branch
So that the command can work as I expect


Scenario: result
Given my repository has a feature branch named "feature" with no parent
And I am on the "feature" branch
When I run "git-town diff-parent" and answer the prompts:
| PROMPT | ANSWER |
| Please specify the parent branch of 'feature' | [ENTER] |
Then it runs the commands
| BRANCH | COMMAND |
| feature | git diff main..feature |
And I am still on the "feature" branch
And Git Town is now aware of this branch hierarchy
| BRANCH | PARENT |
| feature | main |
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: git town-parent-diff: diffing the current feature branch

As a developer makeing changes to a feature branched created with Git Town
I want that Git Town tells me which changes this feature branch contains
So that I know what work is left to do
And which work will be merged into the parent branch


Scenario: result
Given my repository has a feature branch named "feature-1"
And my repository has a feature branch named "feature-2" as a child of "feature-1"
And I am on the "feature-2" branch
When I run "git-town diff-parent"
Then it runs the commands
| BRANCH | COMMAND |
| feature-2 | git diff feature-1..feature-2 |
And I am still on the "feature-2" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: git town-diff-parent: errors when trying to diff the main branch

As a developer accidentally trying to diff the main branch
I should see an error that I cannot diff the main branch
Because the master branch cannot have a parent branch


Scenario: result
Given my repository has a feature branch named "feature"
And I am on the "main" branch
When I run "git-town diff-parent"
Then it runs no commands
And it prints the error:
"""
You can only diff-parent feature branches
"""
And I am still on the "main" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: git town-diff-parent: errors when trying to diff a perennial branch

As a developer accidentally trying to diff a perennial branch
I should see an error that I cannot diff perennial branches
Because perennial branches cannot have parent branches


Scenario: result
Given my repository has the perennial branch "qa"
And I am on the "qa" branch
When I run "git-town diff-parent"
Then it runs no commands
And it prints the error:
"""
You can only diff-parent feature branches
"""
And I am still on the "qa" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: git town-diff-parent: errors if supplied branch does not exist

As a developer mistyping the branch name to diff against its parent
I should get an error that the given branch does not exist
So that I can diff the correct branch

Scenario: result
Given I am on the "main" branch
When I run "git-town diff-parent non-existing-feature"
Then it runs no commands
And it prints the error:
"""
There is no local branch named 'non-existing-feature'
"""
And I end up on the "main" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Feature: git town-diff-parent: diffing a given feature branch

(see ../../current_branch/on_feature_branch/with_parent_branches.feature)


Scenario: result
Given my repository has a feature branch named "feature-1"
And my repository has a feature branch named "feature-2" as a child of "feature-1"
And I am on the "main" branch
When I run "git-town diff-parent feature-2"
Then it runs the commands
| BRANCH | COMMAND |
| main | git diff feature-1..feature-2 |
And I am still on the "main" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Feature: git town-parent-diff: diffing the current feature branch

As a user running parent-diff
With a supplied branch
On the main branch
I should see a prompt to identify a parent branch
So that the command can work as I expect


Scenario: result
Given my repository has a feature branch named "feature" with no parent
And I am on the "main" branch
When I run "git-town diff-parent feature" and answer the prompts:
| PROMPT | ANSWER |
| Please specify the parent branch of 'feature' | [ENTER] |
Then it runs the commands
| BRANCH | COMMAND |
| main | git diff main..feature |
And I am still on the "main" branch
And Git Town is now aware of this branch hierarchy
| BRANCH | PARENT |
| feature | main |
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Feature: git town-diff-parent: diffing a given feature branch

(see ../../current_branch/on_feature_branch/with_parent_branches.feature)


Scenario: result
Given my repository has a feature branch named "feature-1"
And my repository has a feature branch named "feature-2" as a child of "feature-1"
And I am on the "feature-1" branch
When I run "git-town diff-parent feature-2"
Then it runs the commands
| BRANCH | COMMAND |
| feature-1 | git diff feature-1..feature-2 |
And I am still on the "feature-1" branch
15 changes: 15 additions & 0 deletions features/git-town-diff-parent/supplied_branch/main_branch.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: git town-diff-parent: errors when trying to diff the main branch

(see ../current_branch/on_main_branch.feature)


Scenario: result
Given my repository has a feature branch named "feature"
And I am on the "feature" branch
When I run "git-town diff-parent main"
Then it runs no commands
And it prints the error:
"""
You can only diff-parent feature branches
"""
And I am still on the "feature" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Feature: git town-diff-parent: diffing a given feature branch

(see ../../current_branch/on_feature_branch/with_parent_branch.feature)


Scenario: result
Given my repository has a feature branch named "feature-1"
And my repository has a feature branch named "feature-2" as a child of "feature-1"
And I am on the "feature-2" branch
When I run "git-town diff-parent feature-2"
Then it runs the commands
| BRANCH | COMMAND |
| feature-2 | git diff feature-1..feature-2 |
And I am still on the "feature-2" branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Feature: git town-parent-diff: diffing the current feature branch

As a user running parent-diff
With a supplied branch that matches my current branch
On a branch that has no parent branch defined
I should see a prompt to supply a parent branch
So that the command can work as I expect


Scenario: result
Given my repository has a feature branch named "feature" with no parent
And I am on the "feature" branch
When I run "git-town diff-parent feature" and answer the prompts:
| PROMPT | ANSWER |
| Please specify the parent branch of 'feature' | [ENTER] |
Then it runs the commands
| BRANCH | COMMAND |
| feature | git diff main..feature |
And I am still on the "feature" branch
And Git Town is now aware of this branch hierarchy
| BRANCH | PARENT |
| feature | main |
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: git town-diff-parent: errors when trying to diff a perennial branch

(see ../current_branch/on_perennial_branch.feature)


Scenario: result
Given my repository has a feature branch named "feature"
And my repository has the perennial branch "qa"
And I am on the "feature" branch
When I run "git-town diff-parent qa"
Then it runs no commands
And it prints the error:
"""
You can only diff-parent feature branches
"""
And I am still on the "feature" branch
60 changes: 60 additions & 0 deletions src/cmd/diff_parent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"github.com/git-town/git-town/src/git"
"github.com/git-town/git-town/src/prompt"
"github.com/git-town/git-town/src/script"
"github.com/git-town/git-town/src/util"
"github.com/spf13/cobra"
)

type diffParentConfig struct {
branch string
parentBranch string
}

var diffParentCommand = &cobra.Command{
Use: "diff-parent [<branch>]",
Short: "Shows the changes committed to a feature branch",
Long: `Shows the changes committed to a feature branch
Works on either the current branch or the branch name provided.
Exits with error code 1 if the given branch is a perennial branch or the main branch.`,
Run: func(cmd *cobra.Command, args []string) {
config := getDiffParentConfig(args)
script.RunCommandSafe("git", "diff", config.parentBranch+".."+config.branch)
},
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return util.FirstError(
git.ValidateIsRepository,
validateIsConfigured,
)
},
}

// Does not return error because "Ensure" functions will call exit directly
func getDiffParentConfig(args []string) (config diffParentConfig) {
initialBranch := git.GetCurrentBranchName()

if len(args) == 0 {
config.branch = initialBranch
} else {
config.branch = args[0]
}

if initialBranch != config.branch {
git.EnsureHasLocalBranch(config.branch)
}

git.Config().EnsureIsFeatureBranch(config.branch, "You can only diff-parent feature branches.")

prompt.EnsureKnowsParentBranches([]string{config.branch})
config.parentBranch = git.Config().GetParentBranch(config.branch)
return
}

func init() {
RootCmd.AddCommand(diffParentCommand)
}
5 changes: 5 additions & 0 deletions src/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func EnsureHasBranch(branchName string) {
util.Ensure(HasBranch(branchName), fmt.Sprintf("There is no branch named '%s'", branchName))
}

// EnsureHasLocalBranch enforces that a local branch with the given name exists
func EnsureHasLocalBranch(branchName string) {
util.Ensure(HasLocalBranch(branchName), fmt.Sprintf("There is no local branch named '%s'", branchName))
}

// EnsureIsNotMainBranch enforces that a branch with the given name is not the main branch
func EnsureIsNotMainBranch(branchName, errorMessage string) {
util.Ensure(!Config().IsMainBranch(branchName), errorMessage)
Expand Down
11 changes: 11 additions & 0 deletions test/git_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ func (repo *GitRepository) CreateFeatureBranch(name string) error {
return nil
}

// CreateFeatureBranch creates a feature branch with no defined parent in this repository.
func (repo *GitRepository) CreateFeatureBranchNoParent(name string) error {
err := repo.Shell.RunMany([][]string{
{"git", "checkout", "-b", name},
})
if err != nil {
return fmt.Errorf("cannot create feature branch %q: %w", name, err)
}
return nil
}

// CreateFile creates a file with the given name and content in this repository.
func (repo *GitRepository) CreateFile(name, content string) error {
filePath := filepath.Join(repo.Dir, name)
Expand Down
9 changes: 9 additions & 0 deletions test/git_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ func TestGitRepository_CreateFeatureBranch(t *testing.T) {
err := repo.CreateFeatureBranch("f1")
assert.Nil(t, err)
assert.True(t, repo.Configuration(true).IsFeatureBranch("f1"))
assert.Equal(t, []string{"main"}, repo.Configuration(true).GetAncestorBranches("f1"))
}

func TestGitRepository_CreateFeatureBranchNoParent(t *testing.T) {
repo := createTestGitTownRepo(t)
err := repo.CreateFeatureBranchNoParent("f1")
assert.Nil(t, err)
assert.True(t, repo.Configuration(true).IsFeatureBranch("f1"))
assert.Equal(t, []string(nil), repo.Configuration(true).GetAncestorBranches("f1"))
}

func TestGitRepository_CreateFile(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions test/steps/branch_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func BranchSteps(suite *godog.Suite, fs *FeatureState) {
return nil
})

suite.Step(`^my repository has a feature branch named "([^"]*)" with no parent$`, func(branch string) error {
return fs.activeScenarioState.gitEnvironment.DeveloperRepo.CreateFeatureBranchNoParent(branch)
})

suite.Step(`^my repository has a feature branch named "([^"]+)" as a child of "([^"]+)"$`, func(childBranch, parentBranch string) error {
err := fs.activeScenarioState.gitEnvironment.DeveloperRepo.CreateChildFeatureBranch(childBranch, parentBranch)
if err != nil {
Expand Down

0 comments on commit 62f22fb

Please sign in to comment.