diff --git a/commit.go b/commit.go index 27525d2..0b464f9 100644 --- a/commit.go +++ b/commit.go @@ -16,8 +16,9 @@ type RepoCommits struct { Config configCollect CommitDesc map[string]string CommitContent map[string]*CommitNode - Head *CommitNode Root *CommitNode + Leafs map[*CommitNode]Empty + Head *CommitNode } type CommitNode struct { @@ -58,12 +59,15 @@ func cmdCommit(config configCollect) { rc := NewRepoCommits(config) rc.CollectDescs() - rc.PrintDescs() + log.Println("Repo contains", len(rc.CommitDesc), "commits") rc.CollectContent() rc.BuildGraph() - rc.FindHead() - rc.PrintHead() + rc.FindLeafs() + log.Println("Repo has", len(rc.Leafs), "leafs") + + rc.ChooseHead() + log.Println("Proposing following HEAD:\n"+rc.Head.Content.String()) } func NewRepoCommits (config configCollect) *RepoCommits { @@ -71,6 +75,7 @@ func NewRepoCommits (config configCollect) *RepoCommits { rc.Config = config rc.CommitDesc = make(map[string]string) rc.CommitContent = make(map[string]*CommitNode) + rc.Leafs = make(map[*CommitNode]Empty) return rc } @@ -90,19 +95,6 @@ func (rc* RepoCommits) CollectDescs() { } } -func (rc* RepoCommits) PrintDescs() { - /*limit := 10 - for id, path := range rc.CommitDesc { - log.Println(id, path) - limit = limit - 1 - if limit < 0 { - log.Println("Too many commits, output has been truncated") - break - } - }*/ - log.Println("Repo", rc.Config.RepoId, "contains", len(rc.CommitDesc), "commits") -} - func NewCommitNode(c Commit) *CommitNode { cn := new(CommitNode) cn.Content = c @@ -153,7 +145,7 @@ func (rc* RepoCommits) BuildGraph() *CommitNode { return rc.Root } -func (rc* RepoCommits) FindHead() *CommitNode { +func (rc* RepoCommits) FindLeafs() map[*CommitNode]Empty { toProcess := make(map[*CommitNode]Empty) toProcess[rc.Root] = empty @@ -164,21 +156,25 @@ func (rc* RepoCommits) FindHead() *CommitNode { nextToProcess[ccn] = empty } if len(cn.Children) == 0 { - if rc.Head == nil { - rc.Head = cn - } else { - log.Fatal("More than one HEAD has been found") - } + rc.Leafs[cn] = empty } } toProcess = nextToProcess } + if len(rc.Leafs) == 0 { log.Fatal("No leafs have been found") } + return rc.Leafs +} + +func (rc* RepoCommits) ChooseHead() *CommitNode { + for cn, _ := range rc.Leafs { + if rc.Head == nil { + rc.Head = cn + } else if rc.Head.Content.Ctime < cn.Content.Ctime { + rc.Head = cn + } + } if rc.Head == nil { log.Fatal("No HEAD has been found") } return rc.Head } - -func (rc* RepoCommits) PrintHead() { - log.Println(rc.Head.Content.String()) -}