Skip to content

Commit

Permalink
add support for a custom default branch (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi authored Aug 10, 2020
1 parent 7c8864e commit 0ea80bc
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cmd/docsite/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func siteFromFlags() (*docsite.Site, *docsiteConfig, error) {
type docsiteConfig struct {
Content string
ContentExcludePattern string
DefaultContentBranch string
BaseURLPath string
Templates string
Assets string
Expand Down Expand Up @@ -187,6 +188,11 @@ func openDocsiteFromEnv() (*docsite.Site, *docsiteConfig, error) {
if err := json.Unmarshal([]byte(configData), &config); err != nil {
return nil, nil, errors.WithMessage(err, "reading docsite configuration")
}
if config.DefaultContentBranch == "" {
// Default to master out of convention. Alternatives like `main` can be set as well
// through the configuration.
config.DefaultContentBranch = "master"
}

// Read site data.
log.Println("# Downloading site data...")
Expand All @@ -200,7 +206,7 @@ func openDocsiteFromEnv() (*docsite.Site, *docsiteConfig, error) {
}

// Content is in a versioned file system.
content := &versionedFileSystemURL{url: config.Content}
content := &versionedFileSystemURL{url: config.Content, defaultBranch: config.DefaultContentBranch}
// Prefetch content at its default version. This ensures that the program exits if the content
// default version is unavailable.
if _, err := content.OpenVersion(context.Background(), ""); err != nil {
Expand All @@ -222,7 +228,8 @@ func openDocsiteFromEnv() (*docsite.Site, *docsiteConfig, error) {
}

type versionedFileSystemURL struct {
url string
url string
defaultBranch string

mu sync.Mutex
cache map[string]*fileSystemCacheEntry
Expand All @@ -240,11 +247,11 @@ const fileSystemCacheTTL = 5 * time.Minute
func (fs *versionedFileSystemURL) OpenVersion(ctx context.Context, version string) (http.FileSystem, error) {
// HACK(sqs): this works for codeload.github.com
if version == "" {
// HACK: Use master instead of HEAD even though master is technically incorrect in the
// HACK: Use a default branch instead of HEAD even though a branch is technically incorrect in the
// general case. This is because we require that $VERSION be interpolated into
// refs/heads/$VERSION not just $VERSION (to avoid the security problem described below),
// and refs/heads/HEAD doesn't work in general.
version = "master"
version = fs.defaultBranch
}
if strings.Contains(version, "..") || strings.Contains(version, "?") || strings.Contains(version, "#") {
return nil, fmt.Errorf("invalid version %q", version)
Expand Down

0 comments on commit 0ea80bc

Please sign in to comment.