git_diff.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. "bytes"
  8. "fmt"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "strings"
  13. "github.com/Unknwon/com"
  14. "golang.org/x/net/html/charset"
  15. "golang.org/x/text/transform"
  16. "github.com/gogits/git-shell"
  17. "github.com/gogits/gogs/modules/base"
  18. "github.com/gogits/gogs/modules/log"
  19. "github.com/gogits/gogs/modules/process"
  20. )
  21. // Diff line types.
  22. const (
  23. DIFF_LINE_PLAIN = iota + 1
  24. DIFF_LINE_ADD
  25. DIFF_LINE_DEL
  26. DIFF_LINE_SECTION
  27. )
  28. const (
  29. DIFF_FILE_ADD = iota + 1
  30. DIFF_FILE_CHANGE
  31. DIFF_FILE_DEL
  32. DIFF_FILE_RENAME
  33. )
  34. type DiffLine struct {
  35. LeftIdx int
  36. RightIdx int
  37. Type int
  38. Content string
  39. }
  40. func (d DiffLine) GetType() int {
  41. return d.Type
  42. }
  43. type DiffSection struct {
  44. Name string
  45. Lines []*DiffLine
  46. }
  47. type DiffFile struct {
  48. Name string
  49. OldName string
  50. Index int
  51. Addition, Deletion int
  52. Type int
  53. IsCreated bool
  54. IsDeleted bool
  55. IsBin bool
  56. IsRenamed bool
  57. Sections []*DiffSection
  58. }
  59. type Diff struct {
  60. TotalAddition, TotalDeletion int
  61. Files []*DiffFile
  62. }
  63. func (diff *Diff) NumFiles() int {
  64. return len(diff.Files)
  65. }
  66. const DIFF_HEAD = "diff --git "
  67. func ParsePatch(maxlines int, reader io.Reader) (*Diff, error) {
  68. var (
  69. diff = &Diff{Files: make([]*DiffFile, 0)}
  70. curFile *DiffFile
  71. curSection = &DiffSection{
  72. Lines: make([]*DiffLine, 0, 10),
  73. }
  74. leftLine, rightLine int
  75. lineCount int
  76. )
  77. input := bufio.NewReader(reader)
  78. isEOF := false
  79. for {
  80. if isEOF {
  81. break
  82. }
  83. line, err := input.ReadString('\n')
  84. if err != nil {
  85. if err == io.EOF {
  86. isEOF = true
  87. } else {
  88. return nil, fmt.Errorf("ReadString: %v", err)
  89. }
  90. }
  91. if len(line) > 0 && line[len(line)-1] == '\n' {
  92. // Remove line break.
  93. line = line[:len(line)-1]
  94. }
  95. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  96. continue
  97. } else if len(line) == 0 {
  98. continue
  99. }
  100. lineCount++
  101. // Diff data too large, we only show the first about maxlines lines
  102. if lineCount >= maxlines {
  103. log.Warn("Diff data too large")
  104. diff.Files = nil
  105. return diff, nil
  106. }
  107. switch {
  108. case line[0] == ' ':
  109. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  110. leftLine++
  111. rightLine++
  112. curSection.Lines = append(curSection.Lines, diffLine)
  113. continue
  114. case line[0] == '@':
  115. curSection = &DiffSection{}
  116. curFile.Sections = append(curFile.Sections, curSection)
  117. ss := strings.Split(line, "@@")
  118. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  119. curSection.Lines = append(curSection.Lines, diffLine)
  120. // Parse line number.
  121. ranges := strings.Split(ss[1][1:], " ")
  122. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  123. if len(ranges) > 1 {
  124. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  125. } else {
  126. log.Warn("Parse line number failed: %v", line)
  127. rightLine = leftLine
  128. }
  129. continue
  130. case line[0] == '+':
  131. curFile.Addition++
  132. diff.TotalAddition++
  133. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  134. rightLine++
  135. curSection.Lines = append(curSection.Lines, diffLine)
  136. continue
  137. case line[0] == '-':
  138. curFile.Deletion++
  139. diff.TotalDeletion++
  140. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  141. if leftLine > 0 {
  142. leftLine++
  143. }
  144. curSection.Lines = append(curSection.Lines, diffLine)
  145. case strings.HasPrefix(line, "Binary"):
  146. curFile.IsBin = true
  147. continue
  148. }
  149. // Get new file.
  150. if strings.HasPrefix(line, DIFF_HEAD) {
  151. middle := -1
  152. // Note: In case file name is surrounded by double quotes (it happens only in git-shell).
  153. // e.g. diff --git "a/xxx" "b/xxx"
  154. hasQuote := line[len(DIFF_HEAD)] == '"'
  155. if hasQuote {
  156. middle = strings.Index(line, ` "b/`)
  157. } else {
  158. middle = strings.Index(line, " b/")
  159. }
  160. beg := len(DIFF_HEAD)
  161. a := line[beg+2 : middle]
  162. b := line[middle+3:]
  163. if hasQuote {
  164. a = string(git.UnescapeChars([]byte(a[1 : len(a)-1])))
  165. b = string(git.UnescapeChars([]byte(b[1 : len(b)-1])))
  166. }
  167. curFile = &DiffFile{
  168. Name: a,
  169. Index: len(diff.Files) + 1,
  170. Type: DIFF_FILE_CHANGE,
  171. Sections: make([]*DiffSection, 0, 10),
  172. }
  173. diff.Files = append(diff.Files, curFile)
  174. // Check file diff type.
  175. for {
  176. line, err := input.ReadString('\n')
  177. if err != nil {
  178. if err == io.EOF {
  179. isEOF = true
  180. } else {
  181. return nil, fmt.Errorf("ReadString: %v", err)
  182. }
  183. }
  184. switch {
  185. case strings.HasPrefix(line, "new file"):
  186. curFile.Type = DIFF_FILE_ADD
  187. curFile.IsCreated = true
  188. case strings.HasPrefix(line, "deleted"):
  189. curFile.Type = DIFF_FILE_DEL
  190. curFile.IsDeleted = true
  191. case strings.HasPrefix(line, "index"):
  192. curFile.Type = DIFF_FILE_CHANGE
  193. case strings.HasPrefix(line, "similarity index 100%"):
  194. curFile.Type = DIFF_FILE_RENAME
  195. curFile.IsRenamed = true
  196. curFile.OldName = curFile.Name
  197. curFile.Name = b
  198. }
  199. if curFile.Type > 0 {
  200. break
  201. }
  202. }
  203. }
  204. }
  205. // FIXME: detect encoding while parsing.
  206. var buf bytes.Buffer
  207. for _, f := range diff.Files {
  208. buf.Reset()
  209. for _, sec := range f.Sections {
  210. for _, l := range sec.Lines {
  211. buf.WriteString(l.Content)
  212. buf.WriteString("\n")
  213. }
  214. }
  215. charsetLabel := base.DetectEncoding(buf.Bytes())
  216. if charsetLabel != "UTF-8" {
  217. encoding, _ := charset.Lookup(charsetLabel)
  218. if encoding != nil {
  219. d := encoding.NewDecoder()
  220. for _, sec := range f.Sections {
  221. for _, l := range sec.Lines {
  222. if c, _, err := transform.String(d, l.Content); err == nil {
  223. l.Content = c
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. return diff, nil
  231. }
  232. func GetDiffRange(repoPath, beforeCommitID string, afterCommitID string, maxlines int) (*Diff, error) {
  233. repo, err := git.OpenRepository(repoPath)
  234. if err != nil {
  235. return nil, err
  236. }
  237. commit, err := repo.GetCommit(afterCommitID)
  238. if err != nil {
  239. return nil, err
  240. }
  241. var cmd *exec.Cmd
  242. // if "after" commit given
  243. if len(beforeCommitID) == 0 {
  244. // First commit of repository.
  245. if commit.ParentCount() == 0 {
  246. cmd = exec.Command("git", "show", afterCommitID)
  247. } else {
  248. c, _ := commit.Parent(0)
  249. cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
  250. }
  251. } else {
  252. cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
  253. }
  254. cmd.Dir = repoPath
  255. cmd.Stderr = os.Stderr
  256. stdout, err := cmd.StdoutPipe()
  257. if err != nil {
  258. return nil, fmt.Errorf("StdoutPipe: %v", err)
  259. }
  260. if err = cmd.Start(); err != nil {
  261. return nil, fmt.Errorf("Start: %v", err)
  262. }
  263. pid := process.Add(fmt.Sprintf("GetDiffRange (%s)", repoPath), cmd)
  264. defer process.Remove(pid)
  265. diff, err := ParsePatch(maxlines, stdout)
  266. if err != nil {
  267. return nil, fmt.Errorf("ParsePatch: %v", err)
  268. }
  269. if err = cmd.Wait(); err != nil {
  270. return nil, fmt.Errorf("Wait: %v", err)
  271. }
  272. return diff, nil
  273. }
  274. func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) {
  275. return GetDiffRange(repoPath, "", commitId, maxlines)
  276. }