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
538 changes: 229 additions & 309 deletions cmd/av/adopt.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cmd/av/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/cleanup"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/aviator-co/av/internal/utils/uiutils"
"github.com/go-git/go-git/v5/plumbing"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -317,7 +318,7 @@ func createBranch(
}

if _, exist := tx.Branch(parentBranchName); !exist {
return errParentNotAdopted
return uiutils.ErrParentNotAdopted
}
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/av/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/gh"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/aviator-co/av/internal/utils/uiutils"
"github.com/fatih/color"
"github.com/kr/text"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -139,7 +140,7 @@ func main() {
stackTrace := fmt.Sprintf("%+v", err)
fmt.Fprintf(os.Stderr, "error: %s\n%s\n", err, text.Indent(stackTrace, "\t"))
} else {
fmt.Fprint(os.Stderr, renderError(err))
fmt.Fprint(os.Stderr, uiutils.RenderError(err))
}

os.Exit(1)
Expand Down Expand Up @@ -202,7 +203,7 @@ func discoverGitHubAPIToken(ctx context.Context) string {
func getGitHubClient(ctx context.Context) (*gh.Client, error) {
token := discoverGitHubAPIToken(ctx)
if token == "" {
return nil, errNoGitHubToken
return nil, uiutils.ErrNoGitHubToken
}
var err error
once.Do(func() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (vm stackNextModel) View() string {
if len(ret) != 0 {
ret += "\n"
}
ret += renderError(vm.err)
ret += uiutils.RenderError(vm.err)
}
return ret
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/reparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (vm *reparentViewModel) View() string {
if len(ret) != 0 {
ret += "\n"
}
ret += renderError(vm.err)
ret += uiutils.RenderError(vm.err)
}
return ret
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/restack.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (vm *restackViewModel) View() string {
if len(ret) != 0 {
ret += "\n"
}
ret += renderError(vm.err)
ret += uiutils.RenderError(vm.err)
}
return ret
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (vm switchViewModel) View() string {
) + "\n"
}
if vm.err != nil {
ret += renderError(vm.err)
ret += uiutils.RenderError(vm.err)
}
return ret
}
Expand Down
69 changes: 12 additions & 57 deletions cmd/av/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/aviator-co/av/internal/utils/sliceutils"
"github.com/aviator-co/av/internal/utils/uiutils"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/go-git/go-git/v5/plumbing"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -117,65 +116,21 @@ type syncViewModel struct {
repo *git.Repo
db meta.DB
client *gh.Client
views []tea.Model

state *syncState
restackState *sequencerui.RestackState

quitWithConflict bool
err error
}

func (vm *syncViewModel) Init() tea.Cmd {
return vm.initSync()
uiutils.BaseStackedView
}

func (vm *syncViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return vm, tea.Quit
}
case error:
vm.err = msg
return vm, tea.Quit
}
if len(vm.views) > 0 {
idx := len(vm.views) - 1
var cmd tea.Cmd
vm.views[idx], cmd = vm.views[idx].Update(msg)
return vm, cmd
}
return vm, nil
return vm, vm.BaseStackedView.Update(msg)
}

func (vm *syncViewModel) View() string {
var ss []string
for _, v := range vm.views {
r := v.View()
if r != "" {
ss = append(ss, r)
}
}

var ret string
if len(ss) != 0 {
ret = lipgloss.NewStyle().MarginTop(1).MarginBottom(1).MarginLeft(2).Render(
lipgloss.JoinVertical(0, ss...),
)
}
if vm.err != nil {
if len(ret) != 0 {
ret += "\n"
}
ret += renderError(vm.err)
}
return ret
}

func (vm *syncViewModel) addView(m tea.Model) tea.Cmd {
vm.views = append(vm.views, m)
return m.Init()
func (vm *syncViewModel) Init() tea.Cmd {
return vm.initSync()
}

func (vm *syncViewModel) initSync() tea.Cmd {
Expand All @@ -201,7 +156,7 @@ func (vm *syncViewModel) initSync() tea.Cmd {
}

func (vm *syncViewModel) initTrunkCheck() tea.Cmd {
return vm.addView(&uiutils.NewlineModel{Model: uiutils.NewPromptModel(
return vm.AddView(&uiutils.NewlineModel{Model: uiutils.NewPromptModel(
"You are on the trunk, do you want to sync all stacks?",
[]string{"Yes", "No"},
func(choice string) tea.Cmd {
Expand All @@ -217,7 +172,7 @@ func (vm *syncViewModel) initTrunkCheck() tea.Cmd {
}

func (vm *syncViewModel) initPreAvHook() tea.Cmd {
return vm.addView(newPreAvSyncHookModel(vm.repo, vm.initGitFetch))
return vm.AddView(newPreAvSyncHookModel(vm.repo, vm.initGitFetch))
}

func (vm *syncViewModel) initGitFetch() tea.Cmd {
Expand Down Expand Up @@ -261,7 +216,7 @@ func (vm *syncViewModel) initGitFetch() tea.Cmd {
currentBranchRef = plumbing.NewBranchReferenceName(currentBranch)
}

return vm.addView(ghui.NewGitHubFetchModel(
return vm.AddView(ghui.NewGitHubFetchModel(
vm.repo,
vm.db,
vm.client,
Expand All @@ -285,7 +240,7 @@ func (vm *syncViewModel) initSequencerState() tea.Cmd {
func (vm *syncViewModel) continueWithState(state *savedSyncState) tea.Cmd {
vm.state = state.SyncState
vm.restackState = state.RestackState
return vm.addView(sequencerui.NewRestackModel(vm.repo, vm.db, state.RestackState, sequencerui.RestackStateOptions{
return vm.AddView(sequencerui.NewRestackModel(vm.repo, vm.db, state.RestackState, sequencerui.RestackStateOptions{
Command: "av sync",
Abort: syncFlags.Abort,
Continue: syncFlags.Continue,
Expand Down Expand Up @@ -313,7 +268,7 @@ func (vm *syncViewModel) continueWithState(state *savedSyncState) tea.Cmd {
}

func (vm *syncViewModel) initPushBranches() tea.Cmd {
return vm.addView(ghui.NewGitHubPushModel(
return vm.AddView(ghui.NewGitHubPushModel(
vm.repo,
vm.db,
vm.client,
Expand All @@ -324,7 +279,7 @@ func (vm *syncViewModel) initPushBranches() tea.Cmd {
}

func (vm *syncViewModel) initPruneBranches() tea.Cmd {
return vm.addView(gitui.NewPruneBranchModel(
return vm.AddView(gitui.NewPruneBranchModel(
vm.repo,
vm.db,
vm.state.Prune,
Expand Down Expand Up @@ -425,10 +380,10 @@ func (vm *syncViewModel) createState() (*savedSyncState, error) {
}

func (vm *syncViewModel) ExitError() error {
if errors.Is(vm.err, nothingToRestackError) {
if errors.Is(vm.Err, nothingToRestackError) {
return nil
}
if vm.err != nil {
if vm.Err != nil {
return actions.ErrExitSilently{ExitCode: 1}
}
if vm.quitWithConflict {
Expand Down
11 changes: 10 additions & 1 deletion docs/av-adopt.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ av-adopt - Adopt branches that are not managed by `av`
## SYNOPSIS

```synopsis
av adopt [--parent=<parent>]
av adopt [--parent=<parent> | --remote=<branch>]
```

## DESCRIPTION
Expand Down Expand Up @@ -37,11 +37,20 @@ branches so that they form a tree structure.
If you want to adopt the current branch and specify the parent branch, you can
do so by running the command with `--parent`.

## ADOPTING FROM A REMOTE REPOSITORY

You can also adopt branches from a remote repository by using the
`--remote` option. This option fetches the specified remote branch and adopts
it along with its parent branches.

## OPTIONS

`--parent=<parent>`
: Force specify the parent branch.

`--remote=<branch>`
: Specify a remote branch to adopt from.

## SEE ALSO

`av-orphan`(1) for orphaning a branch.
81 changes: 81 additions & 0 deletions internal/actions/adopt_branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package actions

import (
"strings"

"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/aviator-co/av/internal/utils/uiutils"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)

type AdoptingBranch struct {
Name string
Parent meta.BranchState
PullRequest *meta.PullRequest
}

func NewAdoptBranchesModel(
db meta.DB,
branches []AdoptingBranch,
onDone func() tea.Cmd,
) tea.Model {
return &AdoptBranchesModel{
db: db,
spinner: spinner.New(spinner.WithSpinner(spinner.Dot)),
branches: branches,
onDone: onDone,
}
}

type AdoptBranchesModel struct {
db meta.DB
spinner spinner.Model
branches []AdoptingBranch
done bool
onDone func() tea.Cmd
}

func (m *AdoptBranchesModel) Init() tea.Cmd {
return tea.Batch(m.spinner.Tick, m.adoptBranches)
}

func (m *AdoptBranchesModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
return m, nil
}

func (m *AdoptBranchesModel) View() string {
var ss []string
if m.done {
ss = append(ss, colors.SuccessStyle.Render("✓ Adoption complete"))
} else {
ss = append(ss, colors.ProgressStyle.Render(m.spinner.View()+"Adopting the chosen branches..."))
}
ss = append(ss, "")
for _, branch := range m.branches {
ss = append(ss, " * "+branch.Name+" → "+branch.Parent.Name)
}
return strings.Join(ss, "\n")
}

func (m *AdoptBranchesModel) adoptBranches() tea.Msg {
tx := m.db.WriteTx()
for _, branch := range m.branches {
bi, _ := tx.Branch(branch.Name)
bi.Parent = branch.Parent
bi.PullRequest = branch.PullRequest
tx.SetBranch(bi)
}
if err := tx.Commit(); err != nil {
return err
}
m.done = true
return uiutils.SimpleCommandMsg{Cmd: m.onDone()}
}
Loading