Skip to content

Commit

Permalink
mirror: Add debug for pruning refs
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Gherzan <andrei@gherzan.com>
  • Loading branch information
agherzan committed Jul 25, 2022
1 parent e653925 commit fe2205c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,21 @@ func extraSpecs(repo *git.Repository, refs []*plumbing.Reference) ([]config.RefS

// pruneRemote removes all the references in a remote that are not available in
// the repo.
func pruneRemote(remote *git.Remote, auth transport.AuthMethod, repo *git.Repository) error {
func pruneRemote(conf Config, logger *Logger, remote *git.Remote, auth transport.AuthMethod, repo *git.Repository) error {
refs, err := remote.List(&git.ListOptions{
Auth: auth,
})
if err != nil {
return fmt.Errorf("failed to list the destination remote: %w", err)
}

deleteSpecs, err := extraSpecs(repo, refs)
if err != nil {
return fmt.Errorf("failed to get the prune specs: %w", err)
}
deleteRefs, _ := extraRefs(repo, refs)

deleteSpecs := refsToDeleteSpecs(deleteRefs)

if len(deleteSpecs) > 0 {
logger.Debug(conf.Debug, "Pruning the following refs:", deleteRefs)

err := remote.Push(&git.PushOptions{
RemoteName: remote.Config().Name,
Auth: auth,
Expand All @@ -134,6 +135,8 @@ func pruneRemote(remote *git.Remote, auth transport.AuthMethod, repo *git.Reposi
if err != nil && errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("failed to prune destination: %w", err)
}
} else {
logger.Debug(conf.Debug, "No refs found to prune.")
}

return nil
Expand Down Expand Up @@ -259,7 +262,7 @@ func pushWithAuth(conf Config, logger *Logger, stagingRepo *git.Repository) erro
// with the prunning with a separate push.
logger.Info("Pruning the destination...")

err = pruneRemote(dst, auth, stagingRepo)
err = pruneRemote(conf, logger, dst, auth, stagingRepo)
if err != nil {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ func TestDoMirror(t *testing.T) {
conf := Config{
SrcRepo: srcRepoPath,
DstRepo: dstRepoPath,
Debug: true,
SSH: SSHConf{
PrivateKey: testSSHKey,
KnownHosts: testKnownHost,
Expand Down

0 comments on commit fe2205c

Please sign in to comment.