Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to create releases. #129

Merged
merged 22 commits into from Dec 25, 2013
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -320,9 +320,11 @@ For more details, run `gh help alias`.

### gh release (beta)

$ gh release
$ gh releases
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of merging the list and the create? Say gh release to list and gh release add to create, by following the git convention

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. add doesn't really sound right, maybe gh release create? or is it too long? maybe I'm just over thinking.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create actually sounds more accurate. Good call

> (prints a list of releases of YOUR_USER/CURRENT_REPO)

$ gh release TAG
> (creates a new release for the given tag)

### gh issues (beta)

Expand Down
1 change: 1 addition & 0 deletions commands/commands.go
Expand Up @@ -77,6 +77,7 @@ var GitHub = []*Command{
cmdCiStatus,
cmdBrowse,
cmdCompare,
cmdReleases,
cmdRelease,
cmdIssue,
}
Expand Down
3 changes: 2 additions & 1 deletion commands/help.go
Expand Up @@ -82,7 +82,8 @@ GitHub Commands:
browse Open a GitHub page in the default browser
compare Open a compare page on GitHub
ci-status Show the CI status of a commit
release Manipulate releases (beta)
releases List releases for this repo (beta)
release Create releases for this repo (beta)
issue Manipulate issues (beta)

See 'git help <command>' for more information on a specific command.
Expand Down
141 changes: 6 additions & 135 deletions commands/pull_request.go
@@ -1,15 +1,11 @@
package commands

import (
"bufio"
"fmt"
"github.com/jingweno/gh/cmd"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"io"
"io/ioutil"
"os"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -145,25 +141,8 @@ func pullRequest(cmd *Command, args *Args) {
//headProject = github.NewProject("", headProject.Name, headProject.Host)
}

var title, body string

if flagPullRequestMessage != "" {
title, body = readMsg(flagPullRequestMessage)
}

if flagPullRequestFile != "" {
var (
content []byte
err error
)
if flagPullRequestFile == "-" {
content, err = ioutil.ReadAll(os.Stdin)
} else {
content, err = ioutil.ReadFile(flagPullRequestFile)
}
utils.Check(err)
title, body = readMsg(string(content))
}
title, body, err := github.GetTitleAndBodyFromFlags(flagPullRequestMessage, flagPullRequestFile)
utils.Check(err)

fullBase := fmt.Sprintf("%s:%s", baseProject.Owner, base)
fullHead := fmt.Sprintf("%s:%s", headProject.Owner, head)
Expand All @@ -179,10 +158,8 @@ func pullRequest(cmd *Command, args *Args) {

if title == "" && flagPullRequestIssue == "" {
commits, _ := git.RefList(base, head)
t, b, err := writePullRequestTitleAndBody(base, head, fullBase, fullHead, commits)
title, body, err = writePullRequestTitleAndBody(base, head, fullBase, fullHead, commits)
utils.Check(err)
title = t
body = b
}

if title == "" && flagPullRequestIssue == "" {
Expand Down Expand Up @@ -211,34 +188,9 @@ func pullRequest(cmd *Command, args *Args) {
}

func writePullRequestTitleAndBody(base, head, fullBase, fullHead string, commits []string) (title, body string, err error) {
messageFile, err := git.PullReqMsgFile()
if err != nil {
return
}
defer os.Remove(messageFile)

err = writePullRequestChanges(base, head, fullBase, fullHead, commits, messageFile)
if err != nil {
return
}

editor, err := git.Editor()
if err != nil {
return
}

err = editTitleAndBody(editor, messageFile)
if err != nil {
err = fmt.Errorf("error using text editor for pull request message")
return
}

title, body, err = readTitleAndBody(messageFile)
if err != nil {
return
}

return
return github.GetTitleAndBodyFromEditor(func(messageFile string) error {
return writePullRequestChanges(base, head, fullBase, fullHead, commits, messageFile)
})
}

func writePullRequestChanges(base, head, fullBase, fullHead string, commits []string, messageFile string) error {
Expand Down Expand Up @@ -282,87 +234,6 @@ func writePullRequestChanges(base, head, fullBase, fullHead string, commits []st
return ioutil.WriteFile(messageFile, []byte(message), 0644)
}

func editTitleAndBody(editor, messageFile string) error {
editCmd := cmd.New(editor)
r := regexp.MustCompile("[mg]?vi[m]$")
if r.MatchString(editor) {
editCmd.WithArg("-c")
editCmd.WithArg("set ft=gitcommit tw=0 wrap lbr")
}
editCmd.WithArg(messageFile)

return editCmd.Exec()
}

func readTitleAndBody(messageFile string) (title, body string, err error) {
f, err := os.Open(messageFile)
defer f.Close()
if err != nil {
return "", "", err
}

reader := bufio.NewReader(f)

return readTitleAndBodyFrom(reader)
}

func readTitleAndBodyFrom(reader *bufio.Reader) (title, body string, err error) {
r := regexp.MustCompile("\\S")
var titleParts, bodyParts []string

line, err := readLine(reader)
for err == nil {
if strings.HasPrefix(line, "#") {
break
}

if len(bodyParts) == 0 && r.MatchString(line) {
titleParts = append(titleParts, line)
} else {
bodyParts = append(bodyParts, line)
}

line, err = readLine(reader)
}

if err == io.EOF {
err = nil
}

title = strings.Join(titleParts, " ")
title = strings.TrimSpace(title)

body = strings.Join(bodyParts, "\n")
body = strings.TrimSpace(body)

return
}

func readLine(r *bufio.Reader) (string, error) {
var (
isPrefix = true
err error
line, ln []byte
)

for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}

return string(ln), err
}

func readMsg(msg string) (title, body string) {
split := strings.SplitN(msg, "\n\n", 2)
title = strings.TrimSpace(split[0])
if len(split) > 1 {
body = strings.TrimSpace(split[1])
}

return
}

func parsePullRequestProject(context *github.Project, s string) (p *github.Project, ref string) {
p = context
ref = s
Expand Down
18 changes: 0 additions & 18 deletions commands/pull_request_test.go
@@ -1,29 +1,11 @@
package commands

import (
"bufio"
"github.com/bmizerany/assert"
"github.com/jingweno/gh/github"
"strings"
"testing"
)

func TestReadTitleAndBody(t *testing.T) {
message := `A title
A title continues

A body
A body continues
# comment
`
r := strings.NewReader(message)
reader := bufio.NewReader(r)
title, body, err := readTitleAndBodyFrom(reader)
assert.Equal(t, nil, err)
assert.Equal(t, "A title A title continues", title)
assert.Equal(t, "A body\nA body continues", body)
}

func TestParsePullRequestProject(t *testing.T) {
c := &github.Project{Host: "github.com", Owner: "jingweno", Name: "gh"}

Expand Down