1package commands
2
3import (
4 "errors"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/bug"
8 "github.com/MichaelMure/git-bug/cache"
9 "github.com/MichaelMure/git-bug/util/interrupt"
10 "github.com/spf13/cobra"
11)
12
13func runPull(cmd *cobra.Command, args []string) error {
14 if len(args) > 1 {
15 return errors.New("Only pulling from one remote at a time is supported")
16 }
17
18 remote := "origin"
19 if len(args) == 1 {
20 remote = args[0]
21 }
22
23 backend, err := cache.NewRepoCache(repo)
24 if err != nil {
25 return err
26 }
27 defer backend.Close()
28 interrupt.RegisterCleaner(backend.Close)
29
30 fmt.Println("Fetching remote ...")
31
32 stdout, err := backend.Fetch(remote)
33 if err != nil {
34 return err
35 }
36
37 fmt.Println(stdout)
38
39 fmt.Println("Merging data ...")
40
41 for merge := range backend.MergeAll(remote) {
42 if merge.Err != nil {
43 fmt.Println(merge.Err)
44 }
45
46 if merge.Status != bug.MergeStatusNothing {
47 fmt.Printf("%s: %s\n", bug.FormatHumanID(merge.Id), merge)
48 }
49 }
50
51 return nil
52}
53
54// showCmd defines the "push" subcommand.
55var pullCmd = &cobra.Command{
56 Use: "pull [<remote>]",
57 Short: "Pull bugs update from a git remote.",
58 PreRunE: loadRepo,
59 RunE: runPull,
60}
61
62func init() {
63 RootCmd.AddCommand(pullCmd)
64}