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 support for a custom default branch #52

Merged
merged 1 commit into from
Aug 10, 2020
Merged
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
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