Skip to content

Commit

Permalink
Merge pull request #12 from adigulalkari/feat-calc-stats-branch
Browse files Browse the repository at this point in the history
Feat calc stats branch
  • Loading branch information
adigulalkari authored Oct 2, 2024
2 parents 90b0506 + fa2ccec commit 0aadfcd
Showing 1 changed file with 71 additions and 34 deletions.
105 changes: 71 additions & 34 deletions pkg/analyzer/commit_history.go
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)
}
}

0 comments on commit 0aadfcd

Please sign in to comment.