Skip to content

Commit

Permalink
Implements
Browse files Browse the repository at this point in the history
  • Loading branch information
yasukotelin committed Sep 15, 2019
1 parent 1038c94 commit 1c4b465
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 21 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
# git-rm-merged

**ALERT** Not implemented yet 🤷‍♂️
git-rm-merged is git subcommand that removes the merged local branches.

The git sub command that removes the merged local branches.
## Description

## Usage
You can use the `git-rm-merged` as a git subcommand.
This removes the merged local branches.

**This confirms if you want to delete the target branches.**

`rm-merged` removes the merged local branches.
## Install

```
git rm-merged
go get -u github.com/yasukotelin/git-rm-merged
```

## Usage

```
$ git rm-merged
feature/#1 [y/n]:y
feature/#2 [y/n]:n
feature/#3 [y/n]:y
```

## Licence

MIT

## Author

yasukotelin
36 changes: 36 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"os/exec"
"strings"
)

func getMergedBranches() ([]string, error) {
var branches []string
out, err := exec.Command("git", "branch", "--merged").Output()
if err != nil {
return nil, err
}

for _, branch := range strings.Split(string(out), "\n") {
branch = strings.TrimSpace(branch)
if (branch == "") {
continue
}
// * develop のようなカレントブランチは対象外
if strings.HasPrefix(branch, "*") {
continue
}
// master/developは対象外
if strings.Contains(branch, "master") || strings.Contains(branch, "develop") {
continue
}

branches = append(branches, branch)
}
return branches, nil
}

func delBranch(name string) error {
return exec.Command("git", "branch", "-d", name).Run()
}
55 changes: 39 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
package main

import (
"fmt"
"log"
"os"
"fmt"
"log"
"os"

"github.com/urfave/cli"
"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()
app.Name = "git-rm-merged"
app.Description = "The git sub command that removes the merged local branches."
app.Action = func(c *cli.Context) error {
fmt.Println("git-rm-merged!")
return nil
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
app := cli.NewApp()
app.Name = "git-rm-merged"
app.Version = "1.0.0"
app.Description = "The git sub command that removes the merged local branches."
app.Action = mainAction

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

func mainAction(c *cli.Context) error {
branches, err := getMergedBranches()
if err != nil {
return err
}

for _, b := range branches {
fmt.Printf("%s [y/n]:", b)

var key string

fmt.Scanln(&key)
if (key != "y") {
continue
}

err := delBranch(b)
if err != nil {
return err
}
}

return nil
}

0 comments on commit 1c4b465

Please sign in to comment.