Skip to content

Commit

Permalink
Merge branch 'r-lib:main' into challenge-uncommited-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy authored Oct 2, 2024
2 parents 54e13c1 + b4e8477 commit eec87e6
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 111 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# usethis (development version)

## Bug fixes and minor improvements

* `use_package()` now decreases a package minimum version required when
`min_version` is lower than what is currently specified in the DESCRIPTION
file (@jplecavalier, #1957).

* `use_data()` now uses serialization version 3 by default. (@laurabrianna, #2044)

* Reverse dependency checks are only suggested if they exist
(#1817, @seankross).

Expand Down
10 changes: 5 additions & 5 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#' files. If you really want to do so, set this to `TRUE`.
#' @param compress Choose the type of compression used by [save()].
#' Should be one of "gzip", "bzip2", or "xz".
#' @param version The serialization format version to use. The default, 2, was
#' the default format from R 1.4.0 to 3.5.3. Version 3 became the default from
#' R 3.6.0 and can only be read by R versions 3.5.0 and higher.
#' @param version The serialization format version to use. The default, 3, can
#' only be read by R versions 3.5.0 and higher. For R 1.4.0 to 3.5.3, use
#' version 2.
#' @inheritParams base::save
#'
#' @seealso The [data chapter](https://r-pkgs.org/data.html) of [R
Expand All @@ -38,7 +38,7 @@ use_data <- function(...,
internal = FALSE,
overwrite = FALSE,
compress = "bzip2",
version = 2,
version = 3,
ascii = FALSE) {
check_is_package("use_data()")

Expand Down Expand Up @@ -92,7 +92,7 @@ get_objs_from_dots <- function(.dots) {
}

is_name <- vapply(.dots, is.symbol, logical(1))
if (any(!is_name)) {
if (!all(is_name)) {
ui_abort("Can only save existing named objects.")
}

Expand Down
60 changes: 18 additions & 42 deletions R/git-default-branch.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,18 @@ NULL
#' git_default_branch()
#' }
git_default_branch <- function() {
repo <- git_repo()
git_default_branch_(github_remote_config())
}

# TODO: often when we call git_default_branch(), we already have a GitHub
# configuration or target repo, as produced by github_remote_config() or
# target_repo(). In that case, we don't need to start from scratch as we do
# here. But I'm not sure it's worth adding complexity to allow passing this
# data in.

# TODO: this critique feels somewhat mis-placed, i.e. it brings up a general
# concern about a repo's config (or the user's permissions and creds)
# related to whether github_remotes() should be as silent as it is about
# 404s
critique_remote <- function(remote) {
if (remote$is_configured && is.na(remote$default_branch)) {
ui_bullets(c(
"x" = "The {.val {remote$name}} remote is configured, but we can't
determine its default branch.",
" " = "Possible reasons:",
"*" = "The remote repo no longer exists, suggesting the local remote
should be deleted.",
"*" = "We are offline or that specific Git server is down.",
"*" = "You don't have the necessary permission or something is wrong
with your credentials."
))
}
}
# If config is available, we can use it to avoid an additional lookup
# on the GitHub API
git_default_branch_ <- function(cfg) {
repo <- git_repo()

upstream <- git_default_branch_remote("upstream")
upstream <- git_default_branch_remote(cfg, "upstream")
if (is.na(upstream$default_branch)) {
critique_remote(upstream)
origin <- git_default_branch_remote("origin")
origin <- git_default_branch_remote(cfg, "origin")
if (is.na(origin$default_branch)) {
critique_remote(origin)
db_source <- list()
} else {
db_source <- origin
Expand Down Expand Up @@ -186,7 +165,7 @@ git_default_branch <- function() {

# returns a whole data structure, because the caller needs the surrounding
# context to produce a helpful error message
git_default_branch_remote <- function(remote = "origin") {
git_default_branch_remote <- function(cfg, remote = "origin") {
repo <- git_repo()
out <- list(
name = remote,
Expand All @@ -196,25 +175,22 @@ git_default_branch_remote <- function(remote = "origin") {
default_branch = NA_character_
)

url <- git_remotes()[[remote]]
if (length(url) == 0) {
cfg_remote <- cfg[[remote]]
if (!cfg_remote$is_configured) {
out$is_configured <- FALSE
return(out)
}

out$is_configured <- TRUE
out$url <- url

# TODO: generalize here for GHE hosts that don't include 'github'
parsed <- parse_github_remotes(url)
# if the protocol is ssh, I suppose we can't assume a PAT, i.e. it's better
# to use the Git approach vs. the GitHub API approach
if (grepl("github", parsed$host) && parsed$protocol == "https") {
remote_dat <- github_remotes(remote, github_get = NA)
out$repo_spec <- remote_dat$repo_spec
out$default_branch <- remote_dat$default_branch
out$url <- cfg_remote$url

if (!is.na(cfg_remote$default_branch)) {
out$repo_spec <- cfg_remote$repo_spec
out$default_branch <- cfg_remote$default_branch
return(out)
}

# Fall back to pure git based approach
out$default_branch <- tryCatch(
{
gert::git_fetch(remote = remote, repo = repo, verbose = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use_github <- function(organisation = NULL,
visibility <- match.arg(visibility)
check_protocol(protocol)
check_uses_git()
default_branch <- git_default_branch()
default_branch <- guess_local_default_branch()
check_current_branch(
is = default_branch,
# glue-ing happens inside check_current_branch(), where `gb` gives the
Expand Down
27 changes: 15 additions & 12 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,21 @@ use_dependency <- function(package, type, min_version = NULL) {
"!" = "Package {.pkg {package}} is already listed in
{.field {existing_type}} in DESCRIPTION; no change made."
))
} else if (delta == 0 && !is.null(min_version)) {
# change version
upgrade <- existing_version == "*" ||
numeric_version(min_version) > version_spec(existing_version)
if (upgrade) {
ui_bullets(c(
"v" = "Increasing {.pkg {package}} version to {.val {version}} in
DESCRIPTION."
))
desc$set_dep(package, type, version = version)
desc$write()
changed <- TRUE
} else if (delta == 0 && version_spec(version) != version_spec(existing_version)) {
if (version_spec(version) > version_spec(existing_version)) {
direction <- "Increasing"
} else {
direction <- "Decreasing"
}

ui_bullets(c(
"v" = "{direction} {.pkg {package}} version to {.val {version}} in
DESCRIPTION."
))
desc$set_dep(package, type, version = version)
desc$write()
changed <- TRUE

} else if (delta > 0) {
# moving from, e.g., Suggests to Imports
ui_bullets(c(
Expand All @@ -96,6 +98,7 @@ r_version <- function() {
}

version_spec <- function(x) {
if (x == "*") x <- "0"
x <- gsub("(<=|<|>=|>|==)\\s*", "", x)
numeric_version(x)
}
Expand Down
68 changes: 37 additions & 31 deletions R/pr.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,41 @@ pr_init <- function(branch) {
cfg <- github_remote_config(github_get = NA)
check_for_bad_config(cfg)
tr <- target_repo(cfg, ask = FALSE)
online <- is_online(tr$host)

maybe_good_configs <- c("maybe_ours_or_theirs", "maybe_fork")
if (cfg$type %in% maybe_good_configs) {
if (!online) {
ui_bullets(c(
"x" = 'Unable to confirm the GitHub remote configuration is
"pull request ready".',
"i" = "You probably need to configure a personal access token for
{.val {tr$host}}.",
"i" = "See {.run usethis::gh_token_help()} for help with that.",
"i" = "(Or maybe we're just offline?)"
"x" = "You are not currently online.",
"i" = "You can still create a local branch, but we can't check that your
current branch is up-to-date or setup the remote branch."
))
if (ui_github_remote_config_wat(cfg)) {
if (ui_nah("Do you want to continue?")) {
ui_bullets(c("x" = "Cancelling."))
return(invisible())
}
} else {
maybe_good_configs <- c("maybe_ours_or_theirs", "maybe_fork")
if (cfg$type %in% maybe_good_configs) {
ui_bullets(c(
"x" = 'Unable to confirm the GitHub remote configuration is
"pull request ready".',
"i" = "You probably need to configure a personal access token for
{.val {tr$host}}.",
"i" = "See {.run usethis::gh_token_help()} for help with that.",
))
if (ui_github_remote_config_wat(cfg)) {
ui_bullets(c("x" = "Cancelling."))
return(invisible())
}
}
}

default_branch <- git_default_branch()
default_branch <- if (online) git_default_branch_(cfg) else guess_local_default_branch()
challenge_non_default_branch(
"Are you sure you want to create a PR branch based on a non-default branch?",
default_branch = default_branch
)

online <- is_online(tr$host)
if (online) {
# this is not pr_pull_source_override() because:
# a) we may NOT be on default branch (although we probably are)
Expand All @@ -213,10 +224,6 @@ pr_init <- function(branch) {
ui_bullets(c("v" = "Pulling changes from {.val {remref}}."))
git_pull(remref = remref, verbose = FALSE)
}
} else {
ui_bullets(c(
"!" = "Unable to pull changes for current branch, since we are offline."
))
}

ui_bullets(c("v" = "Creating and switching to local branch {.val {branch}}."))
Expand All @@ -237,7 +244,7 @@ pr_resume <- function(branch = NULL) {
ui_bullets(c(
"i" = "No branch specified ... looking up local branches and associated PRs."
))
default_branch <- git_default_branch()
default_branch <- guess_local_default_branch()
branch <- choose_branch(exclude = default_branch)
if (is.null(branch)) {
ui_bullets(c("x" = "Repo doesn't seem to have any non-default branches."))
Expand Down Expand Up @@ -375,7 +382,7 @@ pr_push <- function() {
repo <- git_repo()
cfg <- github_remote_config(github_get = TRUE)
check_for_config(cfg, ok_configs = c("ours", "fork"))
default_branch <- git_default_branch()
default_branch <- git_default_branch_(cfg)
check_pr_branch(default_branch)
challenge_uncommitted_changes(which = "push")

Expand Down Expand Up @@ -423,7 +430,7 @@ pr_push <- function() {
pr_pull <- function() {
cfg <- github_remote_config(github_get = TRUE)
check_for_config(cfg)
default_branch <- git_default_branch()
default_branch <- git_default_branch_(cfg)
check_pr_branch(default_branch)
challenge_uncommitted_changes(which = "pull")

Expand All @@ -449,11 +456,12 @@ pr_merge_main <- function() {
#' @export
#' @rdname pull-requests
pr_view <- function(number = NULL, target = c("source", "primary")) {
tr <- target_repo(github_get = NA, role = target, ask = FALSE)
cfg <- github_remote_config(github_get = NA)
tr <- target_repo(cfg, github_get = NA, role = target, ask = FALSE)
url <- NULL
if (is.null(number)) {
branch <- git_branch()
default_branch <- git_default_branch()
default_branch <- git_default_branch_(cfg)
if (branch != default_branch) {
url <- pr_url(branch = branch, tr = tr)
if (is.null(url)) {
Expand Down Expand Up @@ -491,11 +499,11 @@ pr_view <- function(number = NULL, target = c("source", "primary")) {
#' @export
#' @rdname pull-requests
pr_pause <- function() {
# intentionally naive selection of target repo
tr <- target_repo(github_get = FALSE, ask = FALSE)
cfg <- github_remote_config(github_get = NA)
tr <- target_repo(cfg, github_get = NA, ask = FALSE)

ui_bullets(c("v" = "Switching back to the default branch."))
default_branch <- git_default_branch()
default_branch <- git_default_branch_(cfg)
if (git_branch() == default_branch) {
ui_bullets(c(
"!" = "Already on this repo's default branch ({.val {default_branch}}),
Expand Down Expand Up @@ -535,8 +543,10 @@ pr_clean <- function(number = NULL,
withr::defer(rstudio_git_tickle())
mode <- match.arg(mode)
repo <- git_repo()
tr <- target_repo(github_get = NA, role = target, ask = FALSE)
default_branch <- git_default_branch()

cfg <- github_remote_config(github_get = NA)
tr <- target_repo(cfg, github_get = NA, role = target, ask = FALSE)
default_branch <- git_default_branch_(cfg)

if (is.null(number)) {
check_pr_branch(default_branch)
Expand Down Expand Up @@ -629,14 +639,10 @@ pr_clean <- function(number = NULL,
# we're in DEFAULT branch of a fork. I wish everyone set up DEFAULT to track the
# DEFAULT branch in the source repo, but this protects us against sub-optimal
# setup.
pr_pull_source_override <- function(tr = NULL, default_branch = NULL) {
# naive selection of target repo; calling function should analyse the config
tr <- tr %||% target_repo(github_get = FALSE, ask = FALSE)

pr_pull_source_override <- function(tr, default_branch) {
# TODO: why does this not use a check_*() function, i.e. shared helper?
# I guess to issue a specific error message?
current_branch <- git_branch()
default_branch <- default_branch %||% git_default_branch()
if (current_branch != default_branch) {
ui_abort("
Internal error: {.fun pr_pull_source_override} should only be used when on
Expand Down Expand Up @@ -994,7 +1000,7 @@ pr_branch_delete <- function(pr) {
invisible(TRUE)
}

check_pr_branch <- function(default_branch = git_default_branch()) {
check_pr_branch <- function(default_branch) {
# the glue-ing happens inside check_current_branch(), where `gb` gives the
# current git branch
check_current_branch(
Expand Down
2 changes: 1 addition & 1 deletion R/proj-desc.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ proj_desc_field_update <- function(key, value, overwrite = TRUE, append = FALSE)
return(invisible())
}

if (!overwrite && length(old > 0) && any(old != "")) {
if (!overwrite && length(old) > 0 && any(old != "")) {
ui_abort("
{.field {key}} has a different value in DESCRIPTION.
Use {.code overwrite = TRUE} to overwrite.")
Expand Down
4 changes: 2 additions & 2 deletions R/ui-legacy.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#'
#' usethis itself now uses cli internally for its UI, but these new functions
#' are not exported and presumably never will be. There is a developer-focused
#' article on the process of transitioning usethis's own UI to use cli (LINK
#' TO COME).
#' article on the process of transitioning usethis's own UI to use cli:
#' [Converting usethis's UI to use cli](https://usethis.r-lib.org/articles/ui-cli-conversion.html).

#' @details
#'
Expand Down
2 changes: 1 addition & 1 deletion R/use_standalone.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ as_version_info_row <- function(field, error_call = caller_env()) {

ver <- strsplit(ver, " ")[[1]]

if (!is_character(ver, n = 2) || any(is.na(ver)) || !all(nzchar(ver))) {
if (!is_character(ver, n = 2) || anyNA(ver) || !all(nzchar(ver))) {
cli::cli_abort(
c(
"Can't parse version `{field}` in `imports:` field.",
Expand Down
2 changes: 1 addition & 1 deletion R/utils-git.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ git_user_get <- function(where = c("de_facto", "local", "global")) {

# translate from "usethis" terminology to "git" terminology
where_from_scope <- function(scope = c("user", "project")) {
scope = match.arg(scope)
scope <- match.arg(scope)

where_scope <- c(user = "global", project = "de_facto")

Expand Down
2 changes: 1 addition & 1 deletion R/utils-github.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ github_remotes <- function(these = c("origin", "upstream"),
# 1. Did we call the GitHub API? Means we know `is_fork` and the parent repo.
# 2. If so, did we call it with auth? Means we know if we can push.
grl$github_got <- map_lgl(repo_info, ~ length(.x) > 0)
if (isTRUE(github_get) && any(!grl$github_got)) {
if (isTRUE(github_get) && !all(grl$github_got)) {
oops <- which(!grl$github_got)
oops_remotes <- grl$remote[oops]
oops_hosts <- unique(grl$host[oops])
Expand Down
Loading

0 comments on commit eec87e6

Please sign in to comment.