gittuf/internal/cmd/sync/sync.go
Aditya Sirish A Yelgundhalli 3e7e688406
gittuf: Update how sync and propagation workflows interact
Signed-off-by: Aditya Sirish A Yelgundhalli <ayelgundhall@bloomberg.net>
2025-03-05 11:21:29 -05:00

63 lines
1.3 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package sync
import (
"errors"
"fmt"
"github.com/gittuf/gittuf/experimental/gittuf"
"github.com/spf13/cobra"
)
type options struct {
overwriteLocalRefs bool
}
func (o *options) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(
&o.overwriteLocalRefs,
"overwrite",
false,
"overwrite local references with upstream changes",
)
}
func (o *options) Run(cmd *cobra.Command, args []string) error {
repo, err := gittuf.LoadRepository()
if err != nil {
return err
}
remoteName := "origin"
if len(args) > 0 {
remoteName = args[0]
}
divergedRefs, err := repo.Sync(cmd.Context(), remoteName, o.overwriteLocalRefs, true)
switch {
case errors.Is(err, gittuf.ErrDivergedRefs):
fmt.Println("References have diverged:")
for _, refName := range divergedRefs {
fmt.Println(refName)
}
fmt.Println("To apply upstream changes locally, rerun the command with --overwrite. WARNING: this operation may result in the loss of local work!")
return nil
default:
return err
}
}
func New() *cobra.Command {
o := &options{}
cmd := &cobra.Command{
Use: "sync [remoteName]",
Short: "Synchronize local references with remote references based on RSL",
Args: cobra.MaximumNArgs(1),
RunE: o.Run,
}
o.AddFlags(cmd)
return cmd
}