github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/git/objects.go (about)

     1  package git
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  )
     7  
     8  // RemoteSet is a slice of git remotes.
     9  type RemoteSet []*Remote
    10  
    11  func (r RemoteSet) Len() int      { return len(r) }
    12  func (r RemoteSet) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
    13  func (r RemoteSet) Less(i, j int) bool {
    14  	return remoteNameSortScore(r[i].Name) > remoteNameSortScore(r[j].Name)
    15  }
    16  
    17  func remoteNameSortScore(name string) int {
    18  	switch strings.ToLower(name) {
    19  	case "upstream":
    20  		return 3
    21  	case "github":
    22  		return 2
    23  	case "origin":
    24  		return 1
    25  	default:
    26  		return 0
    27  	}
    28  }
    29  
    30  // Remote is a parsed git remote.
    31  type Remote struct {
    32  	Name     string
    33  	Resolved string
    34  	FetchURL *url.URL
    35  	PushURL  *url.URL
    36  }
    37  
    38  func (r *Remote) String() string {
    39  	return r.Name
    40  }
    41  
    42  func NewRemote(name string, u string) *Remote {
    43  	pu, _ := url.Parse(u)
    44  	return &Remote{
    45  		Name:     name,
    46  		FetchURL: pu,
    47  		PushURL:  pu,
    48  	}
    49  }
    50  
    51  // Ref represents a git commit reference.
    52  type Ref struct {
    53  	Hash string
    54  	Name string
    55  }
    56  
    57  // TrackingRef represents a ref for a remote tracking branch.
    58  type TrackingRef struct {
    59  	RemoteName string
    60  	BranchName string
    61  }
    62  
    63  func (r TrackingRef) String() string {
    64  	return "refs/remotes/" + r.RemoteName + "/" + r.BranchName
    65  }
    66  
    67  type Commit struct {
    68  	Sha   string
    69  	Title string
    70  }
    71  
    72  type BranchConfig struct {
    73  	RemoteName string
    74  	RemoteURL  *url.URL
    75  	MergeRef   string
    76  }