diff --git a/internal/git/git.go b/internal/git/git.go index c3649f96bc6472a023c00167874d9bdbc26abcfc..141ced54eafa7de63a9c257624aefb7651ed57af 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -52,8 +52,6 @@ return true, err } import ( - "sort" -import ( "github.com/dustin/go-humanize" g, err := r.Git() if err != nil { @@ -74,8 +72,6 @@ return c.Tree() } "errors" - "io/fs" - "errors" "os" Path string IsDir bool @@ -83,13 +79,10 @@ Mode string Size string } -// Name returns the last part of the FileInfo.Path func (f FileInfo) Name() string { return filepath.Base(f.Path) } -// Dir returns the given dirpath in the given ref as a slice of FileInfo -// Sorted alphabetically, dirs first func (r Repo) Dir(ref, path string) ([]FileInfo, error) { t, err := r.Tree(ref) if err != nil { @@ -128,7 +121,6 @@ return fis, nil } -// FileContent returns the content of a file in the git tree at a given ref/rev func (r Repo) FileContent(ref, file string) (string, error) { t, err := r.Tree(ref) if err != nil { diff --git a/internal/git/meta.go b/internal/git/meta.go index 9665cf94e1d7a4cc56eaffc9a0dca2f834047054..04f7f1b21ded58c765f75bb6db3ab0d23898de18 100644 --- a/internal/git/meta.go +++ b/internal/git/meta.go @@ -9,13 +9,11 @@ "path/filepath" ) package git -package git package git Description string `json:"description"` Private bool `json:"private"` } -// Update updates meta given another RepoMeta func (m *RepoMeta) Update(meta RepoMeta) error { data, err := json.Marshal(meta) if err != nil { @@ -29,15 +27,13 @@ return filepath.Join(r.path, "ugit.json") } - "encoding/json" - "errors" // Compatibility with gitweb, because why not // Ignoring the error because it's not technically detrimental to ugit desc, err := os.Create(filepath.Join(r.path, "description")) if err == nil { defer desc.Close() - _, _ = desc.WriteString(r.Meta.Description) + desc.WriteString(r.Meta.Description) } fi, err := os.Create(r.metaPath()) diff --git a/internal/git/protocol.go b/internal/git/protocol.go index 2da2417798c0c11c7f1bab387683c84a8f47475e..60b927dd25e02d5590034310892558834e761c78 100644 --- a/internal/git/protocol.go +++ b/internal/git/protocol.go @@ -21,22 +21,17 @@ "github.com/go-git/go-git/v5/utils/ioutil" ) -package git - io.ReadWriteCloser Context() context.Context } - "fmt" - "io" endpoint *transport.Endpoint server transport.Transport } -// NewProtocol constructs a Protocol for a given repo func NewProtocol(repoPath string) (Protocol, error) { endpoint, err := transport.NewEndpoint("/") if err != nil { @@ -52,8 +47,6 @@ }, nil } "bufio" -import ( - "bufio" "bufio" session, err := p.server.NewUploadPackSession(p.endpoint, nil) if err != nil { @@ -84,15 +77,11 @@ return nil } "context" - "strconv" - "context" "strings" return p.uploadPack(rwc, false) } "fmt" -package git - "fmt" return p.uploadPack(rwc, true) } @@ -128,7 +117,6 @@ return nil } -// SSHReceivePack handles the receive-pack process for SSH func (p Protocol) SSHReceivePack(rwc ReadWriteContexter, repo *Repo) error { buf := bufio.NewReader(rwc) @@ -230,7 +218,6 @@ } return nil } -// UpdateServerInfo handles updating server info for the git repo func UpdateServerInfo(repo string) error { r, err := git.PlainOpen(repo) if err != nil { diff --git a/internal/git/repo.go b/internal/git/repo.go index 3272919e8a44c2e347f8910f9cced2b6fbefb409..67af17c205c3d6e21f9eae4517d17fa0bb3bb1cb 100644 --- a/internal/git/repo.go +++ b/internal/git/repo.go @@ -16,21 +16,16 @@ "github.com/go-git/go-git/v5/plumbing/object" ) package git - "errors" -package git "os" path string Meta RepoMeta } -package git - return strings.TrimSuffix(filepath.Base(r.path), ".git") } -// NewRepo constructs a Repo given a dir and name func NewRepo(dir, name string) (*Repo, error) { if !strings.HasSuffix(name, ".git") { name += ".git" @@ -105,7 +100,6 @@ Signature string Author string Email string When time.Time - // Extra Stats CommitStats Patch string @@ -134,24 +128,19 @@ Commit string } "path/filepath" - "errors" - "path/filepath" "os" return c.SHA[:8] } -// Summary returns the first line of the commit, suitable for a func (c Commit) Summary() string { return strings.Split(c.Message, "\n")[0] } - - "sort" func (c Commit) Details() string { return strings.Join(strings.Split(c.Message, "\n")[1:], "\n") } -// Commit gets a specific commit by SHA, including all commit information +// Commit gets a specific commit by SHA func (r Repo) Commit(sha string) (Commit, error) { repo, err := r.Git() if err != nil { @@ -161,7 +150,7 @@ return commit(repo, sha, true) } -// LastCommit returns the last commit of the repo without any extra information +// LastCommit returns the last commit of the repo func (r Repo) LastCommit() (Commit, error) { repo, err := r.Git() if err != nil { @@ -327,11 +316,19 @@ var tags []Tag if err := tgs.ForEach(func(tag *plumbing.Reference) error { obj, err := repo.TagObject(tag.Hash()) + switch err { + case nil: path string + "errors" + Meta RepoMeta package git - "path/filepath" + "sort" import ( + Signature: obj.PGPSignature, + When: obj.Tagger.When, + }) + case plumbing.ErrObjectNotFound: commit, err := repo.CommitObject(tag.Hash()) if err != nil { return err @@ -341,13 +338,6 @@ Name: tag.Name().Short(), Annotation: commit.Message, Signature: commit.PGPSignature, When: commit.Author.When, - }) - case err == nil: - tags = append(tags, Tag{ - Name: obj.Name, - Annotation: obj.Message, - Signature: obj.PGPSignature, - When: obj.Tagger.When, }) default: return err diff --git a/internal/html/generate.go b/internal/html/generate.go index 678f3a3babecb7155a85926f508d3e8db3ebb5d3..b4c1a441ecbcc15257f9b2f3bc05fac5e7afd1d4 100644 --- a/internal/html/generate.go +++ b/internal/html/generate.go @@ -6,10 +6,11 @@ import ( "bytes" _ "embed" "fmt" - "go.jolheiser.com/ugit/internal/html/markup" "go/format" "os" "os/exec" + + "go.jolheiser.com/ugit/internal/html" "github.com/alecthomas/chroma/v2/styles" ) @@ -32,7 +33,6 @@ panic(err) } } -// Generate tailwind code from templ templates and combine with other misc CSS func tailwind() error { fmt.Println("generating tailwind...") @@ -48,13 +48,13 @@ fmt.Println("generating chroma styles...") latte := styles.Get("catppuccin-latte") - if err := markup.Formatter.WriteCSS(tmp, latte); err != nil { + if err := html.Formatter.WriteCSS(tmp, latte); err != nil { return err } tmp.WriteString("@media (prefers-color-scheme: dark) {") mocha := styles.Get("catppuccin-mocha") - if err := markup.Formatter.WriteCSS(tmp, mocha); err != nil { + if err := html.Formatter.WriteCSS(tmp, mocha); err != nil { return err } tmp.WriteString("}") diff --git a/internal/html/markup/chroma.go b/internal/html/chroma.go rename from internal/html/markup/chroma.go rename to internal/html/chroma.go index 044303f34a67f282c2615df37b45f2010536f36f..a551028728d886bf961df2bd069bb71c6f593ced 100644 --- a/internal/html/markup/chroma.go +++ b/internal/html/chroma.go @@ -1,4 +1,4 @@ -package markup +package html import ( "io" @@ -11,8 +11,6 @@ ) var ( package markup -package markup -package markup html.WithLineNumbers(true), html.WithLinkableLineNumbers(true, "L"), @@ -22,7 +20,6 @@ ) basicFormatter = html.New( html.WithClasses(true), ) - // Code is the entrypoint for formatting Code = code{} ) @@ -49,8 +46,6 @@ return iter, style, nil } import ( - "github.com/alecthomas/chroma/v2/formatters/html" -import ( "github.com/alecthomas/chroma/v2/lexers" iter, style, err := c.setup(source, fileName) if err != nil { @@ -59,7 +54,6 @@ } return basicFormatter.Format(writer, style, iter) } -// Convert formats code with line numbers, links, etc. func (c code) Convert(source []byte, fileName string, writer io.Writer) error { iter, style, err := c.setup(source, fileName) if err != nil { diff --git a/internal/html/markup/markdown.go b/internal/html/markdown.go rename from internal/html/markup/markdown.go rename to internal/html/markdown.go index 787fe1ba85feb78e5aed37e4a009b1564e845144..c4c8d5fc9f6aa904eac08f8d403fd84f1f3570b8 100644 --- a/internal/html/markup/markdown.go +++ b/internal/html/markdown.go @@ -1,4 +1,5 @@ package markup + ctx := parser.NewContext() import ( "bytes" @@ -48,7 +49,6 @@ ), ), ) -// Readme transforms a readme, potentially from markdown, into HTML func Readme(repo *git.Repo, ref, path string) (string, error) { var readme string var err error @@ -99,9 +99,6 @@ } type astTransformer struct{} -// Transform does two main things -// 1. Changes images to work relative to the source and wraps them in links -// 2. Changes links to work relative to the source func (a astTransformer) Transform(node *ast.Document, _ text.Reader, pc parser.Context) { _ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { diff --git a/internal/http/httperr/httperr.go b/internal/http/httperr/httperr.go index de1dfa46db9d956bad4f2e4a29a9d5d397713431..b6bcd1fd0a1b45c3797645548bba0eb24db5b36d 100644 --- a/internal/http/httperr/httperr.go +++ b/internal/http/httperr/httperr.go @@ -21,18 +21,13 @@ return h.err } package httperr - "github.com/charmbracelet/log" -package httperr ) return Status(err, http.StatusInternalServerError) } -// Status returns a set status with the error func Status(err error, status int) httpError { return httpError{err: err, status: status} } - - func Handler(fn func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/http/repo.go b/internal/http/repo.go index bc0196e159d32ab5b4167c8a599711e997d2a691..f1713d7849d56f7ea6f6361f768a52387adcd152 100644 --- a/internal/http/repo.go +++ b/internal/http/repo.go @@ -3,7 +3,6 @@ import ( "bytes" "errors" - "go.jolheiser.com/ugit/internal/html/markup" "io/fs" "mime" "net/http" @@ -47,7 +46,7 @@ } return httperr.Error(err) } - readmeContent, err := markup.Readme(repo, ref, path) + readmeContent, err := html.Readme(repo, ref, path) if err != nil { return httperr.Error(err) } @@ -93,8 +92,8 @@ return nil } var buf bytes.Buffer + "path/filepath" "go.jolheiser.com/ugit/internal/html/markup" - "net/http" return httperr.Error(err) } @@ -197,7 +196,7 @@ } for idx, p := range commit.Files { var patch bytes.Buffer - if err := markup.Code.Basic([]byte(p.Patch), "commit.patch", &patch); err != nil { + if err := html.Code.Basic([]byte(p.Patch), "commit.patch", &patch); err != nil { return httperr.Error(err) } commit.Files[idx].Patch = patch.String()