~gabrielgio/cerrado

60004cfb97fe0eb64d1c8310e7c9caae96b8adbe — Gabriel A. Giovanini a month ago aa55968
feat: Add diff view

Adds a very simple diff view for a commit
M README.md => README.md +1 -1
@@ 23,7 23,7 @@ To run the project you just need to do a make run.

### TODO

- Add patch to the commit page
- Impove diff display
- Add log pagination
- Fix submodule link on tree view


M pkg/git/git.go => pkg/git/git.go +39 -0
@@ 179,6 179,45 @@ func (g *GitRepository) Branches() ([]*plumbing.Reference, error) {
	return branches, nil
}

func (g *GitRepository) Diff() (string, error) {
	err := g.validateRef()
	if err != nil {
		return "", err
	}

	c, err := g.repository.CommitObject(g.ref)
	if err != nil {
		return "", err
	}

	commitTree, err := c.Tree()
	if err != nil {
		return "", err
	}

	patch := &object.Patch{}
	parentTree := &object.Tree{}
	if c.NumParents() != 0 {
		parent, err := c.Parents().Next()
		if err == nil {
			parentTree, err = parent.Tree()
			if err == nil {
				patch, err = parentTree.Patch(commitTree)
				if err != nil {
					return "", err
				}
			}
		}
	} else {
		patch, err = parentTree.Patch(commitTree)
		if err != nil {
			return "", err
		}
	}

	return patch.String(), nil
}

func (g *GitRepository) Tree(path string) (*object.Tree, error) {
	err := g.validateRef()
	if err != nil {

M pkg/handler/git/handler.go => pkg/handler/git/handler.go +6 -0
@@ 340,11 340,17 @@ func (g *GitHandler) Commit(w http.ResponseWriter, r *http.Request) error {
		return err
	}

	diff, err := g.gitService.Diff(name, ref)
	if err != nil {
		return err
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref,
		GitItemBase: &templates.GitItemCommitPage{
			Commit: commit,
			Diff:   diff,
		},
	}
	templates.WritePageTemplate(w, gitList)

M pkg/service/git.go => pkg/service/git.go +19 -0
@@ 140,6 140,25 @@ func (g *GitService) WriteTarGZip(w io.Writer, name, ref string, prefix string) 
	return nil
}

func (g *GitService) Diff(name, ref string) (string, error) {
	r := g.configRepo.GetByName(name)
	if r == nil {
		return "", ErrRepositoryNotFound
	}

	repo, err := git.OpenRepository(r.Path)
	if err != nil {
		return "", err
	}

	err = repo.SetRef(ref)
	if err != nil {
		return "", err
	}

	return repo.Diff()
}

func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) {
	r := g.configRepo.GetByName(name)
	if r == nil {

M templates/gititemcommit.qtpl => templates/gititemcommit.qtpl +3 -3
@@ 3,6 3,7 @@
{% code
type GitItemCommitPage struct {
    Commit *object.Commit
    Diff string
}
%}



@@ 12,8 13,7 @@ type GitItemCommitPage struct {
<div class="event-list">
  {%= Commit(name, g.Commit, true) %}
</div>

<div class="alert alert-info text-center" role="alert">
    This page is work in progress.
<div class="code-view">
<pre>{%s g.Diff %}</pre>
</div>
{% endfunc %}

M templates/gititemcommit.qtpl.go => templates/gititemcommit.qtpl.go +26 -22
@@ 23,55 23,59 @@ var (
//line gititemcommit.qtpl:4
type GitItemCommitPage struct {
	Commit *object.Commit
	Diff   string
}

//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
func (g *GitItemCommitPage) StreamNav(qw422016 *qt422016.Writer, name, ref string) {
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	StreamGitItemNav(qw422016, name, ref, Log)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
}

//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
func (g *GitItemCommitPage) WriteNav(qq422016 qtio422016.Writer, name, ref string) {
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	qw422016 := qt422016.AcquireWriter(qq422016)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	g.StreamNav(qw422016, name, ref)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	qt422016.ReleaseWriter(qw422016)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
}

//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
func (g *GitItemCommitPage) Nav(name, ref string) string {
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	qb422016 := qt422016.AcquireByteBuffer()
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	g.WriteNav(qb422016, name, ref)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	qs422016 := string(qb422016.B)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	qt422016.ReleaseByteBuffer(qb422016)
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
	return qs422016
//line gititemcommit.qtpl:9
//line gititemcommit.qtpl:10
}

//line gititemcommit.qtpl:11
//line gititemcommit.qtpl:12
func (g *GitItemCommitPage) StreamGitContent(qw422016 *qt422016.Writer, name, ref string) {
//line gititemcommit.qtpl:11
//line gititemcommit.qtpl:12
	qw422016.N().S(`
<div class="event-list">
  `)
//line gititemcommit.qtpl:13
//line gititemcommit.qtpl:14
	StreamCommit(qw422016, name, g.Commit, true)
//line gititemcommit.qtpl:13
//line gititemcommit.qtpl:14
	qw422016.N().S(`
</div>

<div class="alert alert-info text-center" role="alert">
    This page is work in progress.
<div class="code-view">
<pre>`)
//line gititemcommit.qtpl:17
	qw422016.E().S(g.Diff)
//line gititemcommit.qtpl:17
	qw422016.N().S(`</pre>
</div>
`)
//line gititemcommit.qtpl:19