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