Add a main struct, part 6

This commit encapsulates the submodules parameter.
This commit is contained in:
Tim Hockin 2020-11-08 10:06:35 -08:00
parent 8b321e3940
commit 70dd821e7b
1 changed files with 26 additions and 22 deletions

View File

@ -164,10 +164,12 @@ const (
metricKeyNoOp = "noop" metricKeyNoOp = "noop"
) )
type submodulesMode string
const ( const (
submodulesRecursive = "recursive" submodulesRecursive submodulesMode = "recursive"
submodulesShallow = "shallow" submodulesShallow submodulesMode = "shallow"
submodulesOff = "off" submodulesOff submodulesMode = "off"
) )
func init() { func init() {
@ -257,6 +259,7 @@ type repoSync struct {
branch string // remote branch to sync branch string // remote branch to sync
rev string // the rev or SHA to sync rev string // the rev or SHA to sync
depth int // for shallow sync depth int // for shallow sync
submodules submodulesMode // how to handle submodules
} }
func main() { func main() {
@ -308,7 +311,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
switch *flSubmodules { switch submodulesMode(*flSubmodules) {
case submodulesRecursive, submodulesShallow, submodulesOff: case submodulesRecursive, submodulesShallow, submodulesOff:
default: default:
fmt.Fprintf(os.Stderr, "ERROR: --submodules must be one of %q, %q, or %q", submodulesRecursive, submodulesShallow, submodulesOff) fmt.Fprintf(os.Stderr, "ERROR: --submodules must be one of %q, %q, or %q", submodulesRecursive, submodulesShallow, submodulesOff)
@ -443,6 +446,7 @@ func main() {
branch: *flBranch, branch: *flBranch,
rev: *flRev, rev: *flRev,
depth: *flDepth, depth: *flDepth,
submodules: submodulesMode(*flSubmodules),
} }
// This context is used only for git credentials initialization. There are no long-running operations like // This context is used only for git credentials initialization. There are no long-running operations like
@ -536,7 +540,7 @@ func main() {
for { for {
start := time.Now() start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout) ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout)
if changed, hash, err := git.SyncRepo(ctx, *flLink, *flAskPassURL, *flSubmodules); err != nil { if changed, hash, err := git.SyncRepo(ctx, *flLink, *flAskPassURL); err != nil {
updateSyncMetrics(metricKeyError, start) updateSyncMetrics(metricKeyError, start)
if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures { if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures {
// Exit after too many retries, maybe the error is not recoverable. // Exit after too many retries, maybe the error is not recoverable.
@ -678,7 +682,7 @@ func setRepoReady() {
} }
// AddWorktreeAndSwap creates a new worktree and calls UpdateSymlink to swap the symlink to point to the new worktree // AddWorktreeAndSwap creates a new worktree and calls UpdateSymlink to swap the symlink to point to the new worktree
func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, link string, hash string, submoduleMode string) error { func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, link string, hash string) error {
log.V(0).Info("syncing git", "rev", git.rev, "hash", hash) log.V(0).Info("syncing git", "rev", git.rev, "hash", hash)
args := []string{"fetch", "-f", "--tags"} args := []string{"fetch", "-f", "--tags"}
@ -727,10 +731,10 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, link string, hash s
// Update submodules // Update submodules
// NOTE: this works for repo with or without submodules. // NOTE: this works for repo with or without submodules.
if submoduleMode != submodulesOff { if git.submodules != submodulesOff {
log.V(0).Info("updating submodules") log.V(0).Info("updating submodules")
submodulesArgs := []string{"submodule", "update", "--init"} submodulesArgs := []string{"submodule", "update", "--init"}
if submoduleMode == submodulesRecursive { if git.submodules == submodulesRecursive {
submodulesArgs = append(submodulesArgs, "--recursive") submodulesArgs = append(submodulesArgs, "--recursive")
} }
if git.depth != 0 { if git.depth != 0 {
@ -861,7 +865,7 @@ func (git *repoSync) RevIsHash(ctx context.Context) (bool, error) {
// SyncRepo syncs the branch of a given repository to the link at the given rev. // SyncRepo syncs the branch of a given repository to the link at the given rev.
// returns (1) whether a change occured, (2) the new hash, and (3) an error if one happened // returns (1) whether a change occured, (2) the new hash, and (3) an error if one happened
func (git *repoSync) SyncRepo(ctx context.Context, link string, authURL string, submoduleMode string) (bool, string, error) { func (git *repoSync) SyncRepo(ctx context.Context, link string, authURL string) (bool, string, error) {
if authURL != "" { if authURL != "" {
// For ASKPASS Callback URL, the credentials behind is dynamic, it needs to be // For ASKPASS Callback URL, the credentials behind is dynamic, it needs to be
// re-fetched each time. // re-fetched each time.
@ -903,7 +907,7 @@ func (git *repoSync) SyncRepo(ctx context.Context, link string, authURL string,
hash = remote hash = remote
} }
return true, hash, git.AddWorktreeAndSwap(ctx, link, hash, submoduleMode) return true, hash, git.AddWorktreeAndSwap(ctx, link, hash)
} }
// GetRevs returns the local and upstream hashes for rev. // GetRevs returns the local and upstream hashes for rev.