forked from tangled.org/core
this repo has no description

appview: repo commit

Moves the NiceDiff type to the shared types package.

Changed files
+221 -141
.zed
appview
pages
state
knotserver
types
+15
.zed/settings.json
···
+
// Folder-specific settings
+
//
+
// For a full list of overridable settings, and general information on folder-specific settings,
+
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
+
{
+
"languages": {
+
"HTML": {
+
"prettier": {
+
"allowed": true,
+
"parser": "go-template",
+
"plugins": ["prettier-plugin-go-template"]
+
}
+
}
+
}
+
}
+10
appview/pages/pages.go
···
func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
return p.execute("repo/log", w, params)
}
+
+
type RepoCommitParams struct {
+
LoggedInUser *auth.User
+
RepoInfo RepoInfo
+
types.RepoCommitResponse
+
}
+
+
func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
+
return p.execute("repo/commit", w, params)
+
}
+96 -96
appview/pages/templates/repo/commit.html
···
-
<html>
-
{{ template "layouts/head" . }}
+
{{ define "title" }} commit {{ .Diff.Commit.This }} {{ end }}
-
{{ template "layouts/repo-header" . }}
-
<body>
-
{{ template "layouts/nav" . }}
-
<main>
-
<section class="commit">
-
<pre>{{- .commit.Message -}}</pre>
-
<div class="commit-info">
-
{{ .commit.Author.Name }} <a href="mailto:{{ .commit.Author.Email }}" class="commit-email">{{ .commit.Author.Email}}</a>
-
<div>{{ .commit.Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</div>
-
</div>
+
{{ define "content" }}
-
<div>
-
<strong>commit</strong>
-
<p><a href="/{{ .name }}/commit/{{ .commit.This }}" class="commit-hash">
-
{{ .commit.This }}
-
</a>
-
</p>
-
</div>
+
{{ $repo := .RepoInfo.Name }}
+
{{ $commit := .Diff.Commit }}
+
{{ $stat := .Diff.Stat }}
+
{{ $diff := .Diff.Diff }}
-
{{ if .commit.Parent }}
-
<div>
-
<strong>parent</strong>
-
<p><a href="/{{ .name }}/commit/{{ .commit.Parent }}" class="commit-hash">
-
{{ .commit.Parent }}
-
</a></p>
-
</div>
+
<section class="commit">
+
<pre>{{- $commit.Message -}}</pre>
+
<div class="commit-info">
+
{{ $commit.Author.Name }} <a href="mailto:{{ $commit.Author.Email }}" class="commit-email">{{ $commit.Author.Email}}</a>
+
<div>{{ $commit.Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</div>
+
</div>
-
{{ end }}
-
<div class="diff-stat">
-
<div>
-
{{ .stat.FilesChanged }} files changed,
-
{{ .stat.Insertions }} insertions(+),
-
{{ .stat.Deletions }} deletions(-)
-
</div>
-
<div>
-
<br>
-
<strong>jump to</strong>
-
{{ range .diff }}
-
<ul>
-
<li><a href="#{{ .Name.New }}">{{ .Name.New }}</a></li>
-
</ul>
-
{{ end }}
-
</div>
-
</div>
-
</section>
-
<section>
-
{{ $repo := .name }}
-
{{ $this := .commit.This }}
-
{{ $parent := .commit.Parent }}
-
{{ range .diff }}
-
<div id="{{ .Name.New }}">
-
<div class="diff">
-
{{ if .IsNew }}
-
<span class="diff-type">A</span>
-
{{ end }}
-
{{ if .IsDelete }}
-
<span class="diff-type">D</span>
-
{{ end }}
-
{{ if not (or .IsNew .IsDelete) }}
-
<span class="diff-type">M</span>
-
{{ end }}
-
{{ if .Name.Old }}
-
<a href="/{{ $repo }}/blob/{{ $parent }}/{{ .Name.Old }}">{{ .Name.Old }}</a>
-
{{ if .Name.New }}
-
&#8594;
-
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
-
{{ end }}
-
{{ else }}
-
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
-
{{- end -}}
-
{{ if .IsBinary }}
-
<p>Not showing binary file.</p>
-
{{ else }}
-
<pre>
-
{{- range .TextFragments -}}
-
<p>{{- .Header -}}</p>
-
{{- range .Lines -}}
-
{{- if eq .Op.String "+" -}}
-
<span class="diff-add">{{ .String }}</span>
-
{{- end -}}
-
{{- if eq .Op.String "-" -}}
-
<span class="diff-del">{{ .String }}</span>
-
{{- end -}}
-
{{- if eq .Op.String " " -}}
-
<span class="diff-noop">{{ .String }}</span>
-
{{- end -}}
-
{{- end -}}
-
{{- end -}}
-
{{- end -}}
-
</pre>
-
</div>
-
</div>
-
{{ end }}
-
</section>
-
</main>
-
</body>
-
</html>
+
<div>
+
<strong>commit</strong>
+
<p><a href="/{{ $repo }}/commit/{{ $commit.This }}" class="commit-hash">
+
{{ $commit.This }}
+
</a>
+
</p>
+
</div>
+
+
{{ if $commit.Parent }}
+
<div>
+
<strong>parent</strong>
+
<p><a href="/{{ $repo }}/commit/{{ $commit.Parent }}" class="commit-hash">
+
{{ $commit.Parent }}
+
</a></p>
+
</div>
+
+
{{ end }}
+
<div class="diff-stat">
+
<div>
+
{{ $stat.FilesChanged }} files changed,
+
{{ $stat.Insertions }} insertions(+),
+
{{ $stat.Deletions }} deletions(-)
+
</div>
+
<div>
+
<br>
+
<strong>jump to</strong>
+
{{ range $diff }}
+
<ul>
+
<li><a href="#{{ .Name.New }}">{{ .Name.New }}</a></li>
+
</ul>
+
{{ end }}
+
</div>
+
</div>
+
</section>
+
<section>
+
{{ $this := $commit.This }}
+
{{ $parent := $commit.Parent }}
+
{{ range $diff }}
+
<div id="{{ .Name.New }}">
+
<div class="diff">
+
{{ if .IsNew }}
+
<span class="diff-type">A</span>
+
{{ end }}
+
{{ if .IsDelete }}
+
<span class="diff-type">D</span>
+
{{ end }}
+
{{ if not (or .IsNew .IsDelete) }}
+
<span class="diff-type">M</span>
+
{{ end }}
+
{{ if .Name.Old }}
+
<a href="/{{ $repo }}/blob/{{ $parent }}/{{ .Name.Old }}">{{ .Name.Old }}</a>
+
{{ if .Name.New }}
+
&#8594;
+
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
+
{{ end }}
+
{{ else }}
+
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
+
{{- end -}}
+
{{ if .IsBinary }}
+
<p>Not showing binary file.</p>
+
{{ else }}
+
<pre>
+
{{- range .TextFragments -}}
+
<p>{{- .Header -}}</p>
+
{{- range .Lines -}}
+
{{- if eq .Op.String "+" -}}
+
<span class="diff-add">{{ .String }}</span>
+
{{- end -}}
+
{{- if eq .Op.String "-" -}}
+
<span class="diff-del">{{ .String }}</span>
+
{{- end -}}
+
{{- if eq .Op.String " " -}}
+
<span class="diff-noop">{{ .String }}</span>
+
{{- end -}}
+
{{- end -}}
+
{{- end -}}
+
{{- end -}}
+
</pre>
+
</div>
+
</div>
+
{{ end }}
+
</section>
+
+
{{end}}
+1 -1
appview/pages/templates/repo/empty.html
···
{{ template "layouts/head" . }}
<body>
-
<main>
+
<main></main>
<p>This is an empty Git repository. Push some commits here.</p>
</main>
</body>
+39
appview/state/repo.go
···
return
}
+
func (s *State) RepoCommit(w http.ResponseWriter, r *http.Request) {
+
repoName, knot, id, err := repoKnotAndId(r)
+
if err != nil {
+
log.Println("failed to get repo and knot", err)
+
return
+
}
+
+
ref := chi.URLParam(r, "ref")
+
resp, err := http.Get(fmt.Sprintf("http://%s/%s/%s/commit/%s", knot, id.DID.String(), repoName, ref))
+
if err != nil {
+
log.Println("failed to reach knotserver", err)
+
return
+
}
+
+
body, err := io.ReadAll(resp.Body)
+
if err != nil {
+
log.Fatalf("Error reading response body: %v", err)
+
return
+
}
+
+
var result types.RepoCommitResponse
+
err = json.Unmarshal(body, &result)
+
if err != nil {
+
log.Println("failed to parse response:", err)
+
return
+
}
+
+
s.pages.RepoCommit(w, pages.RepoCommitParams{
+
LoggedInUser: s.auth.GetUser(r),
+
RepoInfo: pages.RepoInfo{
+
OwnerDid: id.DID.String(),
+
OwnerHandle: id.Handle.String(),
+
Name: repoName,
+
},
+
RepoCommitResponse: result,
+
})
+
return
+
}
+
func repoKnotAndId(r *http.Request) (string, string, identity.Identity, error) {
repoName := chi.URLParam(r, "repo")
knot, ok := r.Context().Value("knot").(string)
+1
appview/state/state.go
···
r.With(ResolveRepoKnot(s)).Route("/{repo}", func(r chi.Router) {
r.Get("/", s.RepoIndex)
r.Get("/log/{ref}", s.RepoLog)
+
r.Get("/commit/{ref}", s.RepoCommit)
// These routes get proxied to the knot
r.Get("/info/refs", s.InfoRefs)
+5 -36
knotserver/git/diff.go
···
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/go-git/go-git/v5/plumbing/object"
+
"github.com/sotangled/tangled/types"
)
-
type TextFragment struct {
-
Header string
-
Lines []gitdiff.Line
-
}
-
-
type Diff struct {
-
Name struct {
-
Old string
-
New string
-
}
-
TextFragments []TextFragment
-
IsBinary bool
-
IsNew bool
-
IsDelete bool
-
}
-
-
// A nicer git diff representation.
-
type NiceDiff struct {
-
Commit struct {
-
Message string
-
Author object.Signature
-
This string
-
Parent string
-
}
-
Stat struct {
-
FilesChanged int
-
Insertions int
-
Deletions int
-
}
-
Diff []Diff
-
}
-
-
func (g *GitRepo) Diff() (*NiceDiff, error) {
+
func (g *GitRepo) Diff() (*types.NiceDiff, error) {
c, err := g.r.CommitObject(g.h)
if err != nil {
return nil, fmt.Errorf("commit object: %w", err)
···
log.Println(err)
}
-
nd := NiceDiff{}
+
nd := types.NiceDiff{}
nd.Commit.This = c.Hash.String()
if parent.Hash.IsZero() {
···
nd.Commit.Message = c.Message
for _, d := range diffs {
-
ndiff := Diff{}
+
ndiff := types.Diff{}
ndiff.Name.New = d.NewName
ndiff.Name.Old = d.OldName
ndiff.IsBinary = d.IsBinary
···
ndiff.IsDelete = d.IsDelete
for _, tf := range d.TextFragments {
-
ndiff.TextFragments = append(ndiff.TextFragments, TextFragment{
+
ndiff.TextFragments = append(ndiff.TextFragments, types.TextFragment{
Header: tf.Header(),
Lines: tf.Lines,
})
+5 -8
knotserver/routes.go
···
return
}
-
data := make(map[string]interface{})
+
resp := types.RepoCommitResponse{
+
Ref: ref,
+
Diff: diff,
+
}
-
data["commit"] = diff.Commit
-
data["stat"] = diff.Stat
-
data["diff"] = diff.Diff
-
data["ref"] = ref
-
data["desc"] = getDescription(path)
-
-
writeJSON(w, data)
+
writeJSON(w, resp)
return
}
+38
types/diff.go
···
+
package types
+
+
import (
+
"github.com/bluekeyes/go-gitdiff/gitdiff"
+
"github.com/go-git/go-git/v5/plumbing/object"
+
)
+
+
type TextFragment struct {
+
Header string `json:"header"`
+
Lines []gitdiff.Line `json:"lines"`
+
}
+
+
type Diff struct {
+
Name struct {
+
Old string `json:"old"`
+
New string `json:"new"`
+
} `json:"name"`
+
TextFragments []TextFragment `json:"text_fragments"`
+
IsBinary bool `json:"is_binary"`
+
IsNew bool `json:"is_new"`
+
IsDelete bool `json:"is_delete"`
+
}
+
+
// A nicer git diff representation.
+
type NiceDiff struct {
+
Commit struct {
+
Message string `json:"message"`
+
Author object.Signature `json:"author"`
+
This string `json:"this"`
+
Parent string `json:"parent"`
+
} `json:"commit"`
+
Stat struct {
+
FilesChanged int `json:"files_changed"`
+
Insertions int `json:"insertions"`
+
Deletions int `json:"deletions"`
+
} `json:"stat"`
+
Diff []Diff `json:"diff"`
+
}
+11
types/repo.go
···
Page int `json:"page,omitempty"`
PerPage int `json:"per_page,omitempty"`
}
+
+
// data["commit"] = diff.Commit
+
//
+
// data["stat"] = diff.Stat
+
// data["diff"] = diff.Diff
+
// data["ref"] = ref
+
// data["desc"] = getDescription(path)
+
type RepoCommitResponse struct {
+
Ref string `json:"ref,omitempty"`
+
Diff *NiceDiff `json:"diff,omitempty"`
+
}