Skip to content

Commit

Permalink
Merge pull request #54 from isacikgoz/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
isacikgoz authored Jun 21, 2019
2 parents 22ae27f + 1a98e23 commit ca4a5fa
Show file tree
Hide file tree
Showing 17 changed files with 1,080 additions and 913 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gitin is a minimalist tool that lets you explore a git repository from the comma
</p>

## Features

- Fuzzy search (type `/` to start a search after running `gitin <command>`)
- Interactive stage and see the diff of files (`gitin status` then press `enter` to see diff or `space` to stage)
- Commit/amend changes (`gitin status` then press `c` to commit or `m` to amend)
Expand All @@ -20,21 +21,26 @@ gitin is a minimalist tool that lets you explore a git repository from the comma
- See more options by running `gitin --help`, also you can get help for individual subcommands (e.g. `gitin log --help`)

## Installation
- Linux and macOS are supported, haven't tried on Windows.

- Linux and macOS are supported, Windows is not at the moment.
- Download latest release from [here](https://github.com/isacikgoz/gitin/releases)
- **Or**, manually download it with `go get -d github.com/isacikgoz/gitin`
- `cd` into `$GOPATH/src/github.com/isacikgoz/gitin`
- make would expect a built libgit2 library to make a static link. So, when you run `make` command, you should be able to build libgit2 at your `$GOPATH/pkg/mod/gopkg.in/libgit2/git2go.../vendor/libgit2/build` directory. This issue has been shown up after go modules.
- build with `make install` (`cmake` and `pkg-config` are required)

### Mac/Linux using brew

The tap is recently moved to new repo, so if you added the older one (isacikgoz/gitin), consider removing it and adding the new one.
```

```sh
brew tap isacikgoz/taps
brew install gitin
```

## Usage
```bash

```sh
usage: gitin [<flags>] <command> [<args> ...]

Flags:
Expand All @@ -45,23 +51,35 @@ Commands:
help [<command>...]
Show help.

branch [<flags>]
Checkout, list, or delete branches.

log [<flags>]
log
Show commit logs.

status
Show working-tree status. Also, stage and commit changes.
Show working-tree status. Also stage and commit changes.

branch
Show list of branches.

Environment Variables:

GITIN_LINESIZE=<int>
GITIN_STARTINSEARCH=<bool
GITIN_DISABLECOLOR=<bool>
GITIN_VIMKEYS=<bool>

Press ? for controls while application is running.

```
## Configure
- To set the line size `export GITIN_LINESIZE=5`
- To set always start in search mode `GITIN_STARTSEARCH=true`
- To disable colors `GITIN_DISABLECOLOR=true`
- To disable h,j,k,l for nav `GITIN_VIMKEYS=false`
## Development Requirements
- Requires gitlib2 v27 and `git2go`. See the project homepages for more information about build instructions. For gitin you can simply;
- macOS:
1. install libgit2 via `brew install libgit2` (consider that libgit2.v27 is required)
Expand All @@ -76,12 +94,15 @@ Commands:
- `cd` into `$GOPATH/src/github.com/isacikgoz/gitin` and start hacking
## Contribution
- Contributions are welcome. If you like to please refer to [Contribution Guidelines](/CONTRIBUTING.md)
- Bug reports should include descriptive steps to reproduce so that maintainers can easily understand the actual problem
- Feature requests are welcome, ask for anything that seems appropriate
## Credits
See the [credits page](https://github.com/isacikgoz/gitin/wiki/Credits)
## License
[BSD-3-Clause](/LICENSE)
137 changes: 137 additions & 0 deletions cli/branch.go
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
}
25 changes: 25 additions & 0 deletions cli/commands.go
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
}
Loading

0 comments on commit ca4a5fa

Please sign in to comment.