1package steps
2
3import (
4	"github.com/git-town/git-town/src/drivers"
5	"github.com/git-town/git-town/src/git"
6)
7
8// PreserveCheckoutHistoryStep does stuff.
9type PreserveCheckoutHistoryStep struct {
10	NoOpStep
11	InitialBranch                     string
12	InitialPreviouslyCheckedOutBranch string
13}
14
15// Run executes this step.
16func (step *PreserveCheckoutHistoryStep) Run(repo *git.ProdRepo, driver drivers.CodeHostingDriver) error {
17	expectedPreviouslyCheckedOutBranch, err := repo.Silent.ExpectedPreviouslyCheckedOutBranch(step.InitialPreviouslyCheckedOutBranch, step.InitialBranch)
18	if err != nil {
19		return err
20	}
21	// NOTE: errors are not a failure condition here --> ignoring them
22	previouslyCheckedOutBranch, _ := repo.Silent.PreviouslyCheckedOutBranch()
23	if expectedPreviouslyCheckedOutBranch == previouslyCheckedOutBranch {
24		return nil
25	}
26	currentBranch, err := repo.Silent.CurrentBranch()
27	if err != nil {
28		return err
29	}
30	err = repo.Silent.CheckoutBranch(expectedPreviouslyCheckedOutBranch)
31	if err != nil {
32		return err
33	}
34	return repo.Silent.CheckoutBranch(currentBranch)
35}
36