Home

ugit @main - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / git / git.go
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package git

import (
	"errors"
	"io/fs"
	"os"
	"path/filepath"
	"sort"

	"github.com/dustin/go-humanize"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
)

// EnsureRepo ensures that the repo exists in the given directory
func EnsureRepo(dir string, repo string) error {
	exists, err := PathExists(dir)
	if err != nil {
		return err
	}
	if !exists {
		err = os.MkdirAll(dir, os.ModeDir|os.FileMode(0o700))
		if err != nil {
			return err
		}
	}
	rp := filepath.Join(dir, repo)
	exists, err = PathExists(rp)
	if err != nil {
		return err
	}
	if !exists {
		_, err := git.PlainInit(rp, true)
		if err != nil {
			return err
		}
	}
	return nil
}

// PathExists checks if a path exists and returns true if it does
func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if errors.Is(err, fs.ErrNotExist) {
		return false, nil
	}
	return true, err
}

// Tree returns the git tree at a given ref/rev
func (r Repo) Tree(ref string) (*object.Tree, error) {
	g, err := r.Git()
	if err != nil {
		return nil, err
	}

	hash, err := g.ResolveRevision(plumbing.Revision(ref))
	if err != nil {
		return nil, err
	}

	c, err := g.CommitObject(*hash)
	if err != nil {
		return nil, err
	}

	return c.Tree()
}

// FileInfo is the information for a file in a tree
type FileInfo struct {
	Path  string
	IsDir bool
	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 {
		return nil, err
	}
	if path != "" {
		t, err = t.Tree(path)
		if err != nil {
			return nil, err
		}
	}

	fis := make([]FileInfo, 0)
	for _, entry := range t.Entries {
		fm, err := entry.Mode.ToOSFileMode()
		if err != nil {
			return nil, err
		}
		size, err := t.Size(entry.Name)
		if err != nil {
			return nil, err
		}
		fis = append(fis, FileInfo{
			Path:  filepath.Join(path, entry.Name),
			IsDir: fm.IsDir(),
			Mode:  fm.String(),
			Size:  humanize.Bytes(uint64(size)),
		})
	}
	sort.Slice(fis, func(i, j int) bool {
		fi1 := fis[i]
		fi2 := fis[j]
		return (fi1.IsDir && !fi2.IsDir) || fi1.Name() < fi2.Name()
	})

	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 {
		return "", err
	}

	f, err := t.File(file)
	if err != nil {
		return "", err
	}

	content, err := f.Contents()
	if err != nil {
		return "", err
	}

	return content, nil
}