Skip to content

Commit

Permalink
feat(branches): create branch
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Aug 31, 2024
1 parent f8c232d commit 402219f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
7 changes: 4 additions & 3 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func GetRepo(dir string) (*Repo, error) {
return nil, err
}

headRev, err := repo.RevParse("HEAD")
headRef, err := repo.RevParse("HEAD", gitm.RevParseOptions{
CommandOptions: gitm.CommandOptions{Args: []string{"--abbrev-ref"}},
})
if err != nil {
return nil, err
}
Expand All @@ -75,11 +77,10 @@ func GetRepo(dir string) (*Repo, error) {
for i, b := range bNames {
var updatedAt *time.Time
var lastCommitMsg *string
isHead := false
isHead := b == headRef
commits, err := gitm.Log(dir, b, gitm.LogOptions{MaxCount: 1})
if err == nil && len(commits) > 0 {
updatedAt = &commits[0].Committer.When
isHead = commits[0].ID.Equal(headRev)
lastCommitMsg = utils.StringPtr(commits[0].Summary())
}
commitsAhead, err := repo.RevListCount([]string{fmt.Sprintf("origin/%s..%s", b, b)})
Expand Down
36 changes: 34 additions & 2 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ func (m *Model) checkout() (tea.Cmd, error) {
SectionId: 0,
SectionType: SectionType,
TaskId: taskId,
Msg: repoMsg{repo: repo},
Msg: repoMsg{repo: repo, resetSelection: true},
Err: err,
}
}), nil
}

type repoMsg struct {
repo *git.Repo
repo *git.Repo
resetSelection bool
}

func (m *Model) readRepoCmd() []tea.Cmd {
Expand Down Expand Up @@ -316,3 +317,34 @@ func (m *Model) deleteBranch() tea.Cmd {
}
})
}

func (m *Model) newBranch(name string) tea.Cmd {
taskId := fmt.Sprintf("create_branch_%s_%d", name, time.Now().Unix())
task := context.Task{
Id: taskId,
StartText: fmt.Sprintf("Creating branch %s", name),
FinishedText: fmt.Sprintf("Branch %s has been created", name),
State: context.TaskStart,
Error: nil,
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
// TODO: find out what the default branch is / use the currently selected branch
err := gitm.Checkout(*m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: "main"})
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}

return constants.TaskFinishedMsg{
SectionId: 0,
SectionType: SectionType,
TaskId: taskId,
Msg: repoMsg{repo: repo},
Err: err,
}
})
}
1 change: 0 additions & 1 deletion ui/components/reposection/delete.go

This file was deleted.

35 changes: 21 additions & 14 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,24 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
case msg.Type == tea.KeyEnter:
input := m.PromptConfirmationBox.Value()
action := m.GetPromptConfirmationAction()
pr := findPRForRef(m.Prs, m.getCurrBranch().Data.Name)
sid := tasks.SectionIdentifer{Id: m.Id, Type: SectionType}
if input == "Y" || input == "y" {
switch action {
case "delete":
cmd = m.deleteBranch()
case "close":
cmd = tasks.ClosePR(m.Ctx, sid, pr)
case "reopen":
cmd = tasks.ReopenPR(m.Ctx, sid, pr)
case "ready":
cmd = tasks.PRReady(m.Ctx, sid, pr)
case "merge":
cmd = tasks.MergePR(m.Ctx, sid, pr)
if action == "new" {
cmd = m.newBranch(input)
} else {
pr := findPRForRef(m.Prs, m.getCurrBranch().Data.Name)
sid := tasks.SectionIdentifer{Id: m.Id, Type: SectionType}
if input == "Y" || input == "y" {
switch action {
case "delete":
cmd = m.deleteBranch()
case "close":
cmd = tasks.ClosePR(m.Ctx, sid, pr)
case "reopen":
cmd = tasks.ReopenPR(m.Ctx, sid, pr)
case "ready":
cmd = tasks.PRReady(m.Ctx, sid, pr)
case "merge":
cmd = tasks.MergePR(m.Ctx, sid, pr)
}
}
}

Expand Down Expand Up @@ -152,6 +156,9 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
m.repo = msg.repo
m.Table.SetIsLoading(false)
m.Table.SetRows(m.BuildRows())
if msg.resetSelection {
m.Table.ResetCurrItem()
}

case SectionPullRequestsFetchedMsg:
m.Prs = msg.Prs
Expand Down
2 changes: 2 additions & 0 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ func (m *BaseModel) GetPromptConfirmation() string {
prompt = "Are you sure you want to reopen this issue? (Y/n) "
case m.PromptConfirmationAction == "delete" && m.Ctx.View == config.RepoView:
prompt = "Are you sure you want to delete this branch? (Y/n) "
case m.PromptConfirmationAction == "new" && m.Ctx.View == config.RepoView:
prompt = "Enter branch name: "
}

m.PromptConfirmationBox.SetPrompt(prompt)
Expand Down
14 changes: 14 additions & 0 deletions ui/keys/branchKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type BranchKeyMap struct {
Checkout key.Binding
New key.Binding
FastForward key.Binding
Push key.Binding
Delete key.Binding
Expand All @@ -22,6 +23,10 @@ var BranchKeys = BranchKeyMap{
key.WithKeys("C", " "),
key.WithHelp("C/space", "checkout"),
),
New: key.NewBinding(
key.WithKeys("n"),
key.WithHelp("n", "new"),
),
FastForward: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "fast-forward"),
Expand All @@ -45,6 +50,7 @@ func BranchFullHelp() []key.Binding {
BranchKeys.Checkout,
BranchKeys.FastForward,
BranchKeys.Push,
BranchKeys.New,
BranchKeys.Delete,
BranchKeys.ViewPRs,
}
Expand All @@ -61,6 +67,14 @@ func rebindBranchKeys(keys []config.Keybinding) error {
var key *key.Binding

switch branchKey.Builtin {
case "new":
key = &BranchKeys.New
case "delete":
key = &BranchKeys.Delete
case "push":
key = &BranchKeys.Push
case "fastForward":
key = &BranchKeys.FastForward
case "checkout":
key = &BranchKeys.Checkout
case "viewPRs":
Expand Down
7 changes: 7 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, cmd

case key.Matches(msg, keys.BranchKeys.New):
if currSection != nil {
currSection.SetPromptConfirmationAction("new")
cmd = currSection.SetIsPromptConfirmationShown(true)
}
return m, cmd

case key.Matches(msg, keys.BranchKeys.ViewPRs):
m.ctx.View = m.switchSelectedView()
m.syncMainContentWidth()
Expand Down

0 comments on commit 402219f

Please sign in to comment.