mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	Partial impl of git diff encoding
This commit is contained in:
		@@ -87,7 +87,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		leftLine, rightLine int
 | 
							leftLine, rightLine int
 | 
				
			||||||
		isTooLong           bool
 | 
							isTooLong           bool
 | 
				
			||||||
		// FIXME: use first 30 lines to detect file encoding. Should use cache in the future.
 | 
							// FIXME: Should use cache in the future.
 | 
				
			||||||
		buf bytes.Buffer
 | 
							buf bytes.Buffer
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -106,16 +106,10 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		i = i + 1
 | 
							i = i + 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// FIXME: use first 30 lines to detect file encoding.
 | 
					 | 
				
			||||||
		if i <= 30 {
 | 
					 | 
				
			||||||
			buf.WriteString(line)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Diff data too large, we only show the first about maxlines lines
 | 
							// Diff data too large, we only show the first about maxlines lines
 | 
				
			||||||
		if i == maxlines {
 | 
							if i == maxlines {
 | 
				
			||||||
			isTooLong = true
 | 
								isTooLong = true
 | 
				
			||||||
			log.Warn("Diff data too large")
 | 
								log.Warn("Diff data too large")
 | 
				
			||||||
			//return &Diff{}, nil
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		switch {
 | 
							switch {
 | 
				
			||||||
@@ -127,7 +121,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
			continue
 | 
								continue
 | 
				
			||||||
		case line[0] == '@':
 | 
							case line[0] == '@':
 | 
				
			||||||
			if isTooLong {
 | 
								if isTooLong {
 | 
				
			||||||
				return diff, nil
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			curSection = &DiffSection{}
 | 
								curSection = &DiffSection{}
 | 
				
			||||||
@@ -137,9 +131,14 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
			curSection.Lines = append(curSection.Lines, diffLine)
 | 
								curSection.Lines = append(curSection.Lines, diffLine)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Parse line number.
 | 
								// Parse line number.
 | 
				
			||||||
			ranges := strings.Split(ss[len(ss)-2][1:], " ")
 | 
								ranges := strings.Split(ss[1][1:], " ")
 | 
				
			||||||
			leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
 | 
								leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
 | 
				
			||||||
			rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
 | 
								if len(ranges) > 1 {
 | 
				
			||||||
 | 
									rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									log.Warn("Parse line number failed: %v", line)
 | 
				
			||||||
 | 
									rightLine = leftLine
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		case line[0] == '+':
 | 
							case line[0] == '+':
 | 
				
			||||||
			curFile.Addition++
 | 
								curFile.Addition++
 | 
				
			||||||
@@ -164,7 +163,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
		// Get new file.
 | 
							// Get new file.
 | 
				
			||||||
		if strings.HasPrefix(line, DIFF_HEAD) {
 | 
							if strings.HasPrefix(line, DIFF_HEAD) {
 | 
				
			||||||
			if isTooLong {
 | 
								if isTooLong {
 | 
				
			||||||
				return diff, nil
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			beg := len(DIFF_HEAD)
 | 
								beg := len(DIFF_HEAD)
 | 
				
			||||||
@@ -201,14 +200,19 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// FIXME: use first 30 lines to detect file encoding.
 | 
						for _, f := range diff.Files {
 | 
				
			||||||
	charsetLabel, err := base.DetectEncoding(buf.Bytes())
 | 
							buf.Reset()
 | 
				
			||||||
	if charsetLabel != "utf8" && err == nil {
 | 
							for _, sec := range f.Sections {
 | 
				
			||||||
		encoding, _ := charset.Lookup(charsetLabel)
 | 
								for _, l := range sec.Lines {
 | 
				
			||||||
 | 
									buf.WriteString(l.Content)
 | 
				
			||||||
		if encoding != nil {
 | 
									buf.WriteString("\n")
 | 
				
			||||||
			d := encoding.NewDecoder()
 | 
								}
 | 
				
			||||||
			for _, f := range diff.Files {
 | 
							}
 | 
				
			||||||
 | 
							charsetLabel, err := base.DetectEncoding(buf.Bytes())
 | 
				
			||||||
 | 
							if charsetLabel != "UTF-8" && err == nil {
 | 
				
			||||||
 | 
								encoding, _ := charset.Lookup(charsetLabel)
 | 
				
			||||||
 | 
								if encoding != nil {
 | 
				
			||||||
 | 
									d := encoding.NewDecoder()
 | 
				
			||||||
				for _, sec := range f.Sections {
 | 
									for _, sec := range f.Sections {
 | 
				
			||||||
					for _, l := range sec.Lines {
 | 
										for _, l := range sec.Lines {
 | 
				
			||||||
						if c, _, err := transform.String(d, l.Content); err == nil {
 | 
											if c, _, err := transform.String(d, l.Content); err == nil {
 | 
				
			||||||
@@ -219,7 +223,6 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return diff, nil
 | 
						return diff, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user