From d51ef02e59e24f7f2b1cfb74666d09c049d1ef83 Mon Sep 17 00:00:00 2001 From: rick olson Date: Wed, 18 Oct 2017 13:51:57 -0600 Subject: [PATCH] move config.FetchPruneConfig -> lfs.FetchPruneConfig --- commands/command_fetch.go | 14 ++++++------- commands/command_prune.go | 8 ++++---- config/config.go | 35 -------------------------------- config/config_test.go | 34 ------------------------------- lfs/config.go | 41 ++++++++++++++++++++++++++++++++++++++ lfs/config_test.go | 42 +++++++++++++++++++++++++++++++++++++++ lfs/lfs.go | 2 +- 7 files changed, 94 insertions(+), 82 deletions(-) create mode 100644 lfs/config.go create mode 100644 lfs/config_test.go diff --git a/commands/command_fetch.go b/commands/command_fetch.go index 8de9b000..d47b431f 100644 --- a/commands/command_fetch.go +++ b/commands/command_fetch.go @@ -66,6 +66,7 @@ func fetchCommand(cmd *cobra.Command, args []string) { defer gitscanner.Close() include, exclude := getIncludeExcludeArgs(cmd) + fetchPruneCfg := lfs.NewFetchPruneConfig(cfg.Git) if fetchAllArg { if fetchRecentArg || len(args) > 1 { @@ -89,17 +90,16 @@ func fetchCommand(cmd *cobra.Command, args []string) { success = success && s } - if fetchRecentArg || cfg.FetchPruneConfig().FetchRecentAlways { - s := fetchRecent(refs, filter) + if fetchRecentArg || fetchPruneCfg.FetchRecentAlways { + s := fetchRecent(fetchPruneCfg, refs, filter) success = success && s } } if fetchPruneArg { - fetchconf := cfg.FetchPruneConfig() - verify := fetchconf.PruneVerifyRemoteAlways + verify := fetchPruneCfg.PruneVerifyRemoteAlways // no dry-run or verbose options in fetch, assume false - prune(fetchconf, verify, false, false) + prune(fetchPruneCfg, verify, false, false) } if !success { @@ -169,9 +169,7 @@ func fetchPreviousVersions(ref string, since time.Time, filter *filepathfilter.F } // Fetch recent objects based on config -func fetchRecent(alreadyFetchedRefs []*git.Ref, filter *filepathfilter.Filter) bool { - fetchconf := cfg.FetchPruneConfig() - +func fetchRecent(fetchconf lfs.FetchPruneConfig, alreadyFetchedRefs []*git.Ref, filter *filepathfilter.Filter) bool { if fetchconf.FetchRecentRefsDays == 0 && fetchconf.FetchRecentCommitsDays == 0 { return true } diff --git a/commands/command_prune.go b/commands/command_prune.go index f71ee7aa..80094fa5 100644 --- a/commands/command_prune.go +++ b/commands/command_prune.go @@ -32,7 +32,7 @@ func pruneCommand(cmd *cobra.Command, args []string) { Exit("Cannot specify both --verify-remote and --no-verify-remote") } - fetchPruneConfig := cfg.FetchPruneConfig() + fetchPruneConfig := lfs.NewFetchPruneConfig(cfg.Git) verify := !pruneDoNotVerifyArg && (fetchPruneConfig.PruneVerifyRemoteAlways || pruneVerifyArg) prune(fetchPruneConfig, verify, pruneDryRunArg, pruneVerboseArg) @@ -53,7 +53,7 @@ type PruneProgress struct { } type PruneProgressChan chan PruneProgress -func prune(fetchPruneConfig config.FetchPruneConfig, verifyRemote, dryRun, verbose bool) { +func prune(fetchPruneConfig lfs.FetchPruneConfig, verifyRemote, dryRun, verbose bool) { localObjects := make([]localstorage.Object, 0, 100) retainedObjects := tools.NewStringSetWithCapacity(100) var reachableObjects tools.StringSet @@ -345,7 +345,7 @@ func pruneTaskGetPreviousVersionsOfRef(gitscanner *lfs.GitScanner, ref string, s } // Background task, must call waitg.Done() once at end -func pruneTaskGetRetainedCurrentAndRecentRefs(gitscanner *lfs.GitScanner, fetchconf config.FetchPruneConfig, retainChan chan string, errorChan chan error, waitg *sync.WaitGroup) { +func pruneTaskGetRetainedCurrentAndRecentRefs(gitscanner *lfs.GitScanner, fetchconf lfs.FetchPruneConfig, retainChan chan string, errorChan chan error, waitg *sync.WaitGroup) { defer waitg.Done() // We actually increment the waitg in this func since we kick off sub-goroutines @@ -399,7 +399,7 @@ func pruneTaskGetRetainedCurrentAndRecentRefs(gitscanner *lfs.GitScanner, fetchc } // Background task, must call waitg.Done() once at end -func pruneTaskGetRetainedUnpushed(gitscanner *lfs.GitScanner, fetchconf config.FetchPruneConfig, retainChan chan string, errorChan chan error, waitg *sync.WaitGroup) { +func pruneTaskGetRetainedUnpushed(gitscanner *lfs.GitScanner, fetchconf lfs.FetchPruneConfig, retainChan chan string, errorChan chan error, waitg *sync.WaitGroup) { defer waitg.Done() err := gitscanner.ScanUnpushed(fetchconf.PruneRemoteName, func(p *lfs.WrappedPointer, err error) { diff --git a/config/config.go b/config/config.go index 839df4fc..789e1479 100644 --- a/config/config.go +++ b/config/config.go @@ -22,27 +22,6 @@ var ( gitConfigWarningPrefix = "lfs." ) -// FetchPruneConfig collects together the config options that control fetching and pruning -type FetchPruneConfig struct { - // The number of days prior to current date for which (local) refs other than HEAD - // will be fetched with --recent (default 7, 0 = only fetch HEAD) - FetchRecentRefsDays int `git:"lfs.fetchrecentrefsdays"` - // Makes the FetchRecentRefsDays option apply to remote refs from fetch source as well (default true) - FetchRecentRefsIncludeRemotes bool `git:"lfs.fetchrecentremoterefs"` - // number of days prior to latest commit on a ref that we'll fetch previous - // LFS changes too (default 0 = only fetch at ref) - FetchRecentCommitsDays int `git:"lfs.fetchrecentcommitsdays"` - // Whether to always fetch recent even without --recent - FetchRecentAlways bool `git:"lfs.fetchrecentalways"` - // Number of days added to FetchRecent*; data outside combined window will be - // deleted when prune is run. (default 3) - PruneOffsetDays int `git:"lfs.pruneoffsetdays"` - // Always verify with remote before pruning - PruneVerifyRemoteAlways bool `git:"lfs.pruneverifyremotealways"` - // Name of remote to check for unpushed and verify checks - PruneRemoteName string `git:"lfs.pruneremotetocheck"` -} - type Configuration struct { // Os provides a `*Environment` used to access to the system's // environment through os.Getenv. It is the point of entry for all @@ -259,20 +238,6 @@ func (c *Configuration) SortedExtensions() ([]Extension, error) { return SortExtensions(c.Extensions()) } -func (c *Configuration) FetchPruneConfig() FetchPruneConfig { - f := &FetchPruneConfig{ - FetchRecentRefsDays: 7, - FetchRecentRefsIncludeRemotes: true, - PruneOffsetDays: 3, - PruneRemoteName: "origin", - } - - if err := c.Unmarshal(f); err != nil { - panic(err.Error()) - } - return *f -} - func (c *Configuration) SkipDownloadErrors() bool { return c.Os.Bool("GIT_LFS_SKIP_DOWNLOAD_ERRORS", false) || c.Git.Bool("lfs.skipdownloaderrors", false) } diff --git a/config/config_test.go b/config/config_test.go index 390f5b48..64b01293 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -97,40 +97,6 @@ func TestLoadInvalidExtension(t *testing.T) { assert.Equal(t, 0, ext.Priority) } -func TestFetchPruneConfigDefault(t *testing.T) { - cfg := NewFrom(Values{}) - fp := cfg.FetchPruneConfig() - - assert.Equal(t, 7, fp.FetchRecentRefsDays) - assert.Equal(t, 0, fp.FetchRecentCommitsDays) - assert.Equal(t, 3, fp.PruneOffsetDays) - assert.True(t, fp.FetchRecentRefsIncludeRemotes) - assert.Equal(t, 3, fp.PruneOffsetDays) - assert.Equal(t, "origin", fp.PruneRemoteName) - assert.False(t, fp.PruneVerifyRemoteAlways) - -} -func TestFetchPruneConfigCustom(t *testing.T) { - cfg := NewFrom(Values{ - Git: map[string][]string{ - "lfs.fetchrecentrefsdays": []string{"12"}, - "lfs.fetchrecentremoterefs": []string{"false"}, - "lfs.fetchrecentcommitsdays": []string{"9"}, - "lfs.pruneoffsetdays": []string{"30"}, - "lfs.pruneverifyremotealways": []string{"true"}, - "lfs.pruneremotetocheck": []string{"upstream"}, - }, - }) - fp := cfg.FetchPruneConfig() - - assert.Equal(t, 12, fp.FetchRecentRefsDays) - assert.Equal(t, 9, fp.FetchRecentCommitsDays) - assert.False(t, fp.FetchRecentRefsIncludeRemotes) - assert.Equal(t, 30, fp.PruneOffsetDays) - assert.Equal(t, "upstream", fp.PruneRemoteName) - assert.True(t, fp.PruneVerifyRemoteAlways) -} - func TestFetchIncludeExcludesAreCleaned(t *testing.T) { cfg := NewFrom(Values{ Git: map[string][]string{ diff --git a/lfs/config.go b/lfs/config.go new file mode 100644 index 00000000..62adc421 --- /dev/null +++ b/lfs/config.go @@ -0,0 +1,41 @@ +package lfs + +import "github.com/git-lfs/git-lfs/config" + +// FetchPruneConfig collects together the config options that control fetching and pruning +type FetchPruneConfig struct { + // The number of days prior to current date for which (local) refs other than HEAD + // will be fetched with --recent (default 7, 0 = only fetch HEAD) + FetchRecentRefsDays int + // Makes the FetchRecentRefsDays option apply to remote refs from fetch source as well (default true) + FetchRecentRefsIncludeRemotes bool + // number of days prior to latest commit on a ref that we'll fetch previous + // LFS changes too (default 0 = only fetch at ref) + FetchRecentCommitsDays int + // Whether to always fetch recent even without --recent + FetchRecentAlways bool + // Number of days added to FetchRecent*; data outside combined window will be + // deleted when prune is run. (default 3) + PruneOffsetDays int + // Always verify with remote before pruning + PruneVerifyRemoteAlways bool + // Name of remote to check for unpushed and verify checks + PruneRemoteName string +} + +func NewFetchPruneConfig(git config.Environment) FetchPruneConfig { + pruneRemote, _ := git.Get("lfs.pruneremotetocheck") + if len(pruneRemote) == 0 { + pruneRemote = "origin" + } + + return FetchPruneConfig{ + FetchRecentRefsDays: git.Int("lfs.fetchrecentrefsdays", 7), + FetchRecentRefsIncludeRemotes: git.Bool("lfs.fetchrecentremoterefs", true), + FetchRecentCommitsDays: git.Int("lfs.fetchrecentcommitsdays", 0), + FetchRecentAlways: git.Bool("lfs.fetchrecentalways", false), + PruneOffsetDays: git.Int("lfs.pruneoffsetdays", 3), + PruneVerifyRemoteAlways: git.Bool("lfs.pruneverifyremotealways", false), + PruneRemoteName: pruneRemote, + } +} diff --git a/lfs/config_test.go b/lfs/config_test.go new file mode 100644 index 00000000..6cf2e871 --- /dev/null +++ b/lfs/config_test.go @@ -0,0 +1,42 @@ +package lfs + +import ( + "testing" + + "github.com/git-lfs/git-lfs/config" + "github.com/stretchr/testify/assert" +) + +func TestFetchPruneConfigDefault(t *testing.T) { + cfg := config.NewFrom(config.Values{}) + fp := NewFetchPruneConfig(cfg.Git) + + assert.Equal(t, 7, fp.FetchRecentRefsDays) + assert.Equal(t, 0, fp.FetchRecentCommitsDays) + assert.Equal(t, 3, fp.PruneOffsetDays) + assert.True(t, fp.FetchRecentRefsIncludeRemotes) + assert.Equal(t, 3, fp.PruneOffsetDays) + assert.Equal(t, "origin", fp.PruneRemoteName) + assert.False(t, fp.PruneVerifyRemoteAlways) +} + +func TestFetchPruneConfigCustom(t *testing.T) { + cfg := config.NewFrom(config.Values{ + Git: map[string][]string{ + "lfs.fetchrecentrefsdays": []string{"12"}, + "lfs.fetchrecentremoterefs": []string{"false"}, + "lfs.fetchrecentcommitsdays": []string{"9"}, + "lfs.pruneoffsetdays": []string{"30"}, + "lfs.pruneverifyremotealways": []string{"true"}, + "lfs.pruneremotetocheck": []string{"upstream"}, + }, + }) + fp := NewFetchPruneConfig(cfg.Git) + + assert.Equal(t, 12, fp.FetchRecentRefsDays) + assert.Equal(t, 9, fp.FetchRecentCommitsDays) + assert.False(t, fp.FetchRecentRefsIncludeRemotes) + assert.Equal(t, 30, fp.PruneOffsetDays) + assert.Equal(t, "upstream", fp.PruneRemoteName) + assert.True(t, fp.PruneVerifyRemoteAlways) +} diff --git a/lfs/lfs.go b/lfs/lfs.go index 8d7480f2..e6209f63 100644 --- a/lfs/lfs.go +++ b/lfs/lfs.go @@ -78,7 +78,7 @@ func Environ(cfg *config.Configuration, manifest *tq.Manifest) []string { ultransfers := manifest.GetUploadAdapterNames() sort.Strings(ultransfers) - fetchPruneConfig := cfg.FetchPruneConfig() + fetchPruneConfig := NewFetchPruneConfig(cfg.Git) storageConfig := localstorage.NewConfig(cfg.Git) env = append(env,