Skip to content

diff: capture both old and new of index and mode #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ type DiffFile struct {
// The type of the file.
Type DiffFileType
// The index (SHA1 hash) of the file. For a changed/new file, it is the new SHA,
// and for a deleted file it is the old SHA.
// and for a deleted file it becomes "000000".
Index string
// OldIndex is the old index (SHA1 hash) of the file.
OldIndex string
// The sections in the file.
Sections []*DiffSection

Expand All @@ -118,6 +120,9 @@ type DiffFile struct {

oldName string

mode EntryMode
oldMode EntryMode

isBinary bool
isSubmodule bool
isIncomplete bool
Expand Down Expand Up @@ -158,6 +163,16 @@ func (f *DiffFile) OldName() string {
return f.oldName
}

// Mode returns the mode of the file.
func (f *DiffFile) Mode() EntryMode {
return f.mode
}

// OldMode returns the old mode of the file if it's changed.
func (f *DiffFile) OldMode() EntryMode {
return f.oldMode
}

// IsBinary returns true if the file is in binary format.
func (f *DiffFile) IsBinary() bool {
return f.isBinary
Expand Down Expand Up @@ -268,8 +283,9 @@ func (p *diffParser) parseFileHeader() (*DiffFile, error) {
}

file := &DiffFile{
Name: a,
Type: DiffFileChange,
Name: a,
oldName: b,
Type: DiffFileChange,
}

// Check file diff type and submodule
Expand All @@ -291,20 +307,38 @@ checkType:
case strings.HasPrefix(line, "new file"):
file.Type = DiffFileAdd
file.isSubmodule = strings.HasSuffix(line, " 160000")
fields := strings.Fields(line)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.mode = EntryMode(mode)
if file.oldMode == 0 {
file.oldMode = file.mode
}
}
case strings.HasPrefix(line, "deleted"):
file.Type = DiffFileDelete
file.isSubmodule = strings.HasSuffix(line, " 160000")
fields := strings.Fields(line)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.mode = EntryMode(mode)
if file.oldMode == 0 {
file.oldMode = file.mode
}
}
case strings.HasPrefix(line, "index"): // e.g. index ee791be..9997571 100644
fields := strings.Fields(line[6:])
shas := strings.Split(fields[0], "..")
if len(shas) != 2 {
return nil, errors.New("malformed index: expect two SHAs in the form of <old>..<new>")
}

if file.IsDeleted() {
file.Index = shas[0]
} else {
file.Index = shas[1]
file.OldIndex = shas[0]
file.Index = shas[1]
if len(fields) > 1 {
mode, _ := strconv.ParseUint(fields[1], 8, 64)
file.mode = EntryMode(mode)
file.oldMode = EntryMode(mode)
}
break checkType
case strings.HasPrefix(line, "similarity index "):
Expand All @@ -316,8 +350,18 @@ checkType:
if strings.HasSuffix(line, "100%") {
break checkType
}
case strings.HasPrefix(line, "new mode"):
fields := strings.Fields(line)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.mode = EntryMode(mode)
}
case strings.HasPrefix(line, "old mode"):
break checkType
fields := strings.Fields(line)
if len(fields) > 0 {
mode, _ := strconv.ParseUint(fields[len(fields)-1], 8, 64)
file.oldMode = EntryMode(mode)
}
}
}

Expand Down
Loading