Handle case when branch not exist
Handle default branch name in push and branch create handlers Don't panic in this case in case the project has multiple configs
This commit is contained in:
@@ -24,7 +24,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -93,94 +92,13 @@ func (refs *GitReferences) addReference(id, branch string) {
|
||||
refs.refs = append(refs.refs, GitReference{Branch: branch, Id: id})
|
||||
}
|
||||
|
||||
func processRefs(gitDir string) ([]GitReference, error) {
|
||||
packedRefsPath := path.Join(gitDir, "packed-refs")
|
||||
stat, err := os.Stat(packedRefsPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if stat.Size() > 10000 || stat.IsDir() {
|
||||
return nil, fmt.Errorf("Funny business with 'packed-refs' in '%s'", gitDir)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(packedRefsPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var references GitReferences
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
if len(line) < 1 || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
splitLine := strings.Split(line, " ")
|
||||
if len(splitLine) != 2 {
|
||||
return nil, fmt.Errorf("Unexpected packaged-refs entry '%#v' in '%s'", splitLine, packedRefsPath)
|
||||
}
|
||||
id, ref := splitLine[0], splitLine[1]
|
||||
const remoteRefPrefix = "refs/remotes/origin/"
|
||||
if ref[0:len(remoteRefPrefix)] != remoteRefPrefix {
|
||||
continue
|
||||
}
|
||||
|
||||
references.addReference(id, ref[len(remoteRefPrefix):])
|
||||
}
|
||||
|
||||
return references.refs, nil
|
||||
}
|
||||
|
||||
func findGitDir(p string) (string, error) {
|
||||
gitFile := path.Join(p, ".git")
|
||||
stat, err := os.Stat(gitFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
return path.Join(p, ".git"), nil
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(gitFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
refs := strings.Split(line, ":")
|
||||
if len(refs) != 2 {
|
||||
return "", fmt.Errorf("Unknown format of .git file: '%s'\n", line)
|
||||
}
|
||||
|
||||
if refs[0] != "gitdir" {
|
||||
return "", fmt.Errorf("Unknown header of .git file: '%s'\n", refs[0])
|
||||
}
|
||||
|
||||
return path.Join(p, strings.TrimSpace(refs[1])), nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Can't find git subdirectory in '%s'", p)
|
||||
}
|
||||
|
||||
func (e *GitHandler) GitBranchHead(gitDir, branchName string) (string, error) {
|
||||
path, err := findGitDir(path.Join(e.GitPath, gitDir))
|
||||
id, err := e.GitExecWithOutput(gitDir, "rev-list", "-1", branchName)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Error identifying gitdir in `%s`: %w", gitDir, err)
|
||||
return "", fmt.Errorf("Can't find default remote branch: %s", branchName)
|
||||
}
|
||||
|
||||
refs, err := processRefs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Error finding branches (%s): %w\n", branchName, err)
|
||||
}
|
||||
|
||||
for _, ref := range refs {
|
||||
if ref.Branch == branchName {
|
||||
return ref.Id, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Can't find default remote branch: %s", branchName)
|
||||
return strings.TrimSpace(id), nil
|
||||
}
|
||||
|
||||
func (e *GitHandler) Close() error {
|
||||
@@ -208,6 +126,11 @@ func (h writeFunc) Close() error {
|
||||
}
|
||||
|
||||
func (e *GitHandler) GitExec(cwd string, params ...string) error {
|
||||
_, err := e.GitExecWithOutput(cwd, params...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *GitHandler) GitExecWithOutput(cwd string, params ...string) (string, error) {
|
||||
cmd := exec.Command("/usr/bin/git", params...)
|
||||
cmd.Env = []string{
|
||||
"GIT_CEILING_DIRECTORIES=" + e.GitPath,
|
||||
@@ -232,10 +155,10 @@ func (e *GitHandler) GitExec(cwd string, params ...string) error {
|
||||
if e.DebugLogger {
|
||||
log.Printf(" *** error: %v\n", err)
|
||||
}
|
||||
return fmt.Errorf("error executing: git %#v \n%s\n err: %w", cmd.Args, out, err)
|
||||
return "", fmt.Errorf("error executing: git %#v \n%s\n err: %w", cmd.Args, out, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
type ChanIO struct {
|
||||
|
@@ -485,3 +485,4 @@ func (gitea *GiteaTransport) GetRecentCommits(org, repo, branch string, commitNo
|
||||
|
||||
return commits.Payload, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user