When does push() set the upstream tracking branch? · Issue #375 · ropensci/git2r (original) (raw)

The documentation for push() states that it adds an upstream tracking branch (source):

## Push commits from repository to bare repository
## Adds an upstream tracking branch to branch 'master'
push(repo, "origin", "refs/heads/master")

And the unit tests confirm this behavior. Below I've copied line line 64 through line of 68 of tests/push.R followed by a test I added:

## No tracking branch assigned to master
tools::assertError(push(branches(repo)[[1]]))

## Push changes from repo to origin
push(repo, "origin", "refs/heads/master")

## My addition: Confirm that an upstream tracking branch was added
stopifnot(identical(branch_get_upstream(branches(repo)[[1]])$name, "origin/master"))

However, for these unit tests, the remote repository is a local Git repository on the same machine. When I try to replicate this behavior when the remote repository is on GitHub, the upstream tracking branch is not set.

I checked the usethis package, and they also manually set the upstream tracking branch after running git2r::push (source):

    pushed <- tryCatch({
      git2r::push(r, "origin", "refs/heads/master", credentials = cred)
      TRUE
    }, error = function(e) FALSE)
  }
  if (pushed) {
    git2r::branch_set_upstream(git2r::repository_head(r), "origin/master")
  }

What is the expected behavior of git2r::push() in regards to setting the upstream tracking branch? Is it supposed to when the remote repository is hosted on GitHub or another online provider? If not, could the documentation be updated to describes the rules for when it does or doesn't set the upstream tracking branch?

Potentially related: #133