diff --git a/services.go b/services.go index 1286b98..218a0b3 100644 --- a/services.go +++ b/services.go @@ -69,7 +69,13 @@ func (s *Services) PushRepository() error { sourceBranch := s.cfg.LocalBranch if sourceBranch == "" { - sourceBranch = "main" + // Get the current ref + ref, err := runCmdExpectingOutput("git", "rev-parse", "--abbrev-ref", "HEAD") + if err != nil { + return err + } + + sourceBranch = strings.TrimSpace(ref) } remoteBranch := s.cfg.RemoteBranch if remoteBranch == "" { @@ -96,6 +102,17 @@ func runCmd(cmd ...string) error { return e.Run() } +func runCmdExpectingOutput(cmd ...string) (string, error) { + log.Printf(" .. [exec] %v", strings.Join(cmd, " ")) + e := exec.Command(cmd[0], cmd[1:]...) + e.Stderr = os.Stderr + res, err := e.Output() + if err != nil { + return "", err + } + return string(res), nil +} + func writeFile(path string, content string, mode os.FileMode) error { fullPath := os.ExpandEnv(path) log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content))