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

add nicediff view to pulls

Changed files
+175 -110
appview
db
pages
templates
fragments
repo
+44
appview/db/pulls.go
···
import (
"database/sql"
"fmt"
+
"log"
"strings"
"time"
+
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/bluesky-social/indigo/atproto/syntax"
+
"github.com/sotangled/tangled/types"
)
type PullState int
···
func (p *Pull) LatestPatch() string {
latestSubmission := p.Submissions[len(p.Submissions)-1]
return latestSubmission.Patch
+
}
+
+
func (s PullSubmission) AsNiceDiff(targetBranch string) types.NiceDiff {
+
patch := s.Patch
+
+
diffs, _, err := gitdiff.Parse(strings.NewReader(patch))
+
if err != nil {
+
log.Println(err)
+
}
+
+
nd := types.NiceDiff{}
+
nd.Commit.Parent = targetBranch
+
+
for _, d := range diffs {
+
ndiff := types.Diff{}
+
ndiff.Name.New = d.NewName
+
ndiff.Name.Old = d.OldName
+
ndiff.IsBinary = d.IsBinary
+
ndiff.IsNew = d.IsNew
+
ndiff.IsDelete = d.IsDelete
+
ndiff.IsCopy = d.IsCopy
+
ndiff.IsRename = d.IsRename
+
+
for _, tf := range d.TextFragments {
+
ndiff.TextFragments = append(ndiff.TextFragments, *tf)
+
for _, l := range tf.Lines {
+
switch l.Op {
+
case gitdiff.OpAdd:
+
nd.Stat.Insertions += 1
+
case gitdiff.OpDelete:
+
nd.Stat.Deletions += 1
+
}
+
}
+
}
+
+
nd.Diff = append(nd.Diff, ndiff)
+
}
+
+
nd.Stat.FilesChanged = len(diffs)
+
+
return nd
}
func NewPull(tx *sql.Tx, pull *Pull) error {
+112
appview/pages/templates/fragments/diff.html
···
+
{{ define "fragments/diff" }}
+
{{ $repo := index . 0 }}
+
{{ $diff := index . 1 }}
+
{{ $commit := $diff.Commit }}
+
{{ $stat := $diff.Stat }}
+
{{ $diff := $diff.Diff }}
+
+
{{ $this := $commit.This }}
+
{{ $parent := $commit.Parent }}
+
+
{{ $last := sub (len $diff) 1 }}
+
{{ range $idx, $hunk := $diff }}
+
{{ with $hunk }}
+
<section class="mt-6 border border-gray-200 w-full mx-auto rounded bg-white drop-shadow-sm">
+
<div id="file-{{ .Name.New }}">
+
<div id="diff-file">
+
<details open>
+
<summary class="list-none cursor-pointer sticky top-0">
+
<div id="diff-file-header" class="rounded cursor-pointer bg-white flex justify-between">
+
<div id="left-side-items" class="p-2 flex gap-2 items-center">
+
{{ $markerstyle := "diff-type p-1 mr-1 font-mono text-sm rounded select-none" }}
+
+
{{ if .IsNew }}
+
<span class="bg-green-100 text-green-700 {{ $markerstyle }}">ADDED</span>
+
{{ else if .IsDelete }}
+
<span class="bg-red-100 text-red-700 {{ $markerstyle }}">DELETED</span>
+
{{ else if .IsCopy }}
+
<span class="bg-gray-100 text-gray-700 {{ $markerstyle }}">COPIED</span>
+
{{ else if .IsRename }}
+
<span class="bg-gray-100 text-gray-700 {{ $markerstyle }}">RENAMED</span>
+
{{ else }}
+
<span class="bg-gray-100 text-gray-700 {{ $markerstyle }}">MODIFIED</span>
+
{{ end }}
+
+
{{ if .IsDelete }}
+
<a {{if $this }}href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.Old }}"{{end}}>
+
{{ .Name.Old }}
+
</a>
+
{{ else if (or .IsCopy .IsRename) }}
+
<a {{if $parent}}href="/{{ $repo }}/blob/{{ $parent }}/{{ .Name.Old }}"{{end}}>
+
{{ .Name.Old }}
+
</a>
+
<i class="w-4 h-4" data-lucide="arrow-right"></i>
+
<a {{if $this}}href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}"{{end}}>
+
{{ .Name.New }}
+
</a>
+
{{ else }}
+
<a {{if $this}}href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}"{{end}}>
+
{{ .Name.New }}
+
</a>
+
{{ end }}
+
</div>
+
+
{{ $iconstyle := "p-1 mx-1 hover:bg-gray-100 rounded" }}
+
<div id="right-side-items" class="p-2 flex items-center">
+
<a title="top of file" href="#file-{{ .Name.New }}" class="{{ $iconstyle }}"><i class="w-4 h-4" data-lucide="arrow-up-to-line"></i></a>
+
{{ if gt $idx 0 }}
+
{{ $prev := index $diff (sub $idx 1) }}
+
<a title="previous file" href="#file-{{ $prev.Name.New }}" class="{{ $iconstyle }}"><i class="w-4 h-4" data-lucide="arrow-up"></i></a>
+
{{ end }}
+
+
{{ if lt $idx $last }}
+
{{ $next := index $diff (add $idx 1) }}
+
<a title="next file" href="#file-{{ $next.Name.New }}" class="{{ $iconstyle }}"><i class="w-4 h-4" data-lucide="arrow-down"></i></a>
+
{{ end }}
+
</div>
+
+
</div>
+
</summary>
+
+
<div class="transition-all duration-700 ease-in-out">
+
{{ if .IsDelete }}
+
<p class="text-center text-gray-400 p-4">
+
This file has been deleted in this commit.
+
</p>
+
{{ else }}
+
{{ if .IsBinary }}
+
<p class="text-center text-gray-400 p-4">
+
This is a binary file and will not be displayed.
+
</p>
+
{{ else }}
+
<pre class="overflow-auto">
+
{{- range .TextFragments -}}
+
<div class="bg-gray-100 text-gray-500 select-none">{{ .Header }}</div>
+
{{- range .Lines -}}
+
{{- if eq .Op.String "+" -}}
+
<div class="bg-green-100 text-green-700 p-1"><span class="select-none mx-2">{{ .Op.String }}</span><span>{{ .Line }}</span></div>
+
{{- end -}}
+
+
{{- if eq .Op.String "-" -}}
+
<div class="bg-red-100 text-red-700 p-1"><span class="select-none mx-2">{{ .Op.String }}</span><span>{{ .Line }}</span></div>
+
{{- end -}}
+
+
{{- if eq .Op.String " " -}}
+
<div class="bg-white text-gray-500 px"><span class="select-none mx-2">{{ .Op.String }}</span><span>{{ .Line }}</span></div>
+
{{- end -}}
+
+
{{- end -}}
+
{{- end -}}
+
</pre>
+
{{- end -}}
+
{{ end }}
+
</div>
+
+
</details>
+
+
</div>
+
</div>
+
</section>
+
{{ end }}
+
{{ end }}
+
{{ end }}
+1 -103
appview/pages/templates/repo/commit.html
···
{{end}}
{{ define "repoAfter" }}
-
-
{{ $repo := .RepoInfo.FullName }}
-
{{ $commit := .Diff.Commit }}
-
{{ $stat := .Diff.Stat }}
-
{{ $diff := .Diff.Diff }}
-
-
{{ $this := $commit.This }}
-
{{ $parent := $commit.Parent }}
-
-
{{ $last := sub (len $diff) 1 }}
-
{{ range $idx, $hunk := $diff }}
-
{{ with $hunk }}
-
<section class="mt-6 border border-gray-200 w-full mx-auto rounded bg-white drop-shadow-sm">
-
<div id="file-{{ .Name.New }}">
-
<div id="diff-file">
-
<details open>
-
<summary class="list-none cursor-pointer sticky top-0">
-
<div id="diff-file-header" class="rounded cursor-pointer bg-white flex justify-between">
-
<div id="left-side-items" class="p-2 flex gap-2 items-center">
-
{{ $markerstyle := "diff-type p-1 mr-1 font-mono text-sm rounded select-none" }}
-
-
{{ if .IsNew }}
-
<span class="bg-green-100 text-green-700 {{ $markerstyle }}">ADDED</span>
-
{{ else if .IsDelete }}
-
<span class="bg-red-100 text-red-700 {{ $markerstyle }}">DELETED</span>
-
{{ else if .IsCopy }}
-
<span class="bg-gray-100 text-gray-700 {{ $markerstyle }}">COPIED</span>
-
{{ else if .IsRename }}
-
<span class="bg-gray-100 text-gray-700 {{ $markerstyle }}">RENAMED</span>
-
{{ else }}
-
<span class="bg-gray-100 text-gray-700 {{ $markerstyle }}">MODIFIED</span>
-
{{ end }}
-
-
{{ if .IsDelete }}
-
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.Old }}">{{ .Name.Old }}</a>
-
{{ else if (or .IsCopy .IsRename) }}
-
<a href="/{{ $repo }}/blob/{{ $parent }}/{{ .Name.Old }}">{{ .Name.Old }}</a>
-
<i class="w-4 h-4" data-lucide="arrow-right"></i>
-
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
-
{{ else }}
-
<a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
-
{{ end }}
-
</div>
-
-
{{ $iconstyle := "p-1 mx-1 hover:bg-gray-100 rounded" }}
-
<div id="right-side-items" class="p-2 flex items-center">
-
<a title="top of file" href="#file-{{ .Name.New }}" class="{{ $iconstyle }}"><i class="w-4 h-4" data-lucide="arrow-up-to-line"></i></a>
-
{{ if gt $idx 0 }}
-
{{ $prev := index $diff (sub $idx 1) }}
-
<a title="previous file" href="#file-{{ $prev.Name.New }}" class="{{ $iconstyle }}"><i class="w-4 h-4" data-lucide="arrow-up"></i></a>
-
{{ end }}
-
-
{{ if lt $idx $last }}
-
{{ $next := index $diff (add $idx 1) }}
-
<a title="next file" href="#file-{{ $next.Name.New }}" class="{{ $iconstyle }}"><i class="w-4 h-4" data-lucide="arrow-down"></i></a>
-
{{ end }}
-
</div>
-
-
</div>
-
</summary>
-
-
<div class="transition-all duration-700 ease-in-out">
-
{{ if .IsDelete }}
-
<p class="text-center text-gray-400 p-4">
-
This file has been deleted in this commit.
-
</p>
-
{{ else }}
-
{{ if .IsBinary }}
-
<p class="text-center text-gray-400 p-4">
-
This is a binary file and will not be displayed.
-
</p>
-
{{ else }}
-
<pre class="overflow-auto">
-
{{- range .TextFragments -}}
-
<div class="bg-gray-100 text-gray-500 select-none">{{ .Header }}</div>
-
{{- range .Lines -}}
-
{{- if eq .Op.String "+" -}}
-
<div class="bg-green-100 text-green-700 p-1"><span class="select-none mx-2">{{ .Op.String }}</span><span>{{ .Line }}</span></div>
-
{{- end -}}
-
-
{{- if eq .Op.String "-" -}}
-
<div class="bg-red-100 text-red-700 p-1"><span class="select-none mx-2">{{ .Op.String }}</span><span>{{ .Line }}</span></div>
-
{{- end -}}
-
-
{{- if eq .Op.String " " -}}
-
<div class="bg-white text-gray-500 px"><span class="select-none mx-2">{{ .Op.String }}</span><span>{{ .Line }}</span></div>
-
{{- end -}}
-
-
{{- end -}}
-
{{- end -}}
-
</pre>
-
{{- end -}}
-
{{ end }}
-
</div>
-
-
</details>
-
-
</div>
-
</div>
-
</section>
-
{{ end }}
-
{{ end }}
-
+
{{ template "fragments/diff" (list .RepoInfo.FullName .Diff) }}
{{end}}
+1 -1
appview/pages/templates/repo/index.html
···
{{ define "branchSelector" }}
<div class="flex justify-between pb-5">
<select
-
onchange="window.location.href = '/{{ .RepoInfo.FullName }}/tree/' + this.value"
+
onchange="window.location.href = '/{{ .RepoInfo.FullName }}/tree/' + encodeURIComponent(this.value)"
class="p-1 border border-gray-200 bg-white"
>
<optgroup label="branches" class="bold text-sm">
+6 -5
appview/pages/templates/repo/pulls/pull.html
···
{{ define "submissions" }}
{{ $lastIdx := sub (len .Pull.Submissions) 1 }}
+
{{ $targetBranch := .Pull.TargetBranch }}
+
{{ $repoName := .RepoInfo.FullName }}
{{ range $idx, $item := .Pull.Submissions }}
+
{{ $diff := $item.AsNiceDiff $targetBranch }}
{{ with $item }}
{{ $oneIndexedRound := add .RoundNumber 1 }}
<details {{ if eq $idx $lastIdx }}open{{ end }}>
···
<span>
{{ $owner := index $.DidHandleMap $.Pull.OwnerDid }}
submitted by <a href="/{{ $owner }}">{{ $owner }}</a>
-
<span class="before:content-['·']"></span>
-
<a href="/{{ $.RepoInfo.FullName }}/pulls/{{ $.Pull.PullId }}/patch">view patch</a>
<span class="select-none before:content-['\00B7']"></span>
<time>{{ .Created | timeFmt }}</time>
<span class="select-none before:content-['·']"></span>
···
</div>
</summary>
<div class="pl-12 flex flex-col gap-2 mt-2 relative">
-
<!--div class="bg-white rounded drop-shadow-sm p-4">
-
<pre class="overflow-auto"><code>{{- .Patch -}}</code></pre>
-
</div-->
+
<div>
+
{{ template "fragments/diff" (list $repoName $diff) }}
+
</div>
{{ range .Comments }}
<div id="comment-{{.ID}}" class="bg-white rounded drop-shadow-sm py-2 px-4 relative w-fit">
+11 -1
flake.nix
···
environment.systemPackages = with pkgs; [git];
+
system.activationScripts.gitConfig = ''
+
mkdir -p /home/git/.config/git
+
cat > /home/git/.config/git/config << EOF
+
[user]
+
name = Git User
+
email = git@example.com
+
EOF
+
chown -R git:git /home/git/.config
+
'';
+
users.users.git = {
isNormalUser = true;
home = "/home/git";
···
services.tangled-knotserver = {
enable = true;
server = {
-
secret = "ad7b32ded52fbe96e09f469a288084ee01cd12c971da87a1cbb87ef67081bd87";
+
secret = "6995e040e80e2d593b5e5e9ca611a70140b9ef8044add0a28b48b1ee34aa3e85";
hostname = "localhost:6000";
listenAddr = "0.0.0.0:6000";
};