| 1 |
package filesystem |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/storage/filesystem/dotgit" |
| 9 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 10 |
) |
| 11 |
|
| 12 |
// ShallowStorage where the shallow commits are stored, an internal to |
| 13 |
// manipulate the shallow file |
| 14 |
type ShallowStorage struct { |
| 15 |
dir *dotgit.DotGit |
| 16 |
} |
| 17 |
|
| 18 |
// SetShallow save the shallows in the shallow file in the .git folder as one |
| 19 |
// commit per line represented by 40-byte hexadecimal object terminated by a |
| 20 |
// newline. |
| 21 |
func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error { |
| 22 |
f, err := s.dir.ShallowWriter() |
| 23 |
if err != nil { |
| 24 |
return err |
| 25 |
} |
| 26 |
|
| 27 |
defer ioutil.CheckClose(f, &err) |
| 28 |
for _, h := range commits { |
| 29 |
if _, err := fmt.Fprintf(f, "%s\n", h); err != nil { |
| 30 |
return err |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return err |
| 35 |
} |
| 36 |
|
| 37 |
// Shallow returns the shallow commits reading from shallo file from .git |
| 38 |
func (s *ShallowStorage) Shallow() ([]plumbing.Hash, error) { |
| 39 |
f, err := s.dir.Shallow() |
| 40 |
if f == nil || err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
|
| 44 |
defer ioutil.CheckClose(f, &err) |
| 45 |
|
| 46 |
var hash []plumbing.Hash |
| 47 |
|
| 48 |
scn := bufio.NewScanner(f) |
| 49 |
for scn.Scan() { |
| 50 |
hash = append(hash, plumbing.NewHash(scn.Text())) |
| 51 |
} |
| 52 |
|
| 53 |
return hash, scn.Err() |
| 54 |
} |