You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

git_diff.go 4.3KB

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