git: fix the local/remote madness finally I think.

This commit is contained in:
fiatjaf
2025-11-23 14:50:19 -03:00
parent 79c1a70683
commit 68e49fa6e5

77
git.go
View File

@@ -208,7 +208,7 @@ var gitInit = &cli.Command{
} }
// check existing git remotes // check existing git remotes
remote, _, _, err := getGitNostrRemote(c) remote, _, _, err := getGitNostrRemote(c, false)
if err != nil { if err != nil {
remoteURL := fmt.Sprintf("nostr://%s/%s/%s", remoteURL := fmt.Sprintf("nostr://%s/%s/%s",
nip19.EncodeNpub(owner), config.GraspServers[0], config.Identifier) nip19.EncodeNpub(owner), config.GraspServers[0], config.Identifier)
@@ -474,7 +474,7 @@ var gitPush = &cli.Command{
} }
// get git remotes // get git remotes
nostrRemote, localBranch, remoteBranch, err := getGitNostrRemote(c) nostrRemote, localBranch, remoteBranch, err := getGitNostrRemote(c, true)
if err != nil { if err != nil {
return err return err
} }
@@ -620,7 +620,7 @@ var gitAnnounce = &cli.Command{
} }
// get git remotes // get git remotes
nostrRemote, _, _, err := getGitNostrRemote(c) nostrRemote, _, _, err := getGitNostrRemote(c, false)
if err != nil { if err != nil {
return err return err
} }
@@ -722,10 +722,10 @@ var gitAnnounce = &cli.Command{
}, },
} }
func getGitNostrRemote(c *cli.Command) ( func getGitNostrRemote(c *cli.Command, isPush bool) (
remote nostrRemote, remote nostrRemote,
sourceBranch string, localBranch string,
targetBranch string, remoteBranch string,
err error, err error,
) { ) {
// remote // remote
@@ -742,6 +742,7 @@ func getGitNostrRemote(c *cli.Command) (
return remote, "", "", fmt.Errorf("failed to get current branch: %w", err) return remote, "", "", fmt.Errorf("failed to get current branch: %w", err)
} }
branch := strings.TrimSpace(string(output)) branch := strings.TrimSpace(string(output))
// get remote for branch // get remote for branch
cmd = exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) cmd = exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.remote", branch))
output, err = cmd.Output() output, err = cmd.Output()
@@ -765,17 +766,25 @@ func getGitNostrRemote(c *cli.Command) (
// branch (src and dst) // branch (src and dst)
if args.Len() > 1 { if args.Len() > 1 {
var src, dst string
branchSpec := args.Get(1) branchSpec := args.Get(1)
if strings.Contains(branchSpec, ":") { if strings.Contains(branchSpec, ":") {
parts := strings.Split(branchSpec, ":") parts := strings.Split(branchSpec, ":")
if len(parts) == 2 { if len(parts) == 2 {
sourceBranch = parts[0] src = parts[0]
targetBranch = parts[1] dst = parts[1]
} else { } else {
return remote, "", "", fmt.Errorf("invalid branch spec: %s", branchSpec) return remote, "", "", fmt.Errorf("invalid branch spec: %s", branchSpec)
} }
} else { } else {
sourceBranch = branchSpec src = branchSpec
}
if isPush {
localBranch = src
remoteBranch = dst
} else {
localBranch = dst
remoteBranch = src
} }
} else { } else {
// get current branch // get current branch
@@ -784,30 +793,39 @@ func getGitNostrRemote(c *cli.Command) (
if err != nil { if err != nil {
return remote, "", "", fmt.Errorf("failed to get current branch: %w", err) return remote, "", "", fmt.Errorf("failed to get current branch: %w", err)
} }
sourceBranch = strings.TrimSpace(string(output)) localBranch = strings.TrimSpace(string(output))
} }
// get the target branch from git config // get the target branch from git config
cmd = exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.merge", sourceBranch)) if isPush {
output, err = cmd.Output() cmd = exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.merge", localBranch))
if err == nil { output, err = cmd.Output()
// parse refs/heads/<branch-name> to get just the branch name if err == nil {
mergeRef := strings.TrimSpace(string(output)) // parse refs/heads/<branch-name> to get just the branch name
if strings.HasPrefix(mergeRef, "refs/heads/") { mergeRef := strings.TrimSpace(string(output))
targetBranch = strings.TrimPrefix(mergeRef, "refs/heads/") if strings.HasPrefix(mergeRef, "refs/heads/") {
} else { remoteBranch = strings.TrimPrefix(mergeRef, "refs/heads/")
// fallback if it's not in expected format } else {
targetBranch = sourceBranch // fallback if it's not in expected format
remoteBranch = localBranch
}
}
if remoteBranch == "" {
// no upstream configured, assume same branch name
remoteBranch = localBranch
} }
} else { } else {
// no upstream configured, assume same branch name if localBranch == "" {
targetBranch = sourceBranch // no local branch configured, assume same branch name
localBranch = remoteBranch
}
} }
// parse remote // parse remote
remote, err = parseRemote(remoteURL) remote, err = parseRemote(remoteURL)
return remote, sourceBranch, targetBranch, err return remote, localBranch, remoteBranch, err
} }
func parseRemote(remoteURL string) (remote nostrRemote, err error) { func parseRemote(remoteURL string) (remote nostrRemote, err error) {
@@ -920,9 +938,7 @@ func gitFetchInternal(
return state, "", "", err return state, "", "", err
} }
nostrRemote, sourceBranch, targetBranch, err := getGitNostrRemote(c) nostrRemote, localBranch, remoteBranch, err := getGitNostrRemote(c, false)
localBranch = targetBranch
remoteBranch = sourceBranch
if err != nil { if err != nil {
return state, localBranch, remoteBranch, err return state, localBranch, remoteBranch, err
} }
@@ -945,7 +961,7 @@ func gitFetchInternal(
args := []string{"fetch", cloneURL} args := []string{"fetch", cloneURL}
if remoteBranch != "" { if remoteBranch != "" {
// fetch specific branch when refspec is provided // fetch specific branch when refspec is provided
refspec := fmt.Sprintf("%s:%s", remoteBranch, remoteBranch) refspec := fmt.Sprintf("%s:%s", remoteBranch, localBranch)
args = append(args, refspec) args = append(args, refspec)
} }
args = append(args, "--update-head-ok") args = append(args, "--update-head-ok")
@@ -1050,6 +1066,9 @@ func fetchRepositoryAndState(
func readNip34ConfigFile(baseDir string) (Nip34Config, error) { func readNip34ConfigFile(baseDir string) (Nip34Config, error) {
var localConfig Nip34Config var localConfig Nip34Config
// TODO: the baseDir should inspect parents until we reach the directory that has the ".git"
data, err := os.ReadFile(filepath.Join(baseDir, "nip34.json")) data, err := os.ReadFile(filepath.Join(baseDir, "nip34.json"))
if err != nil { if err != nil {
return localConfig, fmt.Errorf("failed to read nip34.json: %w (run 'nak git init' first)", err) return localConfig, fmt.Errorf("failed to read nip34.json: %w (run 'nak git init' first)", err)
@@ -1061,6 +1080,8 @@ func readNip34ConfigFile(baseDir string) (Nip34Config, error) {
} }
func excludeNip34ConfigFile() { func excludeNip34ConfigFile() {
// TODO: inspect parents until we reach the directory that has the ".git"
excludePath := ".git/info/exclude" excludePath := ".git/info/exclude"
excludeContent, err := os.ReadFile(excludePath) excludeContent, err := os.ReadFile(excludePath)
if err != nil { if err != nil {
@@ -1084,6 +1105,8 @@ func excludeNip34ConfigFile() {
} }
func writeNip34ConfigFile(baseDir string, cfg Nip34Config) error { func writeNip34ConfigFile(baseDir string, cfg Nip34Config) error {
// TODO: baseDir should inspect parents until we reach the directory that has the ".git"
data, err := json.MarshalIndent(cfg, "", " ") data, err := json.MarshalIndent(cfg, "", " ")
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal nip34.json: %w", err) return fmt.Errorf("failed to marshal nip34.json: %w", err)