Skip to content

Commit

Permalink
git sync: dry-run flag (git-town#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph authored Oct 2, 2017
1 parent 5484ed7 commit c8595b3
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Feature: git-town sync: syncing the current feature branch with a tracking branch with dry run

As a developer wanting to test out syncing a feature branch
I want a dry run flag that doesn't run any commands
So that I can be confident in what the tool does before I use it


Background:
Given I have a feature branch named "feature"
And the following commits exist in my repository
| BRANCH | LOCATION | MESSAGE | FILE NAME |
| main | local | local main commit | local_main_file |
| | remote | remote main commit | remote_main_file |
| feature | local | local feature commit | local_feature_file |
| | remote | remote feature commit | remote_feature_file |
And I am on the "feature" branch
And I have an uncommitted file
When I run `git-town sync --dry-run`


Scenario: result
Then it runs the commands
| BRANCH | COMMAND |
| feature | git fetch --prune |
| | git add -A |
| | git stash |
| | git checkout main |
| main | git rebase origin/main |
| | git push |
| | git checkout feature |
| feature | git merge --no-edit origin/feature |
| | git merge --no-edit main |
| | git push |
| | git stash pop |
And I am still on the "feature" branch
And I still have my uncommitted file
And I have the following commits
| BRANCH | LOCATION | MESSAGE | FILE NAME |
| main | local | local main commit | local_main_file |
| feature | local | local feature commit | local_feature_file |
2 changes: 2 additions & 0 deletions src/cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ var (
abortFlag,
allFlag,
continueFlag,
dryRunFlag,
skipFlag,
undoFlag bool
)

var abortFlagDescription = "Abort a previous command that resulted in a conflict"
var continueFlagDescription = "Continue a previous command that resulted in a conflict"
var undoFlagDescription = "Undo a previous command"
var dryRunFlagDescription = "Output the commands that would be run without them"

func validateArgsCount(args []string, count int) error {
err := validateMinArgs(args, count)
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ the main branch is synced with its upstream counterpart.`,
if err != nil {
return err
}
if dryRunFlag {
script.ActivateDryRun()
}
prompt.EnsureIsConfigured()
return nil
},
Expand Down Expand Up @@ -104,6 +107,7 @@ func init() {
syncCmd.Flags().BoolVar(&allFlag, "all", false, "Sync all local branches")
syncCmd.Flags().BoolVar(&abortFlag, "abort", false, abortFlagDescription)
syncCmd.Flags().BoolVar(&continueFlag, "continue", false, continueFlagDescription)
syncCmd.Flags().BoolVar(&dryRunFlag, "dry-run", false, dryRunFlagDescription)
syncCmd.Flags().BoolVar(&skipFlag, "skip", false, "Continue a previous command by skipping the branch that resulted in a conflicted")
RootCmd.AddCommand(syncCmd)
}
25 changes: 25 additions & 0 deletions src/dryrun/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dryrun

var currentBranchName = ""
var isActive = false

// Activate enables dry-run mode
func Activate(initialBranchName string) {
isActive = true
SetCurrentBranchName(initialBranchName)
}

// IsActive returns whether of not dry-run mode is active
func IsActive() bool {
return isActive
}

// GetCurrentBranchName returns the current branch name for dry-run mode
func GetCurrentBranchName() string {
return currentBranchName
}

// SetCurrentBranchName sets the current branch name for dry-run mode
func SetCurrentBranchName(branchName string) {
currentBranchName = branchName
}
4 changes: 4 additions & 0 deletions src/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"strings"

"github.com/Originate/git-town/src/dryrun"
"github.com/Originate/git-town/src/exit"
"github.com/Originate/git-town/src/util"
)
Expand Down Expand Up @@ -47,6 +48,9 @@ func EnsureIsPerennialBranch(branchName, errorMessage string) {

// GetCurrentBranchName returns the name of the currently checked out branch
func GetCurrentBranchName() string {
if dryrun.IsActive() {
return dryrun.GetCurrentBranchName()
}
if IsRebaseInProgress() {
return getCurrentBranchNameDuringRebase()
}
Expand Down
20 changes: 20 additions & 0 deletions src/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ import (
"runtime"
"strings"

"github.com/Originate/git-town/src/dryrun"
"github.com/Originate/git-town/src/exit"
"github.com/Originate/git-town/src/git"
"github.com/Originate/git-town/src/util"

"github.com/fatih/color"
)

var dryRunMessage = `
In dry run mode. No commands will be run. When run in normal mode, the command
output will appear beneath the command. Some commands will only be run if
necessary. For example: 'git push' will run if and only if there are local
commits not on the remote.
`

// ActivateDryRun causes all commands to not be run
func ActivateDryRun() {
color.New(color.FgBlue).Print(dryRunMessage)
dryrun.Activate(git.GetCurrentBranchName())
}

// OpenBrowser opens the default browser with the given URL.
func OpenBrowser(url string) {
command := util.GetOpenBrowserCommand()
Expand Down Expand Up @@ -43,6 +57,12 @@ func PrintCommand(cmd ...string) {
// RunCommand executes the given command-line operation.
func RunCommand(cmd ...string) error {
PrintCommand(cmd...)
if dryrun.IsActive() {
if len(cmd) == 3 && cmd[0] == "git" && cmd[1] == "checkout" {
dryrun.SetCurrentBranchName(cmd[2])
}
return nil
}
// Windows commands run inside CMD
// because opening browsers is done via "start"
if runtime.GOOS == "windows" {
Expand Down
3 changes: 2 additions & 1 deletion src/steps/push_branch_step.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package steps

import (
"github.com/Originate/git-town/src/dryrun"
"github.com/Originate/git-town/src/git"
"github.com/Originate/git-town/src/script"
)
Expand All @@ -24,7 +25,7 @@ func (step *PushBranchStep) CreateUndoStepBeforeRun() Step {

// Run executes this step.
func (step *PushBranchStep) Run() error {
if !git.ShouldBranchBePushed(step.BranchName) {
if !git.ShouldBranchBePushed(step.BranchName) && !dryrun.IsActive() {
return nil
}
if step.Force {
Expand Down

0 comments on commit c8595b3

Please sign in to comment.