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.

573 lines
14 KiB

10 years ago
9 years ago
10 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
9 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "html"
  10. "html/template"
  11. "io"
  12. "io/ioutil"
  13. "os"
  14. "os/exec"
  15. "strconv"
  16. "strings"
  17. "code.gitea.io/git"
  18. "code.gitea.io/gitea/modules/base"
  19. "code.gitea.io/gitea/modules/highlight"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/process"
  22. "code.gitea.io/gitea/modules/setting"
  23. "github.com/Unknwon/com"
  24. "github.com/sergi/go-diff/diffmatchpatch"
  25. "golang.org/x/net/html/charset"
  26. "golang.org/x/text/transform"
  27. )
  28. // DiffLineType represents the type of a DiffLine.
  29. type DiffLineType uint8
  30. // DiffLineType possible values.
  31. const (
  32. DiffLinePlain DiffLineType = iota + 1
  33. DiffLineAdd
  34. DiffLineDel
  35. DiffLineSection
  36. )
  37. // DiffFileType represents the type of a DiffFile.
  38. type DiffFileType uint8
  39. // DiffFileType possible values.
  40. const (
  41. DiffFileAdd DiffFileType = iota + 1
  42. DiffFileChange
  43. DiffFileDel
  44. DiffFileRename
  45. )
  46. // DiffLine represents a line difference in a DiffSection.
  47. type DiffLine struct {
  48. LeftIdx int
  49. RightIdx int
  50. Type DiffLineType
  51. Content string
  52. }
  53. // GetType returns the type of a DiffLine.
  54. func (d *DiffLine) GetType() int {
  55. return int(d.Type)
  56. }
  57. // DiffSection represents a section of a DiffFile.
  58. type DiffSection struct {
  59. Name string
  60. Lines []*DiffLine
  61. }
  62. var (
  63. addedCodePrefix = []byte("<span class=\"added-code\">")
  64. removedCodePrefix = []byte("<span class=\"removed-code\">")
  65. codeTagSuffix = []byte("</span>")
  66. )
  67. func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
  68. buf := bytes.NewBuffer(nil)
  69. // Reproduce signs which are cut for inline diff before.
  70. switch lineType {
  71. case DiffLineAdd:
  72. buf.WriteByte('+')
  73. case DiffLineDel:
  74. buf.WriteByte('-')
  75. }
  76. for i := range diffs {
  77. switch {
  78. case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd:
  79. buf.Write(addedCodePrefix)
  80. buf.WriteString(html.EscapeString(diffs[i].Text))
  81. buf.Write(codeTagSuffix)
  82. case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
  83. buf.Write(removedCodePrefix)
  84. buf.WriteString(html.EscapeString(diffs[i].Text))
  85. buf.Write(codeTagSuffix)
  86. case diffs[i].Type == diffmatchpatch.DiffEqual:
  87. buf.WriteString(html.EscapeString(diffs[i].Text))
  88. }
  89. }
  90. return template.HTML(buf.Bytes())
  91. }
  92. // GetLine gets a specific line by type (add or del) and file line number
  93. func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
  94. var (
  95. difference = 0
  96. addCount = 0
  97. delCount = 0
  98. matchDiffLine *DiffLine
  99. )
  100. LOOP:
  101. for _, diffLine := range diffSection.Lines {
  102. switch diffLine.Type {
  103. case DiffLineAdd:
  104. addCount++
  105. case DiffLineDel:
  106. delCount++
  107. default:
  108. if matchDiffLine != nil {
  109. break LOOP
  110. }
  111. difference = diffLine.RightIdx - diffLine.LeftIdx
  112. addCount = 0
  113. delCount = 0
  114. }
  115. switch lineType {
  116. case DiffLineDel:
  117. if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
  118. matchDiffLine = diffLine
  119. }
  120. case DiffLineAdd:
  121. if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
  122. matchDiffLine = diffLine
  123. }
  124. }
  125. }
  126. if addCount == delCount {
  127. return matchDiffLine
  128. }
  129. return nil
  130. }
  131. var diffMatchPatch = diffmatchpatch.New()
  132. func init() {
  133. diffMatchPatch.DiffEditCost = 100
  134. }
  135. // GetComputedInlineDiffFor computes inline diff for the given line.
  136. func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
  137. if setting.Git.DisableDiffHighlight {
  138. return template.HTML(html.EscapeString(diffLine.Content[1:]))
  139. }
  140. var (
  141. compareDiffLine *DiffLine
  142. diff1 string
  143. diff2 string
  144. )
  145. // try to find equivalent diff line. ignore, otherwise
  146. switch diffLine.Type {
  147. case DiffLineAdd:
  148. compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
  149. if compareDiffLine == nil {
  150. return template.HTML(html.EscapeString(diffLine.Content))
  151. }
  152. diff1 = compareDiffLine.Content
  153. diff2 = diffLine.Content
  154. case DiffLineDel:
  155. compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
  156. if compareDiffLine == nil {
  157. return template.HTML(html.EscapeString(diffLine.Content))
  158. }
  159. diff1 = diffLine.Content
  160. diff2 = compareDiffLine.Content
  161. default:
  162. return template.HTML(html.EscapeString(diffLine.Content))
  163. }
  164. diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true)
  165. diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
  166. return diffToHTML(diffRecord, diffLine.Type)
  167. }
  168. // DiffFile represents a file diff.
  169. type DiffFile struct {
  170. Name string
  171. OldName string
  172. Index int
  173. Addition, Deletion int
  174. Type DiffFileType
  175. IsCreated bool
  176. IsDeleted bool
  177. IsBin bool
  178. IsLFSFile bool
  179. IsRenamed bool
  180. IsSubmodule bool
  181. Sections []*DiffSection
  182. IsIncomplete bool
  183. }
  184. // GetType returns type of diff file.
  185. func (diffFile *DiffFile) GetType() int {
  186. return int(diffFile.Type)
  187. }
  188. // GetHighlightClass returns highlight class for a filename.
  189. func (diffFile *DiffFile) GetHighlightClass() string {
  190. return highlight.FileNameToHighlightClass(diffFile.Name)
  191. }
  192. // Diff represents a difference between two git trees.
  193. type Diff struct {
  194. TotalAddition, TotalDeletion int
  195. Files []*DiffFile
  196. IsIncomplete bool
  197. }
  198. // NumFiles returns number of files changes in a diff.
  199. func (diff *Diff) NumFiles() int {
  200. return len(diff.Files)
  201. }
  202. const cmdDiffHead = "diff --git "
  203. // ParsePatch builds a Diff object from a io.Reader and some
  204. // parameters.
  205. // TODO: move this function to gogits/git-module
  206. func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*Diff, error) {
  207. var (
  208. diff = &Diff{Files: make([]*DiffFile, 0)}
  209. curFile = &DiffFile{}
  210. curSection = &DiffSection{
  211. Lines: make([]*DiffLine, 0, 10),
  212. }
  213. leftLine, rightLine int
  214. lineCount int
  215. curFileLinesCount int
  216. curFileLFSPrefix bool
  217. )
  218. input := bufio.NewReader(reader)
  219. isEOF := false
  220. for !isEOF {
  221. var linebuf bytes.Buffer
  222. for {
  223. b, err := input.ReadByte()
  224. if err != nil {
  225. if err == io.EOF {
  226. isEOF = true
  227. break
  228. } else {
  229. return nil, fmt.Errorf("ReadByte: %v", err)
  230. }
  231. }
  232. if b == '\n' {
  233. break
  234. }
  235. if linebuf.Len() < maxLineCharacters {
  236. linebuf.WriteByte(b)
  237. } else if linebuf.Len() == maxLineCharacters {
  238. curFile.IsIncomplete = true
  239. }
  240. }
  241. line := linebuf.String()
  242. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
  243. continue
  244. }
  245. trimLine := strings.Trim(line, "+- ")
  246. if trimLine == LFSMetaFileIdentifier {
  247. curFileLFSPrefix = true
  248. }
  249. if curFileLFSPrefix && strings.HasPrefix(trimLine, LFSMetaFileOidPrefix) {
  250. oid := strings.TrimPrefix(trimLine, LFSMetaFileOidPrefix)
  251. if len(oid) == 64 {
  252. m := &LFSMetaObject{Oid: oid}
  253. count, err := x.Count(m)
  254. if err == nil && count > 0 {
  255. curFile.IsBin = true
  256. curFile.IsLFSFile = true
  257. curSection.Lines = nil
  258. }
  259. }
  260. }
  261. curFileLinesCount++
  262. lineCount++
  263. // Diff data too large, we only show the first about maxLines lines
  264. if curFileLinesCount >= maxLines {
  265. curFile.IsIncomplete = true
  266. }
  267. switch {
  268. case line[0] == ' ':
  269. diffLine := &DiffLine{Type: DiffLinePlain, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  270. leftLine++
  271. rightLine++
  272. curSection.Lines = append(curSection.Lines, diffLine)
  273. continue
  274. case line[0] == '@':
  275. curSection = &DiffSection{}
  276. curFile.Sections = append(curFile.Sections, curSection)
  277. ss := strings.Split(line, "@@")
  278. diffLine := &DiffLine{Type: DiffLineSection, Content: line}
  279. curSection.Lines = append(curSection.Lines, diffLine)
  280. // Parse line number.
  281. ranges := strings.Split(ss[1][1:], " ")
  282. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  283. if len(ranges) > 1 {
  284. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  285. } else {
  286. log.Warn("Parse line number failed: %v", line)
  287. rightLine = leftLine
  288. }
  289. continue
  290. case line[0] == '+':
  291. curFile.Addition++
  292. diff.TotalAddition++
  293. diffLine := &DiffLine{Type: DiffLineAdd, Content: line, RightIdx: rightLine}
  294. rightLine++
  295. curSection.Lines = append(curSection.Lines, diffLine)
  296. continue
  297. case line[0] == '-':
  298. curFile.Deletion++
  299. diff.TotalDeletion++
  300. diffLine := &DiffLine{Type: DiffLineDel, Content: line, LeftIdx: leftLine}
  301. if leftLine > 0 {
  302. leftLine++
  303. }
  304. curSection.Lines = append(curSection.Lines, diffLine)
  305. case strings.HasPrefix(line, "Binary"):
  306. curFile.IsBin = true
  307. continue
  308. }
  309. // Get new file.
  310. if strings.HasPrefix(line, cmdDiffHead) {
  311. middle := -1
  312. // Note: In case file name is surrounded by double quotes (it happens only in git-shell).
  313. // e.g. diff --git "a/xxx" "b/xxx"
  314. hasQuote := line[len(cmdDiffHead)] == '"'
  315. if hasQuote {
  316. middle = strings.Index(line, ` "b/`)
  317. } else {
  318. middle = strings.Index(line, " b/")
  319. }
  320. beg := len(cmdDiffHead)
  321. a := line[beg+2 : middle]
  322. b := line[middle+3:]
  323. if hasQuote {
  324. var err error
  325. a, err = strconv.Unquote(a)
  326. if err != nil {
  327. return nil, fmt.Errorf("Unquote: %v", err)
  328. }
  329. b, err = strconv.Unquote(b)
  330. if err != nil {
  331. return nil, fmt.Errorf("Unquote: %v", err)
  332. }
  333. }
  334. curFile = &DiffFile{
  335. Name: b,
  336. OldName: a,
  337. Index: len(diff.Files) + 1,
  338. Type: DiffFileChange,
  339. Sections: make([]*DiffSection, 0, 10),
  340. IsRenamed: a != b,
  341. }
  342. diff.Files = append(diff.Files, curFile)
  343. if len(diff.Files) >= maxFiles {
  344. diff.IsIncomplete = true
  345. io.Copy(ioutil.Discard, reader)
  346. break
  347. }
  348. curFileLinesCount = 0
  349. curFileLFSPrefix = false
  350. // Check file diff type and is submodule.
  351. for {
  352. line, err := input.ReadString('\n')
  353. if err != nil {
  354. if err == io.EOF {
  355. isEOF = true
  356. } else {
  357. return nil, fmt.Errorf("ReadString: %v", err)
  358. }
  359. }
  360. switch {
  361. case strings.HasPrefix(line, "new file"):
  362. curFile.Type = DiffFileAdd
  363. curFile.IsCreated = true
  364. case strings.HasPrefix(line, "deleted"):
  365. curFile.Type = DiffFileDel
  366. curFile.IsDeleted = true
  367. case strings.HasPrefix(line, "index"):
  368. curFile.Type = DiffFileChange
  369. case strings.HasPrefix(line, "similarity index 100%"):
  370. curFile.Type = DiffFileRename
  371. }
  372. if curFile.Type > 0 {
  373. if strings.HasSuffix(line, " 160000\n") {
  374. curFile.IsSubmodule = true
  375. }
  376. break
  377. }
  378. }
  379. }
  380. }
  381. // FIXME: detect encoding while parsing.
  382. var buf bytes.Buffer
  383. for _, f := range diff.Files {
  384. buf.Reset()
  385. for _, sec := range f.Sections {
  386. for _, l := range sec.Lines {
  387. buf.WriteString(l.Content)
  388. buf.WriteString("\n")
  389. }
  390. }
  391. charsetLabel, err := base.DetectEncoding(buf.Bytes())
  392. if charsetLabel != "UTF-8" && err == nil {
  393. encoding, _ := charset.Lookup(charsetLabel)
  394. if encoding != nil {
  395. d := encoding.NewDecoder()
  396. for _, sec := range f.Sections {
  397. for _, l := range sec.Lines {
  398. if c, _, err := transform.String(d, l.Content); err == nil {
  399. l.Content = c
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. return diff, nil
  407. }
  408. // GetDiffRange builds a Diff between two commits of a repository.
  409. // passing the empty string as beforeCommitID returns a diff from the
  410. // parent commit.
  411. func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
  412. gitRepo, err := git.OpenRepository(repoPath)
  413. if err != nil {
  414. return nil, err
  415. }
  416. commit, err := gitRepo.GetCommit(afterCommitID)
  417. if err != nil {
  418. return nil, err
  419. }
  420. var cmd *exec.Cmd
  421. // if "after" commit given
  422. if len(beforeCommitID) == 0 {
  423. // First commit of repository.
  424. if commit.ParentCount() == 0 {
  425. cmd = exec.Command("git", "show", afterCommitID)
  426. } else {
  427. c, _ := commit.Parent(0)
  428. cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
  429. }
  430. } else {
  431. cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
  432. }
  433. cmd.Dir = repoPath
  434. cmd.Stderr = os.Stderr
  435. stdout, err := cmd.StdoutPipe()
  436. if err != nil {
  437. return nil, fmt.Errorf("StdoutPipe: %v", err)
  438. }
  439. if err = cmd.Start(); err != nil {
  440. return nil, fmt.Errorf("Start: %v", err)
  441. }
  442. pid := process.GetManager().Add(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath), cmd)
  443. defer process.GetManager().Remove(pid)
  444. diff, err := ParsePatch(maxLines, maxLineCharacters, maxFiles, stdout)
  445. if err != nil {
  446. return nil, fmt.Errorf("ParsePatch: %v", err)
  447. }
  448. if err = cmd.Wait(); err != nil {
  449. return nil, fmt.Errorf("Wait: %v", err)
  450. }
  451. return diff, nil
  452. }
  453. // RawDiffType type of a raw diff.
  454. type RawDiffType string
  455. // RawDiffType possible values.
  456. const (
  457. RawDiffNormal RawDiffType = "diff"
  458. RawDiffPatch RawDiffType = "patch"
  459. )
  460. // GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
  461. // TODO: move this function to gogits/git-module
  462. func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
  463. repo, err := git.OpenRepository(repoPath)
  464. if err != nil {
  465. return fmt.Errorf("OpenRepository: %v", err)
  466. }
  467. commit, err := repo.GetCommit(commitID)
  468. if err != nil {
  469. return fmt.Errorf("GetCommit: %v", err)
  470. }
  471. var cmd *exec.Cmd
  472. switch diffType {
  473. case RawDiffNormal:
  474. if commit.ParentCount() == 0 {
  475. cmd = exec.Command("git", "show", commitID)
  476. } else {
  477. c, _ := commit.Parent(0)
  478. cmd = exec.Command("git", "diff", "-M", c.ID.String(), commitID)
  479. }
  480. case RawDiffPatch:
  481. if commit.ParentCount() == 0 {
  482. cmd = exec.Command("git", "format-patch", "--no-signature", "--stdout", "--root", commitID)
  483. } else {
  484. c, _ := commit.Parent(0)
  485. query := fmt.Sprintf("%s...%s", commitID, c.ID.String())
  486. cmd = exec.Command("git", "format-patch", "--no-signature", "--stdout", query)
  487. }
  488. default:
  489. return fmt.Errorf("invalid diffType: %s", diffType)
  490. }
  491. stderr := new(bytes.Buffer)
  492. cmd.Dir = repoPath
  493. cmd.Stdout = writer
  494. cmd.Stderr = stderr
  495. if err = cmd.Run(); err != nil {
  496. return fmt.Errorf("Run: %v - %s", err, stderr)
  497. }
  498. return nil
  499. }
  500. // GetDiffCommit builds a Diff representing the given commitID.
  501. func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
  502. return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles)
  503. }