From 5ad0e401a64014adef6d99a780ae69b21563052d Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Sat, 24 Apr 2021 12:34:45 +0000 Subject: [PATCH] feat: automatically checkout and pull main branch Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- maintenance.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/maintenance.go b/maintenance.go index 78895a9..e39de33 100644 --- a/maintenance.go +++ b/maintenance.go @@ -70,10 +70,25 @@ func doMaintenanceOnce(_ context.Context, path string) error { zap.String("current", project.Git.CurrentBranch), zap.String("main", project.Git.MainBranch), ) + mainBranch, err := project.Git.repo.Branch(project.Git.MainBranch) + if err != nil { + return fmt.Errorf("failed to get ref for main branch: %q: %w", project.Git.MainBranch, err) + } - // TODO: checkout - // TODO: pull - return fmt.Errorf("not implemented: git checkout master/main") + err = project.Git.workTree.Checkout(&git.CheckoutOptions{ + Branch: mainBranch.Merge, + }) + if err != nil { + return fmt.Errorf("failed to checkout main branch: %q: %w", project.Git.MainBranch, err) + } + + err = project.Git.workTree.Pull(&git.PullOptions{}) + switch err { + case git.NoErrAlreadyUpToDate: // skip + case nil: // skip + default: + return fmt.Errorf("failed to pull main branch: %q: %w", project.Git.MainBranch, err) + } } // check if the project looks like a one that can be maintained by repoman