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

Read title and body by taking into account Windows line endings #806

Merged
merged 1 commit into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Read title and body by taking into account Windows line endings
`bufio.Scanner` takes care of line endings difference for different OSes. See http://golang.org/pkg/bufio/#ScanLines.

This fixes #805
  • Loading branch information
owenthereal committed Feb 13, 2015
commit e554ef562721003ba35768363eff00254eac034c
14 changes: 9 additions & 5 deletions commands/utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package commands

import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/github/hub/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit"
"github.com/github/hub/github"
"github.com/github/hub/utils"
"github.com/github/hub/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit"
)

type listFlag []string
Expand Down Expand Up @@ -94,10 +95,13 @@ func getTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string,
}

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])
s := bufio.NewScanner(strings.NewReader(msg))
if s.Scan() {
title = s.Text()
body = strings.TrimLeft(msg, title)

title = strings.TrimSpace(title)
body = strings.TrimSpace(body)
}

return
Expand Down
21 changes: 20 additions & 1 deletion commands/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package commands

import (
"github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert"
"io/ioutil"
"os"
"testing"

"github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert"
)

func TestReadMsg(t *testing.T) {
title, body := readMsg("")
assert.Equal(t, "", title)
assert.Equal(t, "", body)

title, body = readMsg("my pull title")
assert.Equal(t, "my pull title", title)
assert.Equal(t, "", body)

title, body = readMsg("my pull title\n\nmy description\n\nanother line")
assert.Equal(t, "my pull title", title)
assert.Equal(t, "my description\n\nanother line", body)

title, body = readMsg("my pull title\r\n\r\nmy description\r\n\r\nanother line")
assert.Equal(t, "my pull title", title)
assert.Equal(t, "my description\r\n\r\nanother line", body)
}

func TestDirIsNotEmpty(t *testing.T) {
dir := createTempDir(t)
defer os.RemoveAll(dir)
Expand Down