mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 01:34:27 +00:00 
			
		
		
		
	More on diff page
This commit is contained in:
		
							
								
								
									
										2
									
								
								gogs.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								gogs.go
									
									
									
									
									
								
							@@ -19,7 +19,7 @@ import (
 | 
				
			|||||||
// Test that go1.2 tag above is included in builds. main.go refers to this definition.
 | 
					// Test that go1.2 tag above is included in builds. main.go refers to this definition.
 | 
				
			||||||
const go12tag = true
 | 
					const go12tag = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const APP_VER = "0.1.8.0325"
 | 
					const APP_VER = "0.1.8.0326"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	base.AppVer = APP_VER
 | 
						base.AppVer = APP_VER
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -274,7 +274,6 @@ const DIFF_HEAD = "diff --git "
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func ParsePatch(reader io.Reader) (*Diff, error) {
 | 
					func ParsePatch(reader io.Reader) (*Diff, error) {
 | 
				
			||||||
	scanner := bufio.NewScanner(reader)
 | 
						scanner := bufio.NewScanner(reader)
 | 
				
			||||||
	var totalAdd, totalDel int
 | 
					 | 
				
			||||||
	var curFile *DiffFile
 | 
						var curFile *DiffFile
 | 
				
			||||||
	curSection := &DiffSection{
 | 
						curSection := &DiffSection{
 | 
				
			||||||
		Lines: make([]*DiffLine, 0, 10),
 | 
							Lines: make([]*DiffLine, 0, 10),
 | 
				
			||||||
@@ -285,6 +284,10 @@ func ParsePatch(reader io.Reader) (*Diff, error) {
 | 
				
			|||||||
	for scanner.Scan() {
 | 
						for scanner.Scan() {
 | 
				
			||||||
		line := scanner.Text()
 | 
							line := scanner.Text()
 | 
				
			||||||
		fmt.Println(i, line)
 | 
							fmt.Println(i, line)
 | 
				
			||||||
 | 
							if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		i = i + 1
 | 
							i = i + 1
 | 
				
			||||||
		if line == "" {
 | 
							if line == "" {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
@@ -300,40 +303,51 @@ func ParsePatch(reader io.Reader) (*Diff, error) {
 | 
				
			|||||||
			diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: "@@" + ss[len(ss)-2] + "@@"}
 | 
								diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: "@@" + ss[len(ss)-2] + "@@"}
 | 
				
			||||||
			curSection.Lines = append(curSection.Lines, diffLine)
 | 
								curSection.Lines = append(curSection.Lines, diffLine)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			diffLine = &DiffLine{Type: DIFF_LINE_PLAIN, Content: ss[len(ss)-1]}
 | 
								if len(ss[len(ss)-1]) > 0 {
 | 
				
			||||||
			curSection.Lines = append(curSection.Lines, diffLine)
 | 
									diffLine = &DiffLine{Type: DIFF_LINE_PLAIN, Content: ss[len(ss)-1]}
 | 
				
			||||||
 | 
									curSection.Lines = append(curSection.Lines, diffLine)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		} else if line[0] == '+' {
 | 
							} else if line[0] == '+' {
 | 
				
			||||||
 | 
								curFile.Addition++
 | 
				
			||||||
 | 
								diff.TotalAddition++
 | 
				
			||||||
			diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line}
 | 
								diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line}
 | 
				
			||||||
			curSection.Lines = append(curSection.Lines, diffLine)
 | 
								curSection.Lines = append(curSection.Lines, diffLine)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		} else if line[0] == '-' {
 | 
							} else if line[0] == '-' {
 | 
				
			||||||
 | 
								curFile.Deletion++
 | 
				
			||||||
 | 
								diff.TotalDeletion++
 | 
				
			||||||
			diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line}
 | 
								diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line}
 | 
				
			||||||
			curSection.Lines = append(curSection.Lines, diffLine)
 | 
								curSection.Lines = append(curSection.Lines, diffLine)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Get new file.
 | 
				
			||||||
		if strings.HasPrefix(line, DIFF_HEAD) {
 | 
							if strings.HasPrefix(line, DIFF_HEAD) {
 | 
				
			||||||
			if curFile != nil {
 | 
					 | 
				
			||||||
				curFile.Addition, totalAdd = totalAdd, 0
 | 
					 | 
				
			||||||
				curFile.Deletion, totalDel = totalDel, 0
 | 
					 | 
				
			||||||
				curFile = nil
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			fs := strings.Split(line[len(DIFF_HEAD):], " ")
 | 
								fs := strings.Split(line[len(DIFF_HEAD):], " ")
 | 
				
			||||||
			a := fs[0]
 | 
								a := fs[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			curFile = &DiffFile{
 | 
								curFile = &DiffFile{
 | 
				
			||||||
				Name:     a[strings.Index(a, "/")+1:],
 | 
									Name:     a[strings.Index(a, "/")+1:],
 | 
				
			||||||
				Type:     DIFF_FILE_CHANGE,
 | 
									Type:     DIFF_FILE_CHANGE,
 | 
				
			||||||
				Sections: make([]*DiffSection, 0),
 | 
									Sections: make([]*DiffSection, 0, 10),
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			diff.Files = append(diff.Files, curFile)
 | 
								diff.Files = append(diff.Files, curFile)
 | 
				
			||||||
			scanner.Scan()
 | 
					
 | 
				
			||||||
			scanner.Scan()
 | 
								// Check file diff type.
 | 
				
			||||||
			if scanner.Text() == "--- /dev/null" {
 | 
								for scanner.Scan() {
 | 
				
			||||||
				curFile.Type = DIFF_FILE_ADD
 | 
									switch {
 | 
				
			||||||
 | 
									case strings.HasPrefix(scanner.Text(), "new file"):
 | 
				
			||||||
 | 
										curFile.Type = DIFF_FILE_ADD
 | 
				
			||||||
 | 
									case strings.HasPrefix(scanner.Text(), "deleted"):
 | 
				
			||||||
 | 
										curFile.Type = DIFF_FILE_DEL
 | 
				
			||||||
 | 
									case strings.HasPrefix(scanner.Text(), "index"):
 | 
				
			||||||
 | 
										curFile.Type = DIFF_FILE_CHANGE
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									if curFile.Type > 0 {
 | 
				
			||||||
 | 
										break
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			scanner.Scan()
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -351,14 +365,13 @@ func GetDiff(repoPath, commitid string) (*Diff, error) {
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// ????
 | 
					 | 
				
			||||||
	if commit.ParentCount() == 0 {
 | 
						if commit.ParentCount() == 0 {
 | 
				
			||||||
		return &Diff{}, err
 | 
							return &Diff{}, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rd, wr := io.Pipe()
 | 
						rd, wr := io.Pipe()
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		cmd := exec.Command("git", "diff", commitid, commit.Parent(0).Oid.String())
 | 
							cmd := exec.Command("git", "diff", commit.Parent(0).Oid.String(), commitid)
 | 
				
			||||||
		cmd.Dir = repoPath
 | 
							cmd.Dir = repoPath
 | 
				
			||||||
		cmd.Stdout = wr
 | 
							cmd.Stdout = wr
 | 
				
			||||||
		cmd.Stdin = os.Stdin
 | 
							cmd.Stdin = os.Stdin
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -70,4 +70,5 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{
 | 
				
			|||||||
	"SubStr": func(str string, start, length int) string {
 | 
						"SubStr": func(str string, start, length int) string {
 | 
				
			||||||
		return str[start : start+length]
 | 
							return str[start : start+length]
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
						"DiffTypeToStr": DiffTypeToStr,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -539,3 +539,16 @@ func ActionDesc(act Actioner, avatarLink string) string {
 | 
				
			|||||||
		return "invalid type"
 | 
							return "invalid type"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func DiffTypeToStr(diffType int) string {
 | 
				
			||||||
 | 
						switch diffType {
 | 
				
			||||||
 | 
						case 1:
 | 
				
			||||||
 | 
							return "add"
 | 
				
			||||||
 | 
						case 2:
 | 
				
			||||||
 | 
							return "modify"
 | 
				
			||||||
 | 
						case 3:
 | 
				
			||||||
 | 
							return "del"
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return "unknown"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,7 +39,7 @@
 | 
				
			|||||||
                        <span class="del" data-line="{{.Deletion}}">{{.Deletion}}</span>
 | 
					                        <span class="del" data-line="{{.Deletion}}">{{.Deletion}}</span>
 | 
				
			||||||
                    </div>
 | 
					                    </div>
 | 
				
			||||||
                    <!-- todo finish all file status, now modify, add, delete and rename -->
 | 
					                    <!-- todo finish all file status, now modify, add, delete and rename -->
 | 
				
			||||||
                    <span class="status modify" data-toggle="tooltip" data-placement="right" title="modify"> </span>
 | 
					                    <span class="status {{DiffTypeToStr .Type}}" data-toggle="tooltip" data-placement="right" title="{{DiffTypeToStr .Type}}"> </span>
 | 
				
			||||||
                    <a class="file" href="#diff-1">{{.Name}}</a>
 | 
					                    <a class="file" href="#diff-1">{{.Name}}</a>
 | 
				
			||||||
                </li>
 | 
					                </li>
 | 
				
			||||||
                {{end}}
 | 
					                {{end}}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user