| 34 | } |
| 35 | |
| 36 | func NewCmdSync(f *cmdutil.Factory, runF func(*SyncOptions) error) *cobra.Command { |
| 37 | opts := SyncOptions{ |
| 38 | HttpClient: f.HttpClient, |
| 39 | IO: f.IOStreams, |
| 40 | BaseRepo: f.BaseRepo, |
| 41 | Remotes: f.Remotes, |
| 42 | Git: &gitExecuter{client: f.GitClient}, |
| 43 | } |
| 44 | |
| 45 | cmd := &cobra.Command{ |
| 46 | Use: "sync [<destination-repository>]", |
| 47 | Short: "Sync a repository", |
| 48 | Long: heredoc.Docf(` |
| 49 | Sync destination repository from source repository. Syncing uses the default branch |
| 50 | of the source repository to update the matching branch on the destination |
| 51 | repository so they are equal. A fast forward update will be used except when the |
| 52 | %[1]s--force%[1]s flag is specified, then the two branches will |
| 53 | be synced using a hard reset. |
| 54 | |
| 55 | Without an argument, the local repository is selected as the destination repository. |
| 56 | |
| 57 | The source repository is the parent of the destination repository by default. |
| 58 | This can be overridden with the %[1]s--source%[1]s flag. |
| 59 | `, "`"), |
| 60 | Example: heredoc.Doc(` |
| 61 | # Sync local repository from remote parent |
| 62 | $ gh repo sync |
| 63 | |
| 64 | # Sync local repository from remote parent on specific branch |
| 65 | $ gh repo sync --branch v1 |
| 66 | |
| 67 | # Sync remote fork from its parent |
| 68 | $ gh repo sync owner/cli-fork |
| 69 | |
| 70 | # Sync remote repository from another remote repository |
| 71 | $ gh repo sync owner/repo --source owner2/repo2 |
| 72 | `), |
| 73 | Args: cobra.MaximumNArgs(1), |
| 74 | RunE: func(c *cobra.Command, args []string) error { |
| 75 | if len(args) > 0 { |
| 76 | opts.DestArg = args[0] |
| 77 | } |
| 78 | if runF != nil { |
| 79 | return runF(&opts) |
| 80 | } |
| 81 | return syncRun(&opts) |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | cmd.Flags().StringVarP(&opts.SrcArg, "source", "s", "", "Source repository") |
| 86 | cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Branch to sync (default [default branch])") |
| 87 | cmd.Flags().BoolVarP(&opts.Force, "force", "", false, "Hard reset the branch of the destination repository to match the source repository") |
| 88 | return cmd |
| 89 | } |
| 90 | |
| 91 | func syncRun(opts *SyncOptions) error { |
| 92 | if opts.DestArg == "" { |