github.com/tsuyoshiwada/git-prout@v0.0.0-20170402150409-5c51421d4bdb/pr.go (about)

     1  package main
     2  
     3  import "strconv"
     4  
     5  // PR pull request
     6  type PR struct {
     7  	Remote string
     8  	Number int
     9  	Ref    string
    10  	Branch string
    11  	Force  bool
    12  }
    13  
    14  // NewPR create new PR instance.
    15  func NewPR(remote string, number int, force bool) PR {
    16  	pr := PR{}
    17  	pr.Remote = remote
    18  	pr.Number = number
    19  	pr.Force = force
    20  	pr.Branch = "pr/" + strconv.Itoa(number)
    21  	pr.Ref = "pull/" + strconv.Itoa(number) + "/head:" + pr.Branch
    22  	return pr
    23  }
    24  
    25  // Fetch PR. (force fetch)
    26  func (p *PR) Fetch() (string, error) {
    27  	return git("fetch", p.Remote, p.Ref, "-f", "-u")
    28  }
    29  
    30  // Checkout to PR branch.
    31  func (p *PR) Checkout() (string, error) {
    32  	args := []string{p.Branch}
    33  	if p.Force {
    34  		args = append(args, []string{"-f"}...)
    35  	}
    36  	return git("checkout", args...)
    37  }
    38  
    39  // Apply PR to the working directory.
    40  func (p *PR) Apply() (string, error) {
    41  	return git("reset", "--hard", "HEAD")
    42  }