Skip to content

Commit

Permalink
Merge pull request #19 from shurcooL/add-code.google.com-presenter
Browse files Browse the repository at this point in the history
Add presenter for code.google.com/p/... Go packages.
  • Loading branch information
dmitshur committed Sep 25, 2014
2 parents 63e3bf7 + 4a85370 commit 38743ee
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
94 changes: 94 additions & 0 deletions presenter/codegoogle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package presenter

import (
"html/template"
"net/url"
"strings"

"github.com/shurcooL/go/gists/gist7480523"
"github.com/sourcegraph/go-vcs/vcs"
"github.com/sourcegraph/vcsstore/vcsclient"
)

type codeGooglePresenter struct {
repo *gist7480523.GoPackageRepo

comparison codeGoogleComparison
}

func newCodeGooglePresenter(repo *gist7480523.GoPackageRepo) Presenter {
return &codeGooglePresenter{
repo: repo,
comparison: newCodeGoogleComparison(repo),
}
}

func (this codeGooglePresenter) Repo() *gist7480523.GoPackageRepo {
return this.repo
}
func (this codeGooglePresenter) HomePage() *template.URL {
url := template.URL("https://" + this.repo.RepoImportPath())
return &url
}
func (_ codeGooglePresenter) Image() template.URL {
return "https://github.com/images/gravatars/gravatar-user-420.png"
}
func (this codeGooglePresenter) Changes() <-chan Change {
if this.comparison.err != nil {
return nil
}
out := make(chan Change)
go func() {
foundLocalRev := false
for _, commit := range this.comparison.commits {
// Break out when/if we reach the current local revision.
if commit.ID == vcs.CommitID(this.repo.GoPackages()[0].Dir.Repo.VcsLocal.LocalRev) {
foundLocalRev = true
break
}
out <- changeMessage(firstParagraph(commit.Message))
}
if !foundLocalRev {
out <- changeMessage("... (there may be more changes, not shown)")
}
close(out)
}()
return out
}

// firstParagraph returns the first paragraph of a string.
func firstParagraph(s string) string {
if index := strings.Index(s, "\n\n"); index != -1 {
return s[:index]
}
return s
}

// ---

var sg = vcsclient.New(&url.URL{Scheme: "http", Host: "vcsstore.sourcegraph.com"}, nil)

type codeGoogleComparison struct {
commits []*vcs.Commit
err error
}

func newCodeGoogleComparison(repo *gist7480523.GoPackageRepo) (c codeGoogleComparison) {
cloneUrl, err := url.Parse("https://" + repo.RepoImportPath())
if err != nil {
c.err = err
return
}

r, err := sg.Repository("hg", cloneUrl) // code.google.com/p/... repos are known to use Mercurial.
if err != nil {
c.err = err
return
}

c.commits, _, c.err = r.Commits(vcs.CommitsOptions{
Head: vcs.CommitID(repo.GoPackages()[0].Dir.Repo.VcsRemote.RemoteRev),
N: 20, // Cap for now. TODO: Support arbtirary second revision to go until.
})
return
}
3 changes: 1 addition & 2 deletions presenter/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func init() {
addProvider(func(repo *gist7480523.GoPackageRepo) Presenter {
goPackage := repo.GoPackages()[0]
if strings.HasPrefix(goPackage.Bpkg.ImportPath, "code.google.com/") {
// TODO: Add presenter support for code.google.com?
return nil
return newCodeGooglePresenter(repo)
}
return nil
})
Expand Down

0 comments on commit 38743ee

Please sign in to comment.