github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/packager/git/common.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package git contains functions for interacting with git repositories.
     5  package git
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/Racer159/jackal/src/pkg/message"
    12  	"github.com/Racer159/jackal/src/types"
    13  	"github.com/go-git/go-git/v5/plumbing"
    14  )
    15  
    16  // Git is the main struct for managing git repositories.
    17  type Git struct {
    18  	// Server is the git server configuration.
    19  	Server types.GitServerInfo
    20  	// Spinner is an optional spinner to use for long running operations.
    21  	Spinner *message.Spinner
    22  	// Target working directory for the git repository.
    23  	GitPath string
    24  }
    25  
    26  const onlineRemoteName = "online-upstream"
    27  const offlineRemoteName = "offline-downstream"
    28  const emptyRef = ""
    29  
    30  // New creates a new git instance with the provided server config.
    31  func New(server types.GitServerInfo) *Git {
    32  	return &Git{
    33  		Server: server,
    34  	}
    35  }
    36  
    37  // NewWithSpinner creates a new git instance with the provided server config and spinner.
    38  func NewWithSpinner(server types.GitServerInfo, spinner *message.Spinner) *Git {
    39  	return &Git{
    40  		Server:  server,
    41  		Spinner: spinner,
    42  	}
    43  }
    44  
    45  // ParseRef parses the provided ref into a ReferenceName if it's not a hash.
    46  func ParseRef(r string) plumbing.ReferenceName {
    47  	// If not a full ref, assume it's a tag at this point.
    48  	if !plumbing.IsHash(r) && !strings.HasPrefix(r, "refs/") {
    49  		r = fmt.Sprintf("refs/tags/%s", r)
    50  	}
    51  
    52  	// Set the reference name to the provided ref.
    53  	return plumbing.ReferenceName(r)
    54  }