-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from adigulalkari/feat-calc-stats-branch
Feat calc stats branch
- Loading branch information
Showing
1 changed file
with
71 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,79 @@ | ||
package analyzer | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"fmt" | ||
"log" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5" // Core Go-git library | ||
"github.com/go-git/go-git/v5/plumbing/object" // Used for commit objects | ||
"github.com/go-git/go-git/v5" // Core Go-git library | ||
"github.com/go-git/go-git/v5/plumbing/object" // Used for commit objects | ||
) | ||
|
||
// AnalyzeCommitHistory analyzes and prints commit history of the given repository | ||
func AnalyzeCommitHistory(repoPath string) { | ||
repo, err := git.PlainOpen(repoPath) | ||
if err != nil { | ||
log.Fatalf("Error opening repository: %v", err) | ||
} | ||
|
||
// Get the HEAD reference | ||
ref, err := repo.Head() | ||
if err != nil { | ||
log.Fatalf("Error getting HEAD reference: %v", err) | ||
} | ||
|
||
// Iterate over the commit history starting from HEAD | ||
commitIter, err := repo.Log(&git.LogOptions{From: ref.Hash()}) | ||
if err != nil { | ||
log.Fatalf("Error getting commit log: %v", err) | ||
} | ||
|
||
fmt.Println("Commit history analysis:") | ||
commitCount := 0 | ||
err = commitIter.ForEach(func(c *object.Commit) error { | ||
fmt.Printf("Commit: %s by %s\n", c.Hash, c.Author.Name) | ||
commitCount++ | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalf("Error iterating over commits: %v", err) | ||
} | ||
|
||
fmt.Printf("Total number of commits: %d\n", commitCount) | ||
repo, err := git.PlainOpen(repoPath) | ||
if err != nil { | ||
log.Fatalf("Error opening repository: %v", err) | ||
} | ||
|
||
// Get the HEAD reference | ||
ref, err := repo.Head() | ||
if err != nil { | ||
log.Fatalf("Error getting HEAD reference: %v", err) | ||
} | ||
|
||
// Iterate over the commit history starting from HEAD | ||
commitIter, err := repo.Log(&git.LogOptions{From: ref.Hash()}) | ||
if err != nil { | ||
log.Fatalf("Error getting commit log: %v", err) | ||
} | ||
|
||
// Map to track the number of commits by each author | ||
commitCounts := make(map[string]int) | ||
commitCount := 0 | ||
|
||
fmt.Println("Commit history analysis:") | ||
err = commitIter.ForEach(func(c *object.Commit) error { | ||
// Trim the commit message to remove leading/trailing whitespace | ||
trimmedMessage := strings.TrimSpace(c.Message) | ||
|
||
// Print each commit's message and author | ||
fmt.Printf("Author: %s Commit Message:%s\n", c.Author.Name,trimmedMessage) | ||
|
||
// Increment commit count for the author | ||
commitCounts[c.Author.Name]++ | ||
commitCount++ | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalf("Error iterating over commits: %v", err) | ||
} | ||
|
||
// Print total number of commits | ||
fmt.Printf("\nTotal number of commits: %d\n", commitCount) | ||
|
||
// Sort authors by the number of commits in descending order | ||
type authorCommit struct { | ||
Author string | ||
Count int | ||
} | ||
|
||
// Create a slice of author commits for sorting | ||
var authorCommits []authorCommit | ||
for author, count := range commitCounts { | ||
authorCommits = append(authorCommits, authorCommit{Author: author, Count: count}) | ||
} | ||
|
||
// Sort the slice by commit count in descending order | ||
sort.Slice(authorCommits, func(i, j int) bool { | ||
return authorCommits[i].Count > authorCommits[j].Count | ||
}) | ||
|
||
// Print the sorted list of authors and their commit counts | ||
fmt.Println("\nNumber of commits by each author (in decreasing order):") | ||
for _, ac := range authorCommits { | ||
fmt.Printf("%s: %d commits\n", ac.Author, ac.Count) | ||
} | ||
} |