|
|
@ -37,6 +37,7 @@ const ( |
|
|
|
DIFF_FILE_ADD = iota + 1 |
|
|
|
DIFF_FILE_ADD = iota + 1 |
|
|
|
DIFF_FILE_CHANGE |
|
|
|
DIFF_FILE_CHANGE |
|
|
|
DIFF_FILE_DEL |
|
|
|
DIFF_FILE_DEL |
|
|
|
|
|
|
|
DIFF_FILE_RENAME |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type DiffLine struct { |
|
|
|
type DiffLine struct { |
|
|
@ -57,12 +58,14 @@ type DiffSection struct { |
|
|
|
|
|
|
|
|
|
|
|
type DiffFile struct { |
|
|
|
type DiffFile struct { |
|
|
|
Name string |
|
|
|
Name string |
|
|
|
|
|
|
|
OldName string |
|
|
|
Index int |
|
|
|
Index int |
|
|
|
Addition, Deletion int |
|
|
|
Addition, Deletion int |
|
|
|
Type int |
|
|
|
Type int |
|
|
|
IsCreated bool |
|
|
|
IsCreated bool |
|
|
|
IsDeleted bool |
|
|
|
IsDeleted bool |
|
|
|
IsBin bool |
|
|
|
IsBin bool |
|
|
|
|
|
|
|
IsRenamed bool |
|
|
|
Sections []*DiffSection |
|
|
|
Sections []*DiffSection |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -158,17 +161,29 @@ 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) { |
|
|
|
beg := len(DIFF_HEAD) |
|
|
|
middle := -1 |
|
|
|
a := line[beg : (len(line)-beg)/2+beg] |
|
|
|
|
|
|
|
|
|
|
|
// Note: In case file name is surrounded by double quotes(it happens only in git-shell).
|
|
|
|
|
|
|
|
hasQuote := strings.Index(line, `\"`) > -1 |
|
|
|
|
|
|
|
if hasQuote { |
|
|
|
|
|
|
|
line = strings.Replace(line, `\"`, `"`, -1) |
|
|
|
|
|
|
|
middle = strings.Index(line, ` "b/`) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
middle = strings.Index(line, " b/") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// In case file name is surrounded by double quotes(it happens only in git-shell).
|
|
|
|
beg := len(DIFF_HEAD) |
|
|
|
if a[0] == '"' { |
|
|
|
a := line[beg+2 : middle] |
|
|
|
|
|
|
|
b := line[middle+3:] |
|
|
|
|
|
|
|
if hasQuote { |
|
|
|
a = a[1 : len(a)-1] |
|
|
|
a = a[1 : len(a)-1] |
|
|
|
a = strings.Replace(a, `\"`, `"`, -1) |
|
|
|
b = b[1 : len(b)-1] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println(a, b) |
|
|
|
|
|
|
|
|
|
|
|
curFile = &DiffFile{ |
|
|
|
curFile = &DiffFile{ |
|
|
|
Name: a[strings.Index(a, "/")+1:], |
|
|
|
Name: a, |
|
|
|
Index: len(diff.Files) + 1, |
|
|
|
Index: len(diff.Files) + 1, |
|
|
|
Type: DIFF_FILE_CHANGE, |
|
|
|
Type: DIFF_FILE_CHANGE, |
|
|
|
Sections: make([]*DiffSection, 0, 10), |
|
|
|
Sections: make([]*DiffSection, 0, 10), |
|
|
@ -180,16 +195,17 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff |
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case strings.HasPrefix(scanner.Text(), "new file"): |
|
|
|
case strings.HasPrefix(scanner.Text(), "new file"): |
|
|
|
curFile.Type = DIFF_FILE_ADD |
|
|
|
curFile.Type = DIFF_FILE_ADD |
|
|
|
curFile.IsDeleted = false |
|
|
|
|
|
|
|
curFile.IsCreated = true |
|
|
|
curFile.IsCreated = true |
|
|
|
case strings.HasPrefix(scanner.Text(), "deleted"): |
|
|
|
case strings.HasPrefix(scanner.Text(), "deleted"): |
|
|
|
curFile.Type = DIFF_FILE_DEL |
|
|
|
curFile.Type = DIFF_FILE_DEL |
|
|
|
curFile.IsCreated = false |
|
|
|
|
|
|
|
curFile.IsDeleted = true |
|
|
|
curFile.IsDeleted = true |
|
|
|
case strings.HasPrefix(scanner.Text(), "index"): |
|
|
|
case strings.HasPrefix(scanner.Text(), "index"): |
|
|
|
curFile.Type = DIFF_FILE_CHANGE |
|
|
|
curFile.Type = DIFF_FILE_CHANGE |
|
|
|
curFile.IsCreated = false |
|
|
|
case strings.HasPrefix(scanner.Text(), "similarity index 100%"): |
|
|
|
curFile.IsDeleted = false |
|
|
|
curFile.Type = DIFF_FILE_RENAME |
|
|
|
|
|
|
|
curFile.IsRenamed = true |
|
|
|
|
|
|
|
curFile.OldName = curFile.Name |
|
|
|
|
|
|
|
curFile.Name = b |
|
|
|
} |
|
|
|
} |
|
|
|
if curFile.Type > 0 { |
|
|
|
if curFile.Type > 0 { |
|
|
|
break |
|
|
|
break |
|
|
@ -244,10 +260,10 @@ func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxline |
|
|
|
cmd = exec.Command("git", "show", afterCommitId) |
|
|
|
cmd = exec.Command("git", "show", afterCommitId) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
c, _ := commit.Parent(0) |
|
|
|
c, _ := commit.Parent(0) |
|
|
|
cmd = exec.Command("git", "diff", c.Id.String(), afterCommitId) |
|
|
|
cmd = exec.Command("git", "diff", "-M", c.Id.String(), afterCommitId) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
cmd = exec.Command("git", "diff", beforeCommitId, afterCommitId) |
|
|
|
cmd = exec.Command("git", "diff", "-M", beforeCommitId, afterCommitId) |
|
|
|
} |
|
|
|
} |
|
|
|
cmd.Dir = repoPath |
|
|
|
cmd.Dir = repoPath |
|
|
|
cmd.Stdout = wr |
|
|
|
cmd.Stdout = wr |
|
|
|