aviator-co / av

A command line tool to manage stacked PRs with Aviator
https://aviator.co
MIT License
265 stars 21 forks source link

av stack submit incorrectly picks base branch #77

Open abatilo opened 1 year ago

abatilo commented 1 year ago

I have a repo whose base branch is main instead of master and when I try to submit I get the following output:

⇒  av stack submit
Creating pull request for branch main:
  - pushing to origin/main
error: failed to determine commits to include in PR: git rev-list: exit status 128

Which, when I enable debug logging, appears to be because av is calling git rev-list --reverse master..HEAD instead of main..HEAD

⇒  av stack submit --debug
DEBU[0000] enabled debug logging                         av_version=v0.0.11
DEBU[0000] git [rev-parse --git-dir]                     duration=19.341208ms repo=ops
DEBU[0000] loaded Git repo                               git_dir=.git
DEBU[0000] no configuration found
DEBU[0000] loaded repository metadata: {ID:MDEwOlJlcG9zaXRvcnk1OTI0MzM3Mw== Owner:color Name:ops}
DEBU[0000] git [for-each-ref --format %(refname)%00%(objecttype)%00%(objectname)%00%(upstream)%00%(upstream:track) refs/av/branch-metadata/**]  duration=21.988209ms repo=ops
DEBU[0000] found branch metadata refs                    refs="[{refs/av/branch-metadata/Import-datadog-namespace blob 3ea795847987533e9f34513c14d5dd67b2f0cd81  } {refs/av/branch-metadata/main blob ce210334cfe0357c59e73f869dd9d60fe6449269  }]"
DEBU[0000] git [cat-file --batch]                        repo=ops
DEBU[0000] parsed branch metadata: {"parent":{"name":"main","head":"6909b238020139f5f811262c1ffbd3a41352d757"}} => meta.data{BranchAlias:meta.BranchAlias{Name:"Import-datadog-namespace", Parent:meta.BranchState{Name:"", Trunk:false, Head:""}, Children:[]string(nil), PullRequest:(*meta.PullRequest)(nil), MergeCommit:""}, Parent:json.RawMessage{0x7b, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x68, 0x65, 0x61, 0x64, 0x22, 0x3a, 0x22, 0x36, 0x39, 0x30, 0x39, 0x62, 0x32, 0x33, 0x38, 0x30, 0x32, 0x30, 0x31, 0x33, 0x39, 0x66, 0x35, 0x66, 0x38, 0x31, 0x31, 0x32, 0x36, 0x32, 0x63, 0x31, 0x66, 0x66, 0x62, 0x64, 0x33, 0x61, 0x34, 0x31, 0x33, 0x35, 0x32, 0x64, 0x37, 0x35, 0x37, 0x22, 0x7d}} &meta.Branch{Name:"Import-datadog-namespace", Parent:meta.BranchState{Name:"main", Trunk:false, Head:"6909b238020139f5f811262c1ffbd3a41352d757"}, Children:[]string(nil), PullRequest:(*meta.PullRequest)(nil), MergeCommit:""}
DEBU[0000] parsed branch metadata: {"parent":{"name":"master","trunk":true},"children":["Import-datadog-namespace"]} => meta.data{BranchAlias:meta.BranchAlias{Name:"main", Parent:meta.BranchState{Name:"", Trunk:false, Head:""}, Children:[]string{"Import-datadog-namespace"}, PullRequest:(*meta.PullRequest)(nil), MergeCommit:""}, Parent:json.RawMessage{0x7b, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x22, 0x2c, 0x22, 0x74, 0x72, 0x75, 0x6e, 0x6b, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65, 0x7d}} &meta.Branch{Name:"main", Parent:meta.BranchState{Name:"master", Trunk:true, Head:""}, Children:[]string{"Import-datadog-namespace"}, PullRequest:(*meta.PullRequest)(nil), MergeCommit:""}
DEBU[0000] git [symbolic-ref --short HEAD]               duration=20.74175ms repo=ops
DEBU[0000] finding subsequent branches for "Import-datadog-namespace"
Creating pull request for branch main:
DEBU[0000] git [rev-parse --symbolic-full-name main@{u}]  duration=19.571708ms repo=ops
DEBU[0000] pushing latest changes                        upstream=origin/main
  - pushing to origin/main
DEBU[0001] git [push --force-with-lease]                 duration=1.205279583s repo=ops
DEBU[0001] git [cat-file blob refs/av/branch-metadata/main]  duration=22.642417ms repo=ops
DEBU[0001] parsed branch metadata: {"parent":{"name":"master","trunk":true},"children":["Import-datadog-namespace"]} => meta.data{BranchAlias:meta.BranchAlias{Name:"main", Parent:meta.BranchState{Name:"", Trunk:false, Head:""}, Children:[]string{"Import-datadog-namespace"}, PullRequest:(*meta.PullRequest)(nil), MergeCommit:""}, Parent:json.RawMessage{0x7b, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x22, 0x2c, 0x22, 0x74, 0x72, 0x75, 0x6e, 0x6b, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65, 0x7d}} &meta.Branch{Name:"main", Parent:meta.BranchState{Name:"master", Trunk:true, Head:""}, Children:[]string{"Import-datadog-namespace"}, PullRequest:(*meta.PullRequest)(nil), MergeCommit:""}
DEBU[0001] base branch is a trunk branch                 base=master
DEBU[0001] git [rev-list --reverse master..HEAD] failed: exit status 128: fatal: ambiguous argument 'master..HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'  duration=20.581042ms repo=ops
DEBU[0001] command exited                                duration=1.381650834s
DEBU[0001] fetched latest released version               latest=v0.0.11
error: failed to determine commits to include in PR: git rev-list: exit status 128
        exit status 128
        git rev-list
        github.com/aviator-co/av/internal/git.(*Repo).Git
                /home/runner/work/av/av/internal/git/git.go:68
        github.com/aviator-co/av/internal/actions.CreatePullRequest
                /home/runner/work/av/av/internal/actions/pr.go:161
        main.glob..func10
                /home/runner/work/av/av/cmd/av/stack_submit.go:57
        github.com/spf13/cobra.(*Command).execute
                /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.5.0/command.go:872
        github.com/spf13/cobra.(*Command).ExecuteC
                /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.5.0/command.go:990
        github.com/spf13/cobra.(*Command).Execute
                /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.5.0/command.go:918
        main.main
                /home/runner/work/av/av/cmd/av/main.go:115
        runtime.main
                /opt/hostedtoolcache/go/1.18.10/x64/src/runtime/proc.go:250
        runtime.goexit
                /opt/hostedtoolcache/go/1.18.10/x64/src/runtime/asm_arm64.s:1270
        failed to determine commits to include in PR
draftcode commented 1 year ago

I can see there are two things that went wrong:

draftcode commented 1 year ago

Actually, I feel like the repository has master and main branches at the same time. Can you check that?

abatilo commented 1 year ago

Actually, I feel like the repository has master and main branches at the same time. Can you check that?

@draftcode sorry for the delay. It does not appear that this is the case:

⇒  git branch -a | rg "(main|master)"
  main
  remotes/origin/ecr-describeimages-pipeline-master
  remotes/origin/main
  remotes/origin/ryanking/tf-run-all-on-master

This was a repo that USED to have master as the default branch but we switched it. Could that be causing a problem?

abatilo commented 1 year ago

Ohhhhh interesting. @draftcode. Look at the output of:

⇒  av stack tree
master
    main
        Import-datadog-namespace
            Reference-namespace-resource-for-pg-secret

For some reason, master is still in the av tree? I don't know how this is possible when there isn't a master branch at all.

abatilo commented 1 year ago

I tracked the problem down to this line: https://github.com/aviator-co/av/blob/9b993bb53f138f6fe1af37605dacbbabd4f90ae7/internal/git/git.go#L41

If you have a repo that had a previous default branch name. Like in my case our repo used to have a default branch named master, and then you switch to main, the output of the command git symbolic-ref refs/remotes/origin/HEAD will still point to the original branch name.

If I run the below command:

⇒  git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
⇒  git symbolic-ref refs/remotes/origin/HEAD
refs/remotes/origin/main

Then you can see that my symbolic-ref has been updated correctly also.

Maybe this should have a check for this error case? Thanks!