Skip to content
Open
Changes from 1 commit
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
68 changes: 67 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package main

import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)

Expand Down Expand Up @@ -32,6 +37,67 @@ func main() {
// number of line deleted
// list of function calls seen in the diffs and their number of calls
func compute() *result {
root := "./diffs"

re, err := regexp.Compile("[A-Za-z_][A-Za-z0-9_]+\\(")
if err != nil {
fmt.Println(err)
}
var regions int
var linesAdded int
var linesDeleted int
var files []string
functionCalls := make(map[string]int)

filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()

if strings.HasPrefix(line, "@@") {
regions++
} else if strings.HasPrefix(line, "+++") {
// If the file has been renamed or copied we keep the newer name and get rid
// of the prefix "+++ b/"
files = append(files, line[6:])
} else if strings.HasPrefix(line, "+") {
linesAdded++
} else if strings.HasPrefix(line, "-") && !strings.HasPrefix(line, "---") {
linesDeleted++
} else {
matches := re.FindAllString(line, -1)
if matches == nil {
continue
}
for _, match := range matches {
// We'll keep only the function name i.e. remove the parentheses and params
functionCall := match[:len(match)-1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without any subgroup, the len(match) - 1 could become 0, unless I'm missing something

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The matches are all at least 2 characters long: the function's name followed by the opening bracket. This is only meant to remove that parenthesis. I'll replace it with https://golang.org/pkg/strings/#TrimSuffix for clarity. I also noticed an error in my regular expression which disallowed function names with a single character.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah my bad, I didn't realize we were working on a match, not on the matches array, my bad


if _, ok := functionCalls[functionCall]; ok {
functionCalls[functionCall]++
} else {
functionCalls[functionCall] = 1
}
}
}
}
return nil
})

return &result{files, regions, linesAdded, linesDeleted, functionCalls}

return nil
}