git-lfs/lfs/gitscanner.go

129 lines
3.8 KiB
Go
Raw Normal View History

2016-11-16 19:56:07 +00:00
package lfs
2016-11-17 22:41:19 +00:00
import (
"fmt"
"sync"
2016-11-17 22:41:19 +00:00
"time"
"github.com/rubyist/tracerx"
2016-11-17 22:41:19 +00:00
)
// GitScanner scans objects in a Git repository for LFS pointers.
2016-11-16 19:56:07 +00:00
type GitScanner struct {
2016-11-16 23:05:03 +00:00
remote string
skippedRefs []string
closed bool
started time.Time
mu sync.Mutex
2016-11-16 19:56:07 +00:00
}
// NewGitScanner initializes a *GitScanner for a Git repository in the current
// working directory.
2016-11-16 19:56:07 +00:00
func NewGitScanner() *GitScanner {
return &GitScanner{started: time.Now()}
}
// Close stops exits once all processing has stopped, and all resources are
// tracked and cleaned up.
func (s *GitScanner) Close() {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return
}
s.closed = true
tracerx.PerformanceSince("scan", s.started)
2016-11-16 19:56:07 +00:00
}
// RemoteForPush sets up this *GitScanner to scan for objects to push to the
// given remote. Needed for ScanLeftToRemote().
func (s *GitScanner) RemoteForPush(r string) error {
s.mu.Lock()
defer s.mu.Unlock()
if len(s.remote) > 0 && s.remote != r {
return fmt.Errorf("Trying to set remote to %q, already set to %q", r, s.remote)
}
s.remote = r
2016-11-16 23:05:03 +00:00
s.skippedRefs = calcSkippedRefs(r)
return nil
}
// ScanLeftToRemote scans through all commits starting at the given ref that the
// given remote does not have. See RemoteForPush().
func (s *GitScanner) ScanLeftToRemote(left string) (*PointerChannelWrapper, error) {
s.mu.Lock()
if len(s.remote) == 0 {
s.mu.Unlock()
return nil, fmt.Errorf("Unable to scan starting at %q: no remote set.", left)
}
s.mu.Unlock()
2016-11-16 20:51:36 +00:00
return scanRefsToChan(left, "", s.opts(ScanLeftToRemoteMode))
}
// ScanRefRange scans through all commits from the given left and right refs,
// including git objects that have been modified or deleted.
func (s *GitScanner) ScanRefRange(left, right string) (*PointerChannelWrapper, error) {
opts := s.opts(ScanRefsMode)
opts.SkipDeletedBlobs = false
2016-11-16 20:51:36 +00:00
return scanRefsToChan(left, right, opts)
}
// ScanRefWithDeleted scans through all objects in the given ref, including
// git objects that have been modified or deleted.
func (s *GitScanner) ScanRefWithDeleted(ref string) (*PointerChannelWrapper, error) {
return s.ScanRefRange(ref, "")
}
// ScanRef scans through all objects in the current ref, excluding git objects
// that have been modified or deleted before the ref.
2016-11-16 20:02:45 +00:00
func (s *GitScanner) ScanRef(ref string) (*PointerChannelWrapper, error) {
opts := s.opts(ScanRefsMode)
2016-11-16 20:02:45 +00:00
opts.SkipDeletedBlobs = true
2016-11-16 20:51:36 +00:00
return scanRefsToChan(ref, "", opts)
2016-11-16 20:02:45 +00:00
}
// ScanAll scans through all objects in the git repository.
2016-11-16 20:02:45 +00:00
func (s *GitScanner) ScanAll() (*PointerChannelWrapper, error) {
opts := s.opts(ScanAllMode)
2016-11-16 19:56:07 +00:00
opts.SkipDeletedBlobs = false
2016-11-16 20:51:36 +00:00
return scanRefsToChan("", "", opts)
2016-11-16 19:56:07 +00:00
}
2016-11-17 22:54:05 +00:00
// ScanTree takes a ref and returns WrappedPointer objects in the tree at that
// ref. Differs from ScanRefs in that multiple files in the tree with the same
// content are all reported.
func (s *GitScanner) ScanTree(ref string) (*PointerChannelWrapper, error) {
return runScanTree(ref)
}
2016-11-17 16:53:01 +00:00
// ScanUnpushed scans history for all LFS pointers which have been added but not
// pushed to the named remote. remote can be left blank to mean 'any remote'.
func (s *GitScanner) ScanUnpushed(remote string) (*PointerChannelWrapper, error) {
return scanUnpushed(remote)
2016-11-17 16:53:01 +00:00
}
2016-11-17 22:41:19 +00:00
// ScanPreviousVersions scans changes reachable from ref (commit) back to since.
// Returns channel of pointers for *previous* versions that overlap that time.
// Does not include pointers which were still in use at ref (use ScanRefsToChan
// for that)
func (s *GitScanner) ScanPreviousVersions(ref string, since time.Time) (*PointerChannelWrapper, error) {
return logPreviousSHAs(ref, since)
}
func (s *GitScanner) opts(mode ScanningMode) *ScanRefsOptions {
s.mu.Lock()
defer s.mu.Unlock()
2016-11-16 20:53:56 +00:00
opts := newScanRefsOptions()
opts.ScanMode = mode
2016-11-16 23:05:03 +00:00
opts.RemoteName = s.remote
opts.skippedRefs = s.skippedRefs
return opts
}