1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-26 18:44:27 +00:00
gitea/vendor/gopkg.in/src-d/go-git.v4/storage/filesystem/shallow.go
Lauris BH 08bf443016 Implement git refs API for listing references (branches, tags and other) (#5354)
* Inital routes to git refs api

* Git refs API implementation

* Update swagger

* Fix copyright

* Make swagger happy add basic test

* Fix test

* Fix test again :)
2018-11-27 16:52:20 -05:00

55 lines
1.2 KiB
Go

package filesystem
import (
"bufio"
"fmt"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
"gopkg.in/src-d/go-git.v4/utils/ioutil"
)
// ShallowStorage where the shallow commits are stored, an internal to
// manipulate the shallow file
type ShallowStorage struct {
dir *dotgit.DotGit
}
// SetShallow save the shallows in the shallow file in the .git folder as one
// commit per line represented by 40-byte hexadecimal object terminated by a
// newline.
func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error {
f, err := s.dir.ShallowWriter()
if err != nil {
return err
}
defer ioutil.CheckClose(f, &err)
for _, h := range commits {
if _, err := fmt.Fprintf(f, "%s\n", h); err != nil {
return err
}
}
return err
}
// Shallow return the shallow commits reading from shallo file from .git
func (s *ShallowStorage) Shallow() ([]plumbing.Hash, error) {
f, err := s.dir.Shallow()
if f == nil || err != nil {
return nil, err
}
defer ioutil.CheckClose(f, &err)
var hash []plumbing.Hash
scn := bufio.NewScanner(f)
for scn.Scan() {
hash = append(hash, plumbing.NewHash(scn.Text()))
}
return hash, scn.Err()
}