Skip to content

Commit

Permalink
fix(lang): use local refs for repo resolution to bypass authentic… (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
princjef authored Feb 5, 2020
1 parent a059a74 commit 3187103
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ jobs:
with:
go-version: 1.13.x
- name: Checkout code
uses: actions/checkout@v1
uses: actions/checkout@v2
- name: Lint
uses: crazy-max/ghaction-mage@v1
with:
version: 1.9.0
args: lint
- name: Doc Verify
uses: crazy-max/ghaction-mage@v1
with:
version: 1.9.0
args: docVerify
# TODO: re-enable once the default branch is working in github actions
# - name: Doc Verify
# uses: crazy-max/ghaction-mage@v1
# with:
# version: 1.9.0
# args: docVerify
- name: Test
uses: crazy-max/ghaction-mage@v1
with:
Expand Down
14 changes: 7 additions & 7 deletions lang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ BlockKind identifies the type of block element represented by the corresponding
type BlockKind string
```

## type [Config](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L20-L26>)
## type [Config](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L19-L25>)

Config defines contextual information used to resolve documentation for a construct\.

Expand All @@ -152,15 +152,15 @@ type Config struct {
}
```

### func [NewConfig](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L56>)
### func [NewConfig](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L55>)

```go
func NewConfig(log logger.Logger, pkgDir string) (*Config, error)
```

NewConfig generates a Config for the provided package directory\. It will resolve the filepath and attempt to determine the repository containing the directory\. If no repository is found\, the Repo field will be set to nil\. An error is returned if the provided directory is invalid\.

### func \(\*Config\) [Inc](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L89>)
### func \(\*Config\) [Inc](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L88>)

```go
func (c *Config) Inc(step int) *Config
Expand Down Expand Up @@ -386,7 +386,7 @@ func (fn *Func) Title() string

Title provides the formatted name of the func\. It is primarily designed for generating headers\.

## type [Location](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L38-L43>)
## type [Location](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L37-L42>)

Location holds information for identifying a position within a file and repository\, if present\.

Expand All @@ -399,7 +399,7 @@ type Location struct {
}
```

### func [NewLocation](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L253>)
### func [NewLocation](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L266>)

```go
func NewLocation(cfg *Config, node ast.Node) Location
Expand Down Expand Up @@ -555,7 +555,7 @@ type PackageOptions struct {
}
```

## type [Position](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L46-L49>)
## type [Position](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L45-L48>)

Position represents a line and column number within a file\.

Expand All @@ -566,7 +566,7 @@ type Position struct {
}
```

## type [Repo](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L30-L34>)
## type [Repo](<https://github.com/princjef/gomarkdoc/blob/master/lang/config.go#L29-L33>)

Repo represents information about a repository relevant to documentation generation\.

Expand Down
38 changes: 28 additions & 10 deletions lang/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"go/ast"
"go/token"
"io"
"path/filepath"
"regexp"
"strings"

"github.com/princjef/gomarkdoc/logger"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)

type (
Expand Down Expand Up @@ -61,7 +61,7 @@ func NewConfig(log logger.Logger, pkgDir string) (*Config, error) {

repo, err := getRepoForDir(log, dir)
if err != nil {
log.Infof("unable to resolve repository due to error: %s")
log.Infof("unable to resolve repository due to error: %s", err)
return &Config{
FileSet: token.NewFileSet(),
Level: 1,
Expand Down Expand Up @@ -110,7 +110,7 @@ func getRepoForDir(log logger.Logger, dir string) (*Repo, error) {
}

for _, r := range remotes {
if repo, ok := processRemote(log, r); ok {
if repo, ok := processRemote(log, repo, r); ok {
ri = repo
break
}
Expand All @@ -122,7 +122,7 @@ func getRepoForDir(log logger.Logger, dir string) (*Repo, error) {
return nil, errors.New("no remotes found for repository")
}

repo, ok := processRemote(log, remotes[0])
repo, ok := processRemote(log, repo, remotes[0])
if !ok {
return nil, errors.New("no remotes found for repository")
}
Expand All @@ -140,7 +140,7 @@ func getRepoForDir(log logger.Logger, dir string) (*Repo, error) {
return ri, nil
}

func processRemote(log logger.Logger, remote *git.Remote) (*Repo, bool) {
func processRemote(log logger.Logger, repository *git.Repository, remote *git.Remote) (*Repo, bool) {
repo := &Repo{}

c := remote.Config()
Expand All @@ -151,15 +151,33 @@ func processRemote(log logger.Logger, remote *git.Remote) (*Repo, bool) {
return nil, false
}

refs, err := remote.List(&git.ListOptions{})
refs, err := repository.References()
if err != nil {
log.Debugf("skipping remote %s because list its refs failed: %s", c.URLs[0], err)
log.Debugf("skipping remote %s because listing its refs failed: %s", c.URLs[0], err)
return nil, false
}

for _, ref := range refs {
if ref.Name() == plumbing.HEAD && strings.HasPrefix(string(ref.Target()), "refs/heads/") {
repo.DefaultBranch = strings.TrimPrefix(string(ref.Target()), "refs/heads/")
prefix := fmt.Sprintf("refs/remotes/%s/", c.Name)
headRef := fmt.Sprintf("refs/remotes/%s/HEAD", c.Name)

for {
ref, err := refs.Next()
if err != nil {
if err == io.EOF {
break
}

log.Debugf("skipping remote %s because listing its refs failed: %s", c.URLs[0], err)
return nil, false
}
defer refs.Close()

if ref == nil {
break
}

if string(ref.Name()) == headRef && strings.HasPrefix(string(ref.Target()), prefix) {
repo.DefaultBranch = strings.TrimPrefix(string(ref.Target()), prefix)
log.Debugf("found default branch %s for remote %s", repo.DefaultBranch, c.URLs[0])
break
}
Expand Down

0 comments on commit 3187103

Please # to comment.