aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-08-25 17:25:31 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-25 17:25:31 +0200
commita3418c5e0a3c6e925b5a4fb3ecb1d3db56408d1a (patch)
tree23ab46cb3269a104d9b69e0d1a1bec3c340df112 /repository.go
parent0f97041639b55bc4631145e2053a47a1eb8cdef0 (diff)
downloadgo-git-a3418c5e0a3c6e925b5a4fb3ecb1d3db56408d1a.tar.gz
Repository: Clone protection if non empty object storage, Remote: NoErrAlreadyUpToDate
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go23
1 files changed, 21 insertions, 2 deletions
diff --git a/repository.go b/repository.go
index 445e90c..fc871c7 100644
--- a/repository.go
+++ b/repository.go
@@ -12,8 +12,9 @@ import (
)
var (
- ErrObjectNotFound = errors.New("object not found")
- ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch")
+ ErrObjectNotFound = errors.New("object not found")
+ ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch")
+ ErrRepositoryNonEmpty = errors.New("repository non empty")
)
// Repository giturl string, auth common.AuthMethod repository struct
@@ -94,6 +95,15 @@ func (r *Repository) DeleteRemote(name string) error {
// Clone clones a remote repository
func (r *Repository) Clone(o *CloneOptions) error {
+ empty, err := r.IsEmpty()
+ if err != nil {
+ return err
+ }
+
+ if !empty {
+ return ErrRepositoryNonEmpty
+ }
+
if err := o.Validate(); err != nil {
return err
}
@@ -166,6 +176,15 @@ func (r *Repository) createReferences(ref *core.Reference) error {
return r.s.ReferenceStorage().Set(head)
}
+// IsEmpty returns true if the repository is empty
+func (r *Repository) IsEmpty() (bool, error) {
+ var count int
+ return count == 0, r.Refs().ForEach(func(r *core.Reference) error {
+ count++
+ return nil
+ })
+}
+
// Pull incorporates changes from a remote repository into the current branch
func (r *Repository) Pull(o *PullOptions) error {
if err := o.Validate(); err != nil {