git_diff.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "io"
  8. "os"
  9. "os/exec"
  10. "strings"
  11. "github.com/gogits/git"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. )
  15. // Diff line types.
  16. const (
  17. DIFF_LINE_PLAIN = iota + 1
  18. DIFF_LINE_ADD
  19. DIFF_LINE_DEL
  20. DIFF_LINE_SECTION
  21. )
  22. const (
  23. DIFF_FILE_ADD = iota + 1
  24. DIFF_FILE_CHANGE
  25. DIFF_FILE_DEL
  26. )
  27. type DiffLine struct {
  28. LeftIdx int
  29. RightIdx int
  30. Type int
  31. Content string
  32. }
  33. func (d DiffLine) GetType() int {
  34. return d.Type
  35. }
  36. type DiffSection struct {
  37. Name string
  38. Lines []*DiffLine
  39. }
  40. type DiffFile struct {
  41. Name string
  42. Addition, Deletion int
  43. Type int
  44. IsBin bool
  45. Sections []*DiffSection
  46. }
  47. type Diff struct {
  48. TotalAddition, TotalDeletion int
  49. Files []*DiffFile
  50. }
  51. func (diff *Diff) NumFiles() int {
  52. return len(diff.Files)
  53. }
  54. const DIFF_HEAD = "diff --git "
  55. func ParsePatch(reader io.Reader) (*Diff, error) {
  56. scanner := bufio.NewScanner(reader)
  57. var (
  58. curFile *DiffFile
  59. curSection = &DiffSection{
  60. Lines: make([]*DiffLine, 0, 10),
  61. }
  62. leftLine, rightLine int
  63. )
  64. diff := &Diff{Files: make([]*DiffFile, 0)}
  65. var i int
  66. for scanner.Scan() {
  67. line := scanner.Text()
  68. // fmt.Println(i, line)
  69. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  70. continue
  71. }
  72. i = i + 1
  73. // Diff data too large.
  74. if i == 5000 {
  75. log.Warn("Diff data too large")
  76. return &Diff{}, nil
  77. }
  78. if line == "" {
  79. continue
  80. }
  81. switch {
  82. case line[0] == ' ':
  83. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  84. leftLine++
  85. rightLine++
  86. curSection.Lines = append(curSection.Lines, diffLine)
  87. continue
  88. case line[0] == '@':
  89. curSection = &DiffSection{}
  90. curFile.Sections = append(curFile.Sections, curSection)
  91. ss := strings.Split(line, "@@")
  92. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  93. curSection.Lines = append(curSection.Lines, diffLine)
  94. // Parse line number.
  95. ranges := strings.Split(ss[len(ss)-2][1:], " ")
  96. leftLine, _ = base.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  97. rightLine, _ = base.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  98. continue
  99. case line[0] == '+':
  100. curFile.Addition++
  101. diff.TotalAddition++
  102. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  103. rightLine++
  104. curSection.Lines = append(curSection.Lines, diffLine)
  105. continue
  106. case line[0] == '-':
  107. curFile.Deletion++
  108. diff.TotalDeletion++
  109. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  110. if leftLine > 0 {
  111. leftLine++
  112. }
  113. curSection.Lines = append(curSection.Lines, diffLine)
  114. case strings.HasPrefix(line, "Binary"):
  115. curFile.IsBin = true
  116. continue
  117. }
  118. // Get new file.
  119. if strings.HasPrefix(line, DIFF_HEAD) {
  120. fs := strings.Split(line[len(DIFF_HEAD):], " ")
  121. a := fs[0]
  122. curFile = &DiffFile{
  123. Name: a[strings.Index(a, "/")+1:],
  124. Type: DIFF_FILE_CHANGE,
  125. Sections: make([]*DiffSection, 0, 10),
  126. }
  127. diff.Files = append(diff.Files, curFile)
  128. // Check file diff type.
  129. for scanner.Scan() {
  130. switch {
  131. case strings.HasPrefix(scanner.Text(), "new file"):
  132. curFile.Type = DIFF_FILE_ADD
  133. case strings.HasPrefix(scanner.Text(), "deleted"):
  134. curFile.Type = DIFF_FILE_DEL
  135. case strings.HasPrefix(scanner.Text(), "index"):
  136. curFile.Type = DIFF_FILE_CHANGE
  137. }
  138. if curFile.Type > 0 {
  139. break
  140. }
  141. }
  142. }
  143. }
  144. return diff, nil
  145. }
  146. func GetDiff(repoPath, commitid string) (*Diff, error) {
  147. repo, err := git.OpenRepository(repoPath)
  148. if err != nil {
  149. return nil, err
  150. }
  151. commit, err := repo.GetCommit(commitid)
  152. if err != nil {
  153. return nil, err
  154. }
  155. // First commit of repository.
  156. if commit.ParentCount() == 0 {
  157. rd, wr := io.Pipe()
  158. go func() {
  159. cmd := exec.Command("git", "show", commitid)
  160. cmd.Dir = repoPath
  161. cmd.Stdout = wr
  162. cmd.Stdin = os.Stdin
  163. cmd.Stderr = os.Stderr
  164. cmd.Run()
  165. wr.Close()
  166. }()
  167. defer rd.Close()
  168. return ParsePatch(rd)
  169. }
  170. rd, wr := io.Pipe()
  171. go func() {
  172. c, _ := commit.Parent(0)
  173. cmd := exec.Command("git", "diff", c.Id.String(), commitid)
  174. cmd.Dir = repoPath
  175. cmd.Stdout = wr
  176. cmd.Stdin = os.Stdin
  177. cmd.Stderr = os.Stderr
  178. cmd.Run()
  179. wr.Close()
  180. }()
  181. defer rd.Close()
  182. return ParsePatch(rd)
  183. }