3.4 KB
repo.go
package sharing

import "html/template"

// Repo provides git repository browsing for a specific path.
type Repo struct {
	path string
	host string
}

// NewRepo creates a Repo for browsing at the given path.
func NewRepo(host, path string) *Repo {
	return &Repo{host: host, path: path}
}

// --- Repository info ---

// HasCommits returns true if the repository has commits.
func (r *Repo) HasCommits() bool {
	return HasRepo()
}

// CloneURL returns the HTTPS clone URL.
func (r *Repo) CloneURL() string {
	return CloneURL(r.host)
}

// Branch returns the current branch name.
func (r *Repo) Branch() string {
	return GetBranch()
}

// CommitCount returns the total number of commits.
func (r *Repo) CommitCount() int {
	return GetCommitCount()
}

// Commits returns recent commits.
func (r *Repo) Commits() []Commit {
	commits, _ := GetCommits(50)
	return commits
}

// LastCommit returns the most recent commit.
func (r *Repo) LastCommit() *Commit {
	commit, _ := GetLastCommit()
	return commit
}

// --- Path navigation ---

// Path returns the current path.
func (r *Repo) Path() string {
	return r.path
}

// PathParts returns breadcrumb parts for the current path.
func (r *Repo) PathParts() []PathPart {
	return ParsePathParts(r.path)
}

// ParentPath returns the parent directory path.
func (r *Repo) ParentPath() string {
	return ParentPath(r.path)
}

// --- Tree browsing ---

// Entries returns tree entries for the current path.
func (r *Repo) Entries() []TreeEntry {
	entries, _ := GetTree(r.path)
	return entries
}

// RootEntries returns tree entries for the root directory.
func (r *Repo) RootEntries() []TreeEntry {
	entries, _ := GetTree("")
	return entries
}

// --- File info ---

// FileName returns the name of the current file.
func (r *Repo) FileName() string {
	return FileName(r.path)
}

// FileExtension returns the file extension.
func (r *Repo) FileExtension() string {
	return FileExtension(r.path)
}

// FileSize returns the size of the current file.
func (r *Repo) FileSize() int64 {
	size, _ := GetFileSize(r.path)
	return size
}

// FileContent returns the content of the current file.
func (r *Repo) FileContent() string {
	content, err := GetFile(r.path)
	if err != nil {
		return "Error loading file: " + err.Error()
	}
	return content
}

// FileContentHTML returns the file content for display (auto-escaped by templates).
func (r *Repo) FileContentHTML() string {
	return r.FileContent()
}

// IsTextFile returns true if the file appears to be text.
func (r *Repo) IsTextFile() bool {
	return IsTextFile(r.path)
}

// IsMarkdown returns true if the file is a markdown file.
func (r *Repo) IsMarkdown() bool {
	return IsMarkdown(r.path)
}

// FileContentRendered returns markdown rendered as HTML.
func (r *Repo) FileContentRendered() template.HTML {
	if !r.IsMarkdown() {
		return ""
	}
	return template.HTML(RenderMarkdown(r.FileContent()))
}

// PrismLanguage returns the Prism.js language class for syntax highlighting.
func (r *Repo) PrismLanguage() string {
	return PrismLanguage(r.path)
}

// --- README ---

// HasReadme returns true if a README.md exists.
func (r *Repo) HasReadme() bool {
	return HasReadme()
}

// ReadmeHTML returns the README.md content rendered as HTML.
func (r *Repo) ReadmeHTML() template.HTML {
	return template.HTML(GetReadmeHTML())
}

// --- Utilities ---

// FormatSize formats a file size in human readable format.
func (r *Repo) FormatSize(size int64) string {
	return FormatSize(size)
}
← Back