git.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "bufio"
  7. "container/list"
  8. "fmt"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "strings"
  14. "github.com/gogits/git"
  15. "github.com/gogits/gogs/modules/base"
  16. )
  17. // RepoFile represents a file object in git repository.
  18. type RepoFile struct {
  19. *git.TreeEntry
  20. Path string
  21. Size int64
  22. Repo *git.Repository
  23. Commit *git.Commit
  24. }
  25. // LookupBlob returns the content of an object.
  26. func (file *RepoFile) LookupBlob() (*git.Blob, error) {
  27. if file.Repo == nil {
  28. return nil, ErrRepoFileNotLoaded
  29. }
  30. return file.Repo.LookupBlob(file.Id)
  31. }
  32. // GetBranches returns all branches of given repository.
  33. func GetBranches(userName, repoName string) ([]string, error) {
  34. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  35. if err != nil {
  36. return nil, err
  37. }
  38. refs, err := repo.AllReferences()
  39. if err != nil {
  40. return nil, err
  41. }
  42. brs := make([]string, len(refs))
  43. for i, ref := range refs {
  44. brs[i] = ref.BranchName()
  45. }
  46. return brs, nil
  47. }
  48. // GetTags returns all tags of given repository.
  49. func GetTags(userName, repoName string) ([]string, error) {
  50. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  51. if err != nil {
  52. return nil, err
  53. }
  54. refs, err := repo.AllTags()
  55. if err != nil {
  56. return nil, err
  57. }
  58. tags := make([]string, len(refs))
  59. for i, ref := range refs {
  60. tags[i] = ref.Name
  61. }
  62. return tags, nil
  63. }
  64. func IsBranchExist(userName, repoName, branchName string) bool {
  65. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  66. if err != nil {
  67. return false
  68. }
  69. return repo.IsBranchExist(branchName)
  70. }
  71. func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*RepoFile, error) {
  72. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  73. if err != nil {
  74. return nil, err
  75. }
  76. commit, err := repo.GetCommitOfBranch(branchName)
  77. if err != nil {
  78. commit, err = repo.GetCommit(commitId)
  79. if err != nil {
  80. return nil, err
  81. }
  82. }
  83. parts := strings.Split(path.Clean(rpath), "/")
  84. var entry *git.TreeEntry
  85. tree := commit.Tree
  86. for i, part := range parts {
  87. if i == len(parts)-1 {
  88. entry = tree.EntryByName(part)
  89. if entry == nil {
  90. return nil, ErrRepoFileNotExist
  91. }
  92. } else {
  93. tree, err = repo.SubTree(tree, part)
  94. if err != nil {
  95. return nil, err
  96. }
  97. }
  98. }
  99. size, err := repo.ObjectSize(entry.Id)
  100. if err != nil {
  101. return nil, err
  102. }
  103. repoFile := &RepoFile{
  104. entry,
  105. rpath,
  106. size,
  107. repo,
  108. commit,
  109. }
  110. return repoFile, nil
  111. }
  112. // GetReposFiles returns a list of file object in given directory of repository.
  113. // func GetReposFilesOfBranch(userName, repoName, branchName, rpath string) ([]*RepoFile, error) {
  114. // return getReposFiles(userName, repoName, commitId, rpath)
  115. // }
  116. // GetReposFiles returns a list of file object in given directory of repository.
  117. func GetReposFiles(userName, repoName, commitId, rpath string) ([]*RepoFile, error) {
  118. return getReposFiles(userName, repoName, commitId, rpath)
  119. }
  120. func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFile, error) {
  121. repopath := RepoPath(userName, repoName)
  122. repo, err := git.OpenRepository(repopath)
  123. if err != nil {
  124. return nil, err
  125. }
  126. commit, err := repo.GetCommit(commitId)
  127. if err != nil {
  128. return nil, err
  129. }
  130. var repodirs []*RepoFile
  131. var repofiles []*RepoFile
  132. commit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
  133. if dirname == rpath {
  134. // TODO: size get method shoule be improved
  135. size, err := repo.ObjectSize(entry.Id)
  136. if err != nil {
  137. return 0
  138. }
  139. cmd := exec.Command("git", "log", "-1", "--pretty=format:%H", commitId, "--", path.Join(dirname, entry.Name))
  140. cmd.Dir = repopath
  141. out, err := cmd.Output()
  142. if err != nil {
  143. return 0
  144. }
  145. filecm, err := repo.GetCommit(string(out))
  146. if err != nil {
  147. return 0
  148. }
  149. rp := &RepoFile{
  150. entry,
  151. path.Join(dirname, entry.Name),
  152. size,
  153. repo,
  154. filecm,
  155. }
  156. if entry.IsFile() {
  157. repofiles = append(repofiles, rp)
  158. } else if entry.IsDir() {
  159. repodirs = append(repodirs, rp)
  160. }
  161. }
  162. return 0
  163. })
  164. return append(repodirs, repofiles...), nil
  165. }
  166. func GetCommit(userName, repoName, commitId string) (*git.Commit, error) {
  167. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  168. if err != nil {
  169. return nil, err
  170. }
  171. return repo.GetCommit(commitId)
  172. }
  173. // GetCommitsByBranch returns all commits of given branch of repository.
  174. func GetCommitsByBranch(userName, repoName, branchName string) (*list.List, error) {
  175. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  176. if err != nil {
  177. return nil, err
  178. }
  179. r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchName))
  180. if err != nil {
  181. return nil, err
  182. }
  183. return r.AllCommits()
  184. }
  185. // GetCommitsByCommitId returns all commits of given commitId of repository.
  186. func GetCommitsByCommitId(userName, repoName, commitId string) (*list.List, error) {
  187. repo, err := git.OpenRepository(RepoPath(userName, repoName))
  188. if err != nil {
  189. return nil, err
  190. }
  191. oid, err := git.NewOidFromString(commitId)
  192. if err != nil {
  193. return nil, err
  194. }
  195. return repo.CommitsBefore(oid)
  196. }
  197. // Diff line types.
  198. const (
  199. DIFF_LINE_PLAIN = iota + 1
  200. DIFF_LINE_ADD
  201. DIFF_LINE_DEL
  202. DIFF_LINE_SECTION
  203. )
  204. const (
  205. DIFF_FILE_ADD = iota + 1
  206. DIFF_FILE_CHANGE
  207. DIFF_FILE_DEL
  208. )
  209. type DiffLine struct {
  210. LeftIdx int
  211. RightIdx int
  212. Type int
  213. Content string
  214. }
  215. func (d DiffLine) GetType() int {
  216. return d.Type
  217. }
  218. type DiffSection struct {
  219. Name string
  220. Lines []*DiffLine
  221. }
  222. type DiffFile struct {
  223. Name string
  224. Addition, Deletion int
  225. Type int
  226. Sections []*DiffSection
  227. }
  228. type Diff struct {
  229. TotalAddition, TotalDeletion int
  230. Files []*DiffFile
  231. }
  232. func (diff *Diff) NumFiles() int {
  233. return len(diff.Files)
  234. }
  235. const DIFF_HEAD = "diff --git "
  236. func ParsePatch(reader io.Reader) (*Diff, error) {
  237. scanner := bufio.NewScanner(reader)
  238. var (
  239. curFile *DiffFile
  240. curSection = &DiffSection{
  241. Lines: make([]*DiffLine, 0, 10),
  242. }
  243. leftLine, rightLine int
  244. )
  245. diff := &Diff{Files: make([]*DiffFile, 0)}
  246. var i int
  247. for scanner.Scan() {
  248. line := scanner.Text()
  249. // fmt.Println(i, line)
  250. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  251. continue
  252. }
  253. i = i + 1
  254. if line == "" {
  255. continue
  256. }
  257. if line[0] == ' ' {
  258. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  259. leftLine++
  260. rightLine++
  261. curSection.Lines = append(curSection.Lines, diffLine)
  262. continue
  263. } else if line[0] == '@' {
  264. curSection = &DiffSection{}
  265. curFile.Sections = append(curFile.Sections, curSection)
  266. ss := strings.Split(line, "@@")
  267. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  268. curSection.Lines = append(curSection.Lines, diffLine)
  269. // Parse line number.
  270. ranges := strings.Split(ss[len(ss)-2][1:], " ")
  271. leftLine, _ = base.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  272. rightLine, _ = base.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  273. continue
  274. } else if line[0] == '+' {
  275. curFile.Addition++
  276. diff.TotalAddition++
  277. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  278. rightLine++
  279. curSection.Lines = append(curSection.Lines, diffLine)
  280. continue
  281. } else if line[0] == '-' {
  282. curFile.Deletion++
  283. diff.TotalDeletion++
  284. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  285. if leftLine > 0 {
  286. leftLine++
  287. }
  288. curSection.Lines = append(curSection.Lines, diffLine)
  289. continue
  290. }
  291. // Get new file.
  292. if strings.HasPrefix(line, DIFF_HEAD) {
  293. fs := strings.Split(line[len(DIFF_HEAD):], " ")
  294. a := fs[0]
  295. curFile = &DiffFile{
  296. Name: a[strings.Index(a, "/")+1:],
  297. Type: DIFF_FILE_CHANGE,
  298. Sections: make([]*DiffSection, 0, 10),
  299. }
  300. diff.Files = append(diff.Files, curFile)
  301. // Check file diff type.
  302. for scanner.Scan() {
  303. switch {
  304. case strings.HasPrefix(scanner.Text(), "new file"):
  305. curFile.Type = DIFF_FILE_ADD
  306. case strings.HasPrefix(scanner.Text(), "deleted"):
  307. curFile.Type = DIFF_FILE_DEL
  308. case strings.HasPrefix(scanner.Text(), "index"):
  309. curFile.Type = DIFF_FILE_CHANGE
  310. }
  311. if curFile.Type > 0 {
  312. break
  313. }
  314. }
  315. }
  316. }
  317. return diff, nil
  318. }
  319. func GetDiff(repoPath, commitid string) (*Diff, error) {
  320. repo, err := git.OpenRepository(repoPath)
  321. if err != nil {
  322. return nil, err
  323. }
  324. commit, err := repo.GetCommit(commitid)
  325. if err != nil {
  326. return nil, err
  327. }
  328. // First commit of repository.
  329. if commit.ParentCount() == 0 {
  330. rd, wr := io.Pipe()
  331. go func() {
  332. cmd := exec.Command("git", "show", commitid)
  333. cmd.Dir = repoPath
  334. cmd.Stdout = wr
  335. cmd.Stdin = os.Stdin
  336. cmd.Stderr = os.Stderr
  337. cmd.Run()
  338. wr.Close()
  339. }()
  340. defer rd.Close()
  341. return ParsePatch(rd)
  342. }
  343. rd, wr := io.Pipe()
  344. go func() {
  345. cmd := exec.Command("git", "diff", commit.Parent(0).Oid.String(), commitid)
  346. cmd.Dir = repoPath
  347. cmd.Stdout = wr
  348. cmd.Stdin = os.Stdin
  349. cmd.Stderr = os.Stderr
  350. cmd.Run()
  351. wr.Close()
  352. }()
  353. defer rd.Close()
  354. return ParsePatch(rd)
  355. }