1package git
2
3import (
4 "path/filepath"
5
6 "github.com/gogs/git-module"
7)
8
9var (
10 // DiffMaxFile is the maximum number of files to show in a diff.
11 DiffMaxFiles = 1000
12 // DiffMaxFileLines is the maximum number of lines to show in a file diff.
13 DiffMaxFileLines = 1000
14 // DiffMaxLineChars is the maximum number of characters to show in a line diff.
15 DiffMaxLineChars = 1000
16)
17
18// Repository is a wrapper around git.Repository with helper methods.
19type Repository struct {
20 *git.Repository
21 Path string
22}
23
24// Clone clones a repository.
25func Clone(src, dst string, opts ...git.CloneOptions) error {
26 return git.Clone(src, dst, opts...)
27}
28
29// Init initializes and opens a new git repository.
30func Init(path string, bare bool) (*Repository, error) {
31 err := git.Init(path, git.InitOptions{Bare: bare})
32 if err != nil {
33 return nil, err
34 }
35 return Open(path)
36}
37
38// Open opens a git repository at the given path.
39func Open(path string) (*Repository, error) {
40 repo, err := git.Open(path)
41 if err != nil {
42 return nil, err
43 }
44 return &Repository{
45 Repository: repo,
46 Path: path,
47 }, nil
48}
49
50// Name returns the name of the repository.
51func (r *Repository) Name() string {
52 return filepath.Base(r.Path)
53}
54
55// HEAD returns the HEAD reference for a repository.
56func (r *Repository) HEAD() (*Reference, error) {
57 rn, err := r.SymbolicRef()
58 if err != nil {
59 return nil, err
60 }
61 hash, err := r.ShowRefVerify(rn)
62 if err != nil {
63 return nil, err
64 }
65 return &Reference{
66 Reference: &git.Reference{
67 ID: hash,
68 Refspec: rn,
69 },
70 Hash: Hash(hash),
71 path: r.Path,
72 }, nil
73}
74
75// References returns the references for a repository.
76func (r *Repository) References() ([]*Reference, error) {
77 refs, err := r.ShowRef()
78 if err != nil {
79 return nil, err
80 }
81 rrefs := make([]*Reference, 0, len(refs))
82 for _, ref := range refs {
83 rrefs = append(rrefs, &Reference{
84 Reference: ref,
85 Hash: Hash(ref.ID),
86 path: r.Path,
87 })
88 }
89 return rrefs, nil
90}
91
92// Tree returns the tree for the given reference.
93func (r *Repository) Tree(ref *Reference) (*Tree, error) {
94 if ref == nil {
95 rref, err := r.HEAD()
96 if err != nil {
97 return nil, err
98 }
99 ref = rref
100 }
101 tree, err := r.LsTree(ref.Hash.String())
102 if err != nil {
103 return nil, err
104 }
105 return &Tree{
106 Tree: tree,
107 Path: "",
108 }, nil
109}
110
111// TreePath returns the tree for the given path.
112func (r *Repository) TreePath(ref *Reference, path string) (*Tree, error) {
113 path = filepath.Clean(path)
114 if path == "." {
115 path = ""
116 }
117 if path == "" {
118 return r.Tree(ref)
119 }
120 t, err := r.Tree(ref)
121 if err != nil {
122 return nil, err
123 }
124 return t.SubTree(path)
125}
126
127// Diff returns the diff for the given commit.
128func (r *Repository) Diff(commit *Commit) (*Diff, error) {
129 ddiff, err := r.Repository.Diff(commit.Hash.String(), DiffMaxFiles, DiffMaxFileLines, DiffMaxLineChars)
130 if err != nil {
131 return nil, err
132 }
133 files := make([]*DiffFile, 0, len(ddiff.Files))
134 for _, df := range ddiff.Files {
135 sections := make([]*DiffSection, 0, len(df.Sections))
136 for _, ds := range df.Sections {
137 sections = append(sections, &DiffSection{
138 DiffSection: ds,
139 })
140 }
141 files = append(files, &DiffFile{
142 DiffFile: df,
143 Sections: sections,
144 })
145 }
146 diff := &Diff{
147 Diff: ddiff,
148 Files: files,
149 }
150 return diff, nil
151}
152
153// Patch returns the patch for the given reference.
154func (r *Repository) Patch(commit *Commit) (string, error) {
155 diff, err := r.Diff(commit)
156 if err != nil {
157 return "", err
158 }
159 return diff.Patch(), err
160}
161
162// CountCommits returns the number of commits in the repository.
163func (r *Repository) CountCommits(ref *Reference) (int64, error) {
164 return r.Repository.RevListCount([]string{ref.Name().String()})
165}
166
167// CommitsByPage returns the commits for a given page and size.
168func (r *Repository) CommitsByPage(ref *Reference, page, size int) (Commits, error) {
169 cs, err := r.Repository.CommitsByPage(ref.Name().String(), page, size)
170 if err != nil {
171 return nil, err
172 }
173 commits := make(Commits, len(cs))
174 for i, c := range cs {
175 commits[i] = &Commit{
176 Commit: c,
177 Hash: Hash(c.ID.String()),
178 }
179 }
180 return commits, nil
181}
182
183// UpdateServerInfo updates the repository server info.
184func (r *Repository) UpdateServerInfo() error {
185 cmd := git.NewCommand("update-server-info")
186 _, err := cmd.RunInDir(r.Path)
187 return err
188}