Skip to content

Commit

Permalink
feat: improve git branch name detection - fix #33
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Oct 20, 2023
1 parent 57210af commit 1076f1d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ the `CITATION.cff` file
* Modify `add_citation()` to create two additional files: `CITATION.cff` and
`.github/workflows/update-citation-cff.yaml`
* Improve detection of dependencies in `get_deps_*()` functions
* New feature: `get_git_branch_name()` detect git branch name


# rcompendium 1.1
Expand Down
2 changes: 1 addition & 1 deletion R/add_readme_rmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ add_readme_rmd <- function(type = "package", given = NULL, family = NULL,
xfun::gsub_file(path, "{{given}}", given, fixed = TRUE)
xfun::gsub_file(path, "{{family}}", family, fixed = TRUE)
xfun::gsub_file(path, "{{github}}", github, fixed = TRUE)
xfun::gsub_file(path, "{{branch}}", "main", fixed = TRUE) #
xfun::gsub_file(path, "{{branch}}", get_git_branch_name(), fixed = TRUE)


## Message ----
Expand Down
32 changes: 32 additions & 0 deletions R/utils-gh.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,35 @@ is_gh_repo <- function(owner, repo) {
tryCatch(gh::gh("GET /repos/{owner}/{repo}", repo = repo,
owner = owner), error = function(e) NULL)
}



#' **Get current/default git branch name**
#'
#' @noRd

get_git_branch_name <- function() {

is_git()

current_branch <- gert::git_branch()

if (is.null(current_branch)) {

config <- as.data.frame(gert::git_config_global())

default_branch <- config[which(config$"name" == "init.defaultbranch"),
"value"]

if (length(default_branch) == 0) {

current_branch <- "master"

} else {

current_branch <- default_branch
}
}

current_branch
}

0 comments on commit 1076f1d

Please sign in to comment.