-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from isacikgoz/development
Development
- Loading branch information
Showing
17 changed files
with
1,080 additions
and
913 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/fatih/color" | ||
"github.com/isacikgoz/gitin/prompt" | ||
"github.com/isacikgoz/gitin/term" | ||
git "github.com/isacikgoz/libgit2-api" | ||
"github.com/justincampbell/timeago" | ||
) | ||
|
||
// branch holds a list of items used to fill the terminal screen. | ||
type branch struct { | ||
repository *git.Repository | ||
prompt *prompt.Prompt | ||
} | ||
|
||
// BranchPrompt configures a prompt to serve as a branch prompt | ||
func BranchPrompt(r *git.Repository, opts *prompt.Options) (*prompt.Prompt, error) { | ||
branches, err := r.Branches() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not load branches: %v", err) | ||
} | ||
list, err := prompt.NewList(branches, opts.LineSize) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create list: %v", err) | ||
} | ||
|
||
b := &branch{repository: r} | ||
b.prompt = prompt.Create("Branches", opts, list, | ||
prompt.WithSelectionHandler(b.onSelect), | ||
prompt.WithItemRenderer(renderItem), | ||
prompt.WithInformation(b.branchInfo), | ||
) | ||
b.defineKeyBindings() | ||
|
||
return b.prompt, nil | ||
} | ||
|
||
func (b *branch) onSelect(item interface{}) error { | ||
branch := item.(*git.Branch) | ||
args := []string{"checkout", branch.Name} | ||
cmd := exec.Command("git", args...) | ||
cmd.Dir = b.repository.Path() | ||
if err := cmd.Run(); err != nil { | ||
return nil // possibly dirty branch | ||
} | ||
b.prompt.Stop() // quit after selection | ||
return nil | ||
} | ||
|
||
func (b *branch) defineKeyBindings() error { | ||
keybindings := []*prompt.KeyBinding{ | ||
&prompt.KeyBinding{ | ||
Key: 'd', | ||
Display: "d", | ||
Desc: "delete branch", | ||
Handler: b.deleteBranch, | ||
}, | ||
&prompt.KeyBinding{ | ||
Key: 'D', | ||
Display: "D", | ||
Desc: "force delete branch", | ||
Handler: b.forceDeleteBranch, | ||
}, | ||
&prompt.KeyBinding{ | ||
Key: 'q', | ||
Display: "q", | ||
Desc: "quit", | ||
Handler: b.quit, | ||
}, | ||
} | ||
for _, kb := range keybindings { | ||
if err := b.prompt.AddKeyBinding(kb); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (b *branch) branchInfo(item interface{}) [][]term.Cell { | ||
branch := item.(*git.Branch) | ||
target := branch.Target() | ||
grid := make([][]term.Cell, 0) | ||
if target != nil { | ||
cells := term.Cprint("Last commit was ", color.Faint) | ||
cells = append(cells, term.Cprint(timeago.FromTime(target.Author.When), color.FgBlue)...) | ||
grid = append(grid, cells) | ||
if branch.IsRemote() { | ||
return grid | ||
} | ||
grid = append(grid, branchInfo(branch, false)...) | ||
} | ||
return grid | ||
} | ||
|
||
func (b *branch) deleteBranch(item interface{}) error { | ||
return b.bareDelete(item, "d") | ||
} | ||
|
||
func (b *branch) forceDeleteBranch(item interface{}) error { | ||
return b.bareDelete(item, "D") | ||
} | ||
|
||
func (b *branch) bareDelete(item interface{}, mode string) error { | ||
branch := item.(*git.Branch) | ||
cmd := exec.Command("git", "branch", "-"+mode, branch.Name) | ||
cmd.Dir = b.repository.Path() | ||
if err := cmd.Run(); err != nil { | ||
return nil // possibly an unmerged branch, just ignore it | ||
} | ||
return b.reloadBranches() | ||
} | ||
|
||
func (b *branch) quit(item interface{}) error { | ||
b.prompt.Stop() | ||
return nil | ||
} | ||
|
||
// reloads the list | ||
func (b *branch) reloadBranches() error { | ||
branches, err := b.repository.Branches() | ||
if err != nil { | ||
return err | ||
} | ||
state := b.prompt.State() | ||
list, err := prompt.NewList(branches, state.ListSize) | ||
if err != nil { | ||
return fmt.Errorf("could not reload branches: %v", err) | ||
} | ||
state.List = list | ||
b.prompt.SetState(state) | ||
// return err | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
|
||
git "github.com/isacikgoz/libgit2-api" | ||
) | ||
|
||
func popGitCommand(r *git.Repository, args []string) error { | ||
os.Setenv("LESS", "-RCS") | ||
cmd := exec.Command("git", args...) | ||
cmd.Dir = r.Path() | ||
|
||
cmd.Stdout = os.Stdout | ||
cmd.Stdin = os.Stdin | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
if err := cmd.Wait(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.