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