@@ -25,32 +25,50 @@ func NewRepository(id string, repo *git.Repository) Repository {
2525 }
2626}
2727
28- // RepositoryPool holds a pool of initialized git repositories and
28+ // NewRepositoryFromPath creates and initializes a new Repository structure
29+ // and initializes a go-git repository
30+ func NewRepositoryFromPath (id , path string ) (Repository , error ) {
31+ repo , err := git .PlainOpen (path )
32+ if err != nil {
33+ return Repository {}, err
34+ }
35+
36+ return NewRepository (id , repo ), nil
37+ }
38+
39+ // RepositoryPool holds a pool git repository paths and
2940// functionality to open and iterate them.
3041type RepositoryPool struct {
31- repositories []Repository
42+ repositories map [string ]string
43+ idOrder []string
3244}
3345
3446// NewRepositoryPool initializes a new RepositoryPool
3547func NewRepositoryPool () RepositoryPool {
36- return RepositoryPool {}
48+ return RepositoryPool {
49+ repositories : make (map [string ]string ),
50+ }
3751}
3852
3953// Add inserts a new repository in the pool
40- func (p * RepositoryPool ) Add (id string , repo * git.Repository ) {
41- repository := NewRepository (id , repo )
42- p .repositories = append (p .repositories , repository )
54+ func (p * RepositoryPool ) Add (id , path string ) {
55+ _ , ok := p .repositories [id ]
56+ if ! ok {
57+ p .idOrder = append (p .idOrder , id )
58+ }
59+
60+ p .repositories [id ] = path
4361}
4462
45- // AddGit opens a new git repository and adds it to the pool. It
63+ // AddGit checks if a git repository can be opened and adds it to the pool. It
4664// also sets its path as ID.
4765func (p * RepositoryPool ) AddGit (path string ) (string , error ) {
48- repo , err := git .PlainOpen (path )
66+ _ , err := git .PlainOpen (path )
4967 if err != nil {
5068 return "" , err
5169 }
5270
53- p .Add (path , repo )
71+ p .Add (path , path )
5472
5573 return path , nil
5674}
@@ -65,26 +83,33 @@ func (p *RepositoryPool) AddDir(path string) error {
6583 for _ , f := range dirs {
6684 if f .IsDir () {
6785 name := filepath .Join (path , f .Name ())
68- repo , err := git .PlainOpen (name )
69- if err != nil {
70- // TODO: log that the repo could not be opened
71- } else {
72- p .Add (f .Name (), repo )
73- }
86+ // TODO: log that the repo could not be opened
87+ p .AddGit (name )
7488 }
7589 }
7690
7791 return nil
7892}
7993
80- // GetPos retrieves a repository at a given position. It returns false
81- // as second return value if the position is out of bounds.
82- func (p * RepositoryPool ) GetPos (pos int ) (* Repository , bool ) {
94+ // GetPos retrieves a repository at a given position. If the position is
95+ // out of bounds it returns io.EOF
96+ func (p * RepositoryPool ) GetPos (pos int ) (* Repository , error ) {
8397 if pos >= len (p .repositories ) {
84- return nil , false
98+ return nil , io .EOF
99+ }
100+
101+ id := p .idOrder [pos ]
102+ if id == "" {
103+ return nil , io .EOF
104+ }
105+
106+ path := p .repositories [id ]
107+ repo , err := NewRepositoryFromPath (id , path )
108+ if err != nil {
109+ return nil , err
85110 }
86111
87- return & p . repositories [ pos ], true
112+ return & repo , nil
88113}
89114
90115// RepoIter creates a new Repository iterator
@@ -106,9 +131,9 @@ type RepositoryIter struct {
106131// Next retrieves the next Repository. It returns io.EOF as error
107132// when there are no more Repositories to retrieve.
108133func (i * RepositoryIter ) Next () (* Repository , error ) {
109- r , ok := i .pool .GetPos (i .pos )
110- if ! ok {
111- return nil , io . EOF
134+ r , err := i .pool .GetPos (i .pos )
135+ if err != nil {
136+ return nil , err
112137 }
113138
114139 i .pos ++
0 commit comments