Skip to content

Commit

Permalink
feat(ui): add branch/tag commit date and hash
Browse files Browse the repository at this point in the history
Fixes: #382
  • Loading branch information
aymanbagabas committed Oct 23, 2023
1 parent 7ba5a57 commit fd02409
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 97 deletions.
22 changes: 3 additions & 19 deletions git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,11 @@ import (
"github.com/gogs/git-module"
)

// ZeroHash is the zero hash.
var ZeroHash Hash = git.EmptyID

// Hash represents a git hash.
type Hash string

// String returns the string representation of a hash as a string.
func (h Hash) String() string {
return string(h)
}

// SHA1 represents the hash as a SHA1.
func (h Hash) SHA1() *git.SHA1 {
return git.MustIDFromString(h.String())
}
// ZeroID is the zero hash.
const ZeroID = git.EmptyID

// Commit is a wrapper around git.Commit with helper methods.
type Commit struct {
*git.Commit
Hash Hash
}
type Commit = git.Commit

// Commits is a list of commits.
type Commits []*Commit
Expand Down
8 changes: 4 additions & 4 deletions git/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ func (f *DiffFileChange) Mode() git.EntryMode {

// Files returns the diff files.
func (f *DiffFile) Files() (from *DiffFileChange, to *DiffFileChange) {
if f.OldIndex != ZeroHash.String() {
if f.OldIndex != ZeroID {
from = &DiffFileChange{
hash: f.OldIndex,
name: f.OldName(),
mode: f.OldMode(),
}
}
if f.Index != ZeroHash.String() {
if f.Index != ZeroID {
to = &DiffFileChange{
hash: f.Index,
name: f.Name,
Expand Down Expand Up @@ -298,14 +298,14 @@ func writeFilePatchHeader(sb *strings.Builder, filePatch *DiffFile) {
lines = append(lines,
fmt.Sprintf("diff --git %s %s", srcPrefix+to.Name(), dstPrefix+to.Name()),
fmt.Sprintf("new file mode %o", to.Mode()),
fmt.Sprintf("index %s..%s", ZeroHash, to.Hash()),
fmt.Sprintf("index %s..%s", ZeroID, to.Hash()),
)
lines = appendPathLines(lines, "/dev/null", dstPrefix+to.Name(), isBinary)
case to == nil:
lines = append(lines,
fmt.Sprintf("diff --git %s %s", srcPrefix+from.Name(), dstPrefix+from.Name()),
fmt.Sprintf("deleted file mode %o", from.Mode()),
fmt.Sprintf("index %s..%s", from.Hash(), ZeroHash),
fmt.Sprintf("index %s..%s", from.Hash(), ZeroID),
)
lines = appendPathLines(lines, srcPrefix+from.Name(), "/dev/null", isBinary)
}
Expand Down
28 changes: 1 addition & 27 deletions git/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,20 @@ const (
// Reference is a wrapper around git.Reference with helper methods.
type Reference struct {
*git.Reference
Hash Hash
path string // repo path
}

// ReferenceName is a Refspec wrapper.
type ReferenceName string

// NewReference creates a new reference.
func NewReference(rp, refspec string) *Reference {
return &Reference{
Reference: &git.Reference{
Refspec: refspec,
},
path: rp,
}
}

// String returns the reference name i.e. refs/heads/master.
func (r ReferenceName) String() string {
return string(r)
}

// Short returns the short name of the reference i.e. master.
func (r ReferenceName) Short() string {
s := strings.Split(r.String(), "/")
if len(s) > 0 {
return s[len(s)-1]
}
return r.String()
return git.RefShortName(string(r))
}

// Name returns the reference name i.e. refs/heads/master.
Expand All @@ -63,14 +48,3 @@ func (r *Reference) IsBranch() bool {
func (r *Reference) IsTag() bool {
return strings.HasPrefix(r.Refspec, git.RefsTags)
}

// TargetHash returns the hash of the reference target.
func (r *Reference) TargetHash() Hash {
if r.IsTag() {
id, err := git.ShowRefVerify(r.path, r.Refspec)
if err == nil {
return Hash(id)
}
}
return r.Hash
}
11 changes: 3 additions & 8 deletions git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (r *Repository) HEAD() (*Reference, error) {
ID: hash,
Refspec: rn,
},
Hash: Hash(hash),
path: r.Path,
}, nil
}
Expand All @@ -92,7 +91,6 @@ func (r *Repository) References() ([]*Reference, error) {
for _, ref := range refs {
rrefs = append(rrefs, &Reference{
Reference: ref,
Hash: Hash(ref.ID),
path: r.Path,
})
}
Expand Down Expand Up @@ -121,7 +119,7 @@ func (r *Repository) Tree(ref *Reference) (*Tree, error) {
}
ref = rref
}
return r.LsTree(ref.Hash.String())
return r.LsTree(ref.ID)
}

// TreePath returns the tree for the given path.
Expand All @@ -142,7 +140,7 @@ func (r *Repository) TreePath(ref *Reference, path string) (*Tree, error) {

// Diff returns the diff for the given commit.
func (r *Repository) Diff(commit *Commit) (*Diff, error) {
ddiff, err := r.Repository.Diff(commit.Hash.String(), DiffMaxFiles, DiffMaxFileLines, DiffMaxLineChars, git.DiffOptions{
ddiff, err := r.Repository.Diff(commit.ID.String(), DiffMaxFiles, DiffMaxFileLines, DiffMaxLineChars, git.DiffOptions{
CommandOptions: git.CommandOptions{
Envs: []string{"GIT_CONFIG_GLOBAL=/dev/null"},
},
Expand Down Expand Up @@ -192,10 +190,7 @@ func (r *Repository) CommitsByPage(ref *Reference, page, size int) (Commits, err
}
commits := make(Commits, len(cs))
for i, c := range cs {
commits[i] = &Commit{
Commit: c,
Hash: Hash(c.ID.String()),
}
commits[i] = c
}
return commits, nil
}
Expand Down
5 changes: 5 additions & 0 deletions git/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package git

import "github.com/gogs/git-module"

type Tag = git.Tag
2 changes: 1 addition & 1 deletion server/ssh/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func blobCommand() *cobra.Command {
if err != nil {
return err
}
ref = head.Hash.String()
ref = head.ID
}

tree, err := r.LsTree(ref)
Expand Down
7 changes: 1 addition & 6 deletions server/ssh/cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ func commitCommand() *cobra.Command {
return err
}

rawCommit, err := r.CommitByRevision(commitSHA)
commit, err := r.CommitByRevision(commitSHA)
if err != nil {
return err
}

commit := &git.Commit{
Commit: rawCommit,
Hash: git.Hash(commitSHA),
}

patch, err := r.Patch(commit)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion server/ssh/cmd/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func treeCommand() *cobra.Command {
return err
}

ref = head.Hash.String()
ref = head.ID
}

tree, err := r.LsTree(ref)
Expand Down
19 changes: 15 additions & 4 deletions server/ui/pages/repo/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/soft-serve/git"
ggit "github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/server/proto"
"github.com/charmbracelet/soft-serve/server/ui/common"
"github.com/charmbracelet/soft-serve/server/ui/components/selector"
)

// RefMsg is a message that contains a git.Reference.
type RefMsg *ggit.Reference
type RefMsg *git.Reference

// RefItemsMsg is a message that contains a list of RefItem.
type RefItemsMsg struct {
Expand Down Expand Up @@ -215,7 +214,19 @@ func (r *Refs) updateItemsCmd() tea.Msg {
}
for _, ref := range refs {
if strings.HasPrefix(ref.Name().String(), r.refPrefix) {
its = append(its, RefItem{Reference: ref})
refItem := RefItem{
Reference: ref,
}

if ref.IsTag() {
refItem.Tag, _ = rr.Tag(ref.Name().Short())
if refItem.Tag != nil {
refItem.Commit, _ = refItem.Tag.Commit()
}
} else {
refItem.Commit, _ = rr.CatFileCommit(ref.ID)
}
its = append(its, refItem)
}
}
sort.Sort(its)
Expand All @@ -238,7 +249,7 @@ func (r *Refs) setItems(items []selector.IdentifiableItem) tea.Cmd {
}
}

func switchRefCmd(ref *ggit.Reference) tea.Cmd {
func switchRefCmd(ref *git.Reference) tea.Cmd {
return func() tea.Msg {
return RefMsg(ref)
}
Expand Down
Loading

0 comments on commit fd02409

Please sign in to comment.