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 7.2KB

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