usethis icon indicating copy to clipboard operation
usethis copied to clipboard

can `pr_finish()` also delete the local remote-tracking branch?

Open dchiu911 opened this issue 1 year ago • 10 comments

pr_finish()
✔ Switching back to default branch ('master').
✔ Pulling changes from 'origin/master'
✔ Deleting local 'tmp' branch.
✔ PR 'repo_name/test/#1' has been merged, deleting remote branch 'origin/tmp'
system("git branch -rd origin/tmp")
Deleted remote-tracking branch origin/tmp2 (was 4e26abe).

Can we get pr_finish() to also run git branch -rd origin/tmp to delete the local remote-tracking branch or is there a reason that it is kept?

dchiu911 avatar Sep 12 '22 20:09 dchiu911

pr_finish() does delete the local remote-tracking branch in many situations and, in particular, it always does so for me when I think it should. So the fact that it's not doing so for you suggests you are using some unanticipated workflow.

If you want to step through the function that does this to see why it's exiting early for you, here it is:

https://github.com/r-lib/usethis/blob/29062553a7d1ce3f0dcc73c24a3eb13c8600205b/R/pr.R#L524

jennybc avatar Sep 12 '22 21:09 jennybc

I'm trying to write a short reprex of a PR workflow:

library(usethis)
# 1. create branch and push to remote
pr_init(branch = "tmp")
✔ Pulling changes from 'origin/master'.
✔ Creating and switching to local branch 'tmp'.
pr_push()
✔ Pushing local 'tmp' branch to 'origin' remote.
• Create PR at link given below
✔ Opening URL 'https://github.com/repo_name/test/compare/tmp'
# 2. make changes, sync and create PR
pr_pull()
✔ Pulling from 'origin/tmp'.
pr_merge_main()
✔ Pulling changes from 'origin/master'.
pr_push()
✔ Checking that local branch 'tmp' has the changes in 'origin/tmp'
✔ Pushing local 'tmp' branch to 'origin/tmp'.
• Create PR at link given below
✔ Opening URL 'https://github.com/repo_name/test/compare/tmp'
# 3. merge PR and clean up branches
pr_view()
ℹ Current branch ('tmp') is connected to PR #1.
✔ Opening URL 'https://github.com/repo_name/test/pull/1'
pr_finish()
✔ Switching back to default branch ('master').
✔ Pulling changes from 'origin/master'
✔ Deleting local 'tmp' branch.
✔ PR 'repo_name/test/#1' has been merged, deleting remote branch 'origin/tmp'

I reviewed pr_clean() and I'm wondering if the local remote-tracking branch is supposed to be removed at L608. I think L587 removes the local branch and L595 removes the remote branch but not sure where the local remote-tracking branch is removed?

dchiu911 avatar Sep 12 '22 22:09 dchiu911

How can the branch be deleted manually? It doesn't seem to work for local remote-tracking branches:

library(gert)
git_branch_exists("tmp", local = TRUE)
#>[1] TRUE
git_branch_delete("tmp")
git_branch_exists("origin/tmp", local = FALSE)
#> [1] TRUE
git_branch_delete("origin/tmp")
#> Error in libgit2::git_branch_lookup : 
#>   cannot locate local branch 'origin/tmp'

dchiu911 avatar Sep 12 '22 23:09 dchiu911

pr_finish() does delete the local remote-tracking branch

OK, yes, I think what I really mean here is "local branch".

My head is not at all in this place right now. But my hunch is that you're striving for a level of git hygiene that I just don't worry about. If the branch is gone locally and remotely, I move on, even if it has left some cruft behind that the occasional git prune sorts out.

jennybc avatar Sep 13 '22 02:09 jennybc

There is also a known issue where libgit2 (and therefore gert) leaves junk behind in git config when you delete branches and remotes. It's harmless (but regrettable).

jennybc avatar Sep 13 '22 02:09 jennybc

thanks @jennybc it's a really low impact item that I was wondering if usethis had a mechanism to tidy up the tracking branches left behind like below without going to the terminal Screen Shot 2022-09-13 at 10 20 31 AM

dchiu911 avatar Sep 13 '22 17:09 dchiu911

OK so this is about something RStudio does. I have never noticed that before.

jennybc avatar Sep 13 '22 17:09 jennybc

I feel like this has become a gert question. Can you make a pure gert example and ask the question there? I think gert would have to gain a feature for usethis to be able to do this cleanup.

jennybc avatar Sep 13 '22 17:09 jennybc

Well, actually gert::git_fetch(prune = TRUE) does remove this vestigial ref. But it feels a bit presumptuous for us to do that. Something more specific to the branch we're deleting would feel more appropriate.

jennybc avatar Sep 13 '22 17:09 jennybc

This pseudo reprex using gert does accomplish the workflow I'm trying to illustrate. Adding gert::git_fetch(prune = TRUE) at some point to pr_finish() would suffice although we find out afterwards what was pruned when we don't specify the branch.

library(gert)
library(usethis)
library(here)
repo <- here()
git_branch_create("tmp", repo = repo)
git_push(remote = "origin", verbose = FALSE, repo = repo)
cat("- a change", sep = "\n", file = here("src/03-results.Rmd"), append = TRUE)
git_add(files = ".", repo = repo)
#>                 file   status staged
#> 1 src/03-results.Rmd modified   TRUE
git_commit("change")
#> [1] "7c510dac9f596a62f17a3171bc87af2386d21217"
git_push()
#> Looking up https credentials for https://github.com/TalhoukLab/test
#> Transferred 4 of 4 objects...done!
#> [status] refs/heads/tmp: unchanged
#> [updated] 1af19a9d3f..7c510dac9f refs/remotes/origin/tmp

# Optionally, open PR on GitHub

git_branch_checkout(branch = "master")
#> <git repository>: /Users/derekchiu/Documents/Git/test[@master]
git_merge("tmp")
#> Performing fast-forward merge, no commit needed
git_push()
#> Looking up https credentials for https://github.com/TalhoukLab/test
#> Transferred 0 of 0 objects...done!
#> [status] refs/heads/master: unchanged
#> [updated] 1af19a9d3f..7c510dac9f refs/remotes/origin/master

git_branch_delete(branch = "tmp", repo = repo)
gh::gh("DELETE /repos/TalhoukLab/test/git/refs/heads/tmp")
#> raw(0)
#> attr(,"class")
#> [1] "gh_response" "raw"
git_fetch(prune = TRUE)
#> Looking up https credentials for https://github.com/TalhoukLab/test
#> [updated] 8000284792..0000000000 refs/remotes/origin/tmp

dchiu911 avatar Sep 13 '22 18:09 dchiu911