diff --git a/.github/contributing.md b/.github/contributing.md index d2ec4233..b9c3d7ca 100644 --- a/.github/contributing.md +++ b/.github/contributing.md @@ -83,7 +83,7 @@ See [`support.md`][support] on how to get help. it’s your job to convince us * Two small PRs are prefered than one big PR * Separate issues from features in a PR, if you need to fix an issue to develop a feature or you find an issue when developing a feature. Make a `fix` branch, solve the issue, make the PR in the repository. Apply the patch by cherry-pick or by merging in the `feature` branch. Show in the description that PR ## is included in this feature. -* Merge master is preferred in PR if there is any update. We prefer that you solve the possible merge conflicts. +* Merge main is preferred in PR if there is any update. We prefer that you solve the possible merge conflicts. ## Resources diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18e8b678..03205d9b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,10 +3,10 @@ name: CI on: push: branches: - - master + - main pull_request: branches: - - master + - main paths-ignore: - 'dotfiles_template/*.md' - 'docs/**' diff --git a/README.md b/README.md index fe2384b3..207eee9d 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ This need a very big improvement --> * Can be installed as standalone, not mandatory to be as git submodule (Should be done manually). -* Init scripts ([see init-scripts](https://github.com/gtrabanco/dotfiles/tree/master/shell/init.scripts) in [gtrabanco/dotfiles](https://github.com/gtrabanco/dotfiles)). This provides many possibilities as modular loading of custom variables or aliases by machine, loading secrets... Whatever you can imagine. +* Init scripts ([see init-scripts](https://github.com/gtrabanco/dotfiles/tree/main/shell/init.scripts) in [gtrabanco/dotfiles](https://github.com/gtrabanco/dotfiles)). This provides many possibilities as modular loading of custom variables or aliases by machine, loading secrets... Whatever you can imagine. * Per machine (or whatever name you want to) export packages `sloth packages dump` (you can use `dot` instead of `sloth`, we also have aliases for this command like `lazy` and `s`). * Compatibility with all Dotly scripts. * When you install SLOTH a backup of all previous files is done (`.bashrc`, `.zshrc` and `.zshenv`) if you request it. @@ -62,7 +62,7 @@ This need a very big improvement * Improved package managers and the way they are executed. You can also create your own wrappers for your package manager. * Improved registry (recipes) and how recipes can be updated as if they were packages. -**About autocompletion** Is a known issue that current autocompletion for `dot` command does not work as good as supposed and currently only autocomplete the first argument (option). This will be fixed in the future but suppossed a gain in complexity for autocompletion that I am not interested in develop now. [See PR #146 for more information](https://github.com/gtrabanco/.Sloth/pull/146) +**About autocompletion** Is a known issue that current autocompletion for `dot` command does not work as good as supposed and currently only autocomplete the first argument (option). This will be fixed in the future but suppossed a gain in complexity for autocompletion that I am not interested in develop now. [See PR #146 for more information](https://github.com/gtrabanco/dotSloth/pull/146) ## INSTALLATION @@ -130,7 +130,7 @@ dotfiles git remote add origin git@github.com:${GITHUB_USER}/${GITHUB_DOTFILES_REPOSITORY}.git git add . git commit -m "Initial commit" -git push origin master +git push origin main ``` Replace the variables for your own values or the full url for your repository. @@ -211,7 +211,7 @@ There is a recipe for NVM and NVM and default LTS node, npm & npx are installed # Contributing -You can contribute to the project by making a PR, reporting an issue, suggesting a feature, writting about the project or by applying any idea you have. All contributions that respect our [Code of Conduct](https://github.com/gtrabanco/.Sloth/blob/master/.github/code-of-conduct.md) are very welcoming. +You can contribute to the project by making a PR, reporting an issue, suggesting a feature, writting about the project or by applying any idea you have. All contributions that respect our [Code of Conduct](https://github.com/gtrabanco/.Sloth/blob/main/.github/code-of-conduct.md) are very welcoming. # Roadmap diff --git a/dotly-migrator b/dotly-migrator index 75a6cb38..39246fa5 100755 --- a/dotly-migrator +++ b/dotly-migrator @@ -1,10 +1,11 @@ #!/usr/bin/env bash +export DOTLY_DEFAULT_GIT_BRANCH="${DOTLY_DEFAULT_GIT_BRANCH:-main}" export DOTLY_LOG_FILE=${DOTLY_LOG_FILE:-$HOME/dotly.log} export DOTLY_ENV=${DOTLY_ENV:-PROD} export DOTLY_INSTALLER=true export DOTLY_MIGRATION=true -export SLOTH_REPOSITORY="${SLOTH_REPOSITORY:-https://github.com/gtrabanco/sloth}" +export SLOTH_REPOSITORY="${SLOTH_REPOSITORY:-https://github.com/gtrabanco/dotSloth}" export SLOTH_INSTALLER_URL="${SLOTH_INSTALLER_URL:-https://raw.githubusercontent.com/gtrabanco/sloth/HEAD/installer}" red='\033[0;31m' @@ -20,7 +21,10 @@ _w() { _a() { _w " > $1"; } _e() { _a "${red}$1${normal}"; } _s() { _a "${green}$1${normal}"; } -_q() { read -rp "πŸ€” $* : " "REPLY"; echo "${REPLY:-}"; } +_q() { + read -rp "πŸ€” $* : " "REPLY" + echo "${REPLY:-}" +} _pk() { read -rp "Press a key to ${1}... πŸ‘‡" "REPLY"; } _log() { @@ -70,29 +74,29 @@ if [[ -z "${DOTLY_PATH:-}" || -z "$DOTFILES_PATH" ]]; then fi if $DOTLY_MIGRATION; then - cp -R "$DOTFILES_PATH" "$HOME/.dotfiles.back-$(date +%s)" - cd "$DOTFILES_PATH" - # Move dotly to sloth - git mv "modules/dotly" "modules/sloth" - # Replace values in .gitmodules: path of module, branch and url - call_sed -i 's|modules/dotly|modules/sloth|g' .gitmodules - git config -f .gitmodules submodule."modules/sloth".branch "master" - git config -f .gitmodules submodule."modules/sloth".url "https://github.com/gtrabanco/sloth" - # Select master branch - cd modules/sloth || exit 4 - git checkout --force master - cd ../.. - # Sync submodule - git submodule sync --recursive | _log "Sync sloth module" - # Define new variables - export DOTLY_PATH="$DOTFILES_PATH/modules/sloth" - export SLOTH_PATH="$DOTFILES_PATH/modules/sloth" - export ZIM_HOME="${DOTFILES_PATH:-}/shell/zsh/.zimfw" - # Reinstall zim - zsh "$ZIM_HOME/zimfw.zsh" install - "${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" shell zsh reload_completions - #shellcheck disable=SC1091 - . "${SLOTH_PATH:-${DOTLY_PATH:-}}/shell/init-sloth.sh" - "${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" migration v2.0.0 - _w "πŸŽ‰ Restart your terminal" + cp -R "$DOTFILES_PATH" "$HOME/.dotfiles.back-$(date +%s)" + cd "$DOTFILES_PATH" + # Move dotly to sloth + git mv "modules/dotly" "modules/sloth" + # Replace values in .gitmodules: path of module, branch and url + call_sed -i 's|modules/dotly|modules/sloth|g' .gitmodules + git config -f .gitmodules submodule."modules/sloth".branch "${DOTLY_DEFAULT_GIT_BRANCH}" + git config -f .gitmodules submodule."modules/sloth".url "${SLOTH_REPOSITORY}" + # Select master branch + cd modules/sloth || exit 4 + git checkout --force "${DOTLY_DEFAULT_GIT_BRANCH}" + cd ../.. + # Sync submodule + git submodule sync --recursive | _log "Sync sloth module" + # Define new variables + export DOTLY_PATH="$DOTFILES_PATH/modules/sloth" + export SLOTH_PATH="$DOTFILES_PATH/modules/sloth" + export ZIM_HOME="${DOTFILES_PATH:-}/shell/zsh/.zimfw" + # Reinstall zim + zsh "$ZIM_HOME/zimfw.zsh" install + "${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" shell zsh reload_completions + #shellcheck disable=SC1091 + . "${SLOTH_PATH:-${DOTLY_PATH:-}}/shell/init-sloth.sh" + "${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" migration v2.0.0 + _w "πŸŽ‰ Restart your terminal" fi diff --git a/installer b/installer index 501acb79..94fff3b5 100755 --- a/installer +++ b/installer @@ -11,7 +11,7 @@ ICLOUD_PATH="$HOME/Library/Mobile\ Documents/com~apple~CloudDocs/" IS_ICLOUD_DOTFILES=false DOTLY_REPOSITORY=${DOTLY_REPOSITORY:-gtrabanco/sloth} -DOTLY_BRANCH=${DOTLY_BRANCH:-master} +DOTLY_BRANCH=${DOTLY_BRANCH:-main} DOTLY_LOG_FILE=${DOTLY_LOG_FILE:-$HOME/dotly.log} export DOTLY_ENV=${DOTLY_ENV:-PROD} export DOTLY_INSTALLER=true @@ -54,13 +54,13 @@ _log() { current_date=$(date "+%Y-%m-%d %H:%M:%S") touch "$DOTLY_LOG_FILE" - echo "----- $current_date - $log_name -----" >> "$DOTLY_LOG_FILE" + echo "----- $current_date - $log_name -----" >>"$DOTLY_LOG_FILE" while IFS= read -r log_message; do - echo "$log_message" >> "$DOTLY_LOG_FILE" + echo "$log_message" >>"$DOTLY_LOG_FILE" done - echo "" >> "$DOTLY_LOG_FILE" + echo "" >>"$DOTLY_LOG_FILE" } current_timestamp() { date +%s; } @@ -81,7 +81,7 @@ create_dotfiles_dir() { } command_exists() { - type "$1" > /dev/null 2>&1 + type "$1" >/dev/null 2>&1 } is_macos() { @@ -89,7 +89,7 @@ is_macos() { } is_clt_installed() { - command -vp xcode-select > /dev/null 2>&1 && xpath=$(command -p xcode-select --print-path) && test -d "${xpath}" && test -x "${xpath}" + command -vp xcode-select >/dev/null 2>&1 && xpath=$(command -p xcode-select --print-path) && test -d "${xpath}" && test -x "${xpath}" } install_clt() { @@ -116,7 +116,7 @@ install_clt() { if ! is_clt_installed && command -p sudo -v -B; then command -p xcode-select --install if [[ "${DOTLY_ENV:-PROD}" != "CI" ]]; then - until command -p xcode-select --print-path > /dev/null 2>&1; do + until command -p xcode-select --print-path >/dev/null 2>&1; do _a "Waiting for Command Line tools to be installed... Check again in 10 secs" sleep 10 done @@ -150,17 +150,17 @@ start_sudo() { } stop_sudo() { - builtin kill "$SUDO_PID" > /dev/null 2>&1 + builtin kill "$SUDO_PID" >/dev/null 2>&1 builtin trap - SIGINT SIGTERM command -p sudo -k } has_sudo() { [[ "${DOTLY_ENV:-PROD}" == "CI" ]] && return - command -p sudo -n -v > /dev/null 2>&1 + command -p sudo -n -v >/dev/null 2>&1 } -if command -v brew > /dev/null 2>&1; then +if command -v brew >/dev/null 2>&1; then eval "$(command brew shellenv)" else PATH="/usr/local/bin:/opt/homebrew/bin:/home/.linuxbrew/bin:${HOME}/.homebrew/bin:${HOME}/.brew/bin:${HOME}/.linuxbrew/bin${PATH:+:$PATH}" @@ -338,7 +338,7 @@ if ! command_exists git; then if is_macos; then _a "Checking if Command Line Tools are installed πŸ•΅οΈβ€β™‚οΈ" - xcode-select --install 2>&1 | grep installed > /dev/null + xcode-select --install 2>&1 | grep installed >/dev/null if [[ $? ]]; then _a "Installing Command Line Tools πŸ“Ί" xcode-select --install @@ -400,4 +400,4 @@ _a "Installing .Sloth" "${PWD}/bin/dot" core install 2>&1 | _log "Installing .Sloth" _a "πŸŽ‰ .Sloth was sucessfully installed! πŸŽ‰" -_a "Please, restart your terminal to see the changes" \ No newline at end of file +_a "Please, restart your terminal to see the changes" diff --git a/scripts/core/src/git.sh b/scripts/core/src/git.sh index ecc20fd7..0d9425dc 100755 --- a/scripts/core/src/git.sh +++ b/scripts/core/src/git.sh @@ -12,19 +12,19 @@ if [[ -n "${GIT_EXECUTABLE:-}" && ! -x "$GIT_EXECUTABLE" ]] && - command -v git > /dev/null 2>&1 + command -v git >/dev/null 2>&1 then GIT_EXECUTABLE="$(command -v git)" elif [[ -z "${GIT_EXECUTABLE:-}" ]] && - command -v git > /dev/null 2>&1 + command -v git >/dev/null 2>&1 then GIT_EXECUTABLE="$(command -v git)" elif [[ -z "${GIT_EXECUTABLE:-}" ]] && - command -vp git > /dev/null 2>&1 + command -vp git >/dev/null 2>&1 then GIT_EXECUTABLE="$(command -vp git)" @@ -38,6 +38,8 @@ then echoerr "No git binary found, please install it or review your env \`PATH\` variable or check if defined that \`GIT_EXECUTABLE\` has a right value" | log::file "Error trying to locate git command" fi export GIT_EXECUTABLE +export GIT_DEFAULT_BRANCH="${GIT_DEFAULT_BRANCH:-main}" +export GIT_DEFAULT_REMOTE="${GIT_DEFAULT_REMOTE:-origin}" #; # git::git() @@ -70,7 +72,7 @@ git::git() { # check if a directory is a repository #" git::is_in_repo() { - git::git "$@" rev-parse --is-inside-work-tree > /dev/null 2>&1 + git::git "$@" rev-parse --is-inside-work-tree >/dev/null 2>&1 } #; @@ -78,7 +80,7 @@ git::is_in_repo() { # Get the current active branch #" git::current_branch() { - git::git "$@" branch --show-current --no-color 2> /dev/null || return + git::git "$@" branch --show-current --no-color 2>/dev/null || return } #; @@ -117,7 +119,7 @@ git::get_submodule_property() { git::submodule_exists() { local -r submodule_name="${1:-}" - [[ -n "$submodule_name" ]] && git::git "${@:2}" config -f ".gitmodules" submodule."$submodule_name".path > /dev/null 2>&1 + [[ -n "$submodule_name" ]] && git::git "${@:2}" config -f ".gitmodules" submodule."$submodule_name".path >/dev/null 2>&1 } #; @@ -145,9 +147,9 @@ git::remove_submodule() { # @param string remote Optional remote, use "origin" as default #" git::check_remote_exists() { - local -r remote="${1:-origin}" + local -r remote="${1:-${GIT_DEFAULT_REMOTE}}" [[ -n "${1:-}" ]] && shift - git::git "$@" remote get-url "$remote" > /dev/null 2>&1 + git::git "$@" remote get-url "$remote" >/dev/null 2>&1 } #; @@ -213,16 +215,16 @@ git::remote_branch_exists() { shift 2 elif [[ $# -eq 1 ]]; then branch="$1" - remote="origin" + remote="${GIT_DEFAULT_REMOTE}" shift else - branch="master" - remote="origin" + branch="${GIT_DEFAULT_BRANCH}" + remote="${GIT_DEFAULT_REMOTE}" fi ! git::check_remote_exists "$remote" "$@" && return 1 - [[ -n "$(git::git "$@" branch --remotes --list "${remote}/${branch}" 2> /dev/null)" ]] + [[ -n "$(git::git "$@" branch --remotes --list "${remote}/${branch}" 2>/dev/null)" ]] } #; @@ -249,7 +251,7 @@ git::local_latest_tag_version() { local -r remote_url="${1:-}" [[ -z "$remote_url" ]] && return - git::git "${@:2}" describe --tags "$(git::git "${@:2}" rev-list --tags --max-count=1)" 2> /dev/null | sed 's/^v//' + git::git "${@:2}" describe --tags "$(git::git "${@:2}" rev-list --tags --max-count=1)" 2>/dev/null | sed 's/^v//' } #; @@ -265,7 +267,7 @@ git::remote_latest_tag_version() { local -r version_pattern="${2:-v*.*.*}" [[ -z "$remote_url" ]] && return - git::git "${@:3}" ls-remote --tags --refs "$remote_url" "${version_pattern}" 2> /dev/null | command awk '{gsub(/\\^\\{\\}/,"", $NF);gsub("refs/tags/",""); gsub("v",""); print $NF}' | command sort -Vur | command head -n1 + git::git "${@:3}" ls-remote --tags --refs "$remote_url" "${version_pattern}" 2>/dev/null | command awk '{gsub(/\\^\\{\\}/,"", $NF);gsub("refs/tags/",""); gsub("v",""); print $NF}' | command sort -Vur | command head -n1 } #; @@ -317,7 +319,7 @@ git::check_branch_is_ahead() { # @return string|void #" git::get_remote_head_upstream_branch() { - local -r remote="${1:-origin}" + local -r remote="${1:-$GIT_DEFAULT_REMOTE}" [[ -n "${1:-}" ]] && shift ! git::check_remote_exists "$remote" "$@" && return @@ -335,12 +337,12 @@ git::set_remote_head_upstream_branch() { branch="$2" shift 2 elif [[ $# -eq 1 ]]; then - remote="origin" + remote="${GIT_DEFAULT_REMOTE}" branch="$1" shift else - remote="origin" - branch="master" + remote="${GIT_DEFAULT_REMOTE}" + branch="${GIT_DEFAULT_BRANCH}" fi git::git "$@" remote set-head "$remote" "$branch" @@ -350,7 +352,7 @@ git::set_remote_head_upstream_branch() { # git::check_file_exists_in_previous_commit() #" git::check_file_exists_in_previous_commit() { - [[ -n "${1:-}" ]] && ! git::git "${@:2}" rev-parse @~:"${1:-}" > /dev/null 2>&1 + [[ -n "${1:-}" ]] && ! git::git "${@:2}" rev-parse @~:"${1:-}" >/dev/null 2>&1 } #; @@ -359,7 +361,7 @@ git::check_file_exists_in_previous_commit() { # @param string file #" git::get_file_last_commit_timestamp() { - [[ -n "${1:-}" ]] && git "${@:2}" rev-list --all --date-order --timestamp -1 "${1:-}" 2> /dev/null | awk '{print $1}' + [[ -n "${1:-}" ]] && git "${@:2}" rev-list --all --date-order --timestamp -1 "${1:-}" 2>/dev/null | awk '{print $1}' } #; @@ -367,7 +369,7 @@ git::get_file_last_commit_timestamp() { # @param string commit #" git::get_commit_timestamp() { - [[ -n "${1:-}" ]] && git::git "${@:2}" rev-list --all --date-order --timestamp 2> /dev/null | grep "${1:-}" | awk '{print $1}' + [[ -n "${1:-}" ]] && git::git "${@:2}" rev-list --all --date-order --timestamp 2>/dev/null | grep "${1:-}" | awk '{print $1}' } #; @@ -384,7 +386,7 @@ git::check_file_is_modified_after_commit() { { [[ -z "$file_path" ]] || [[ -z "${commit_to_check:-}" ]] || [[ ! -e "$file_path" ]]; } && return 1 shift 2 - file_commit_date="$(git::get_file_last_commit_timestamp "${file_path:-}" "$@" 2> /dev/null)" + file_commit_date="$(git::get_file_last_commit_timestamp "${file_path:-}" "$@" 2>/dev/null)" [[ -z "$file_commit_date" ]] && return 0 # File path did not exists previously then # it is more recent than any commit πŸ˜… @@ -407,12 +409,12 @@ git::clone_track_branch() { branch="${2:-}" shift 2 elif [[ $# -eq 1 ]]; then - remote="origin" + remote="${GIT_DEFAULT_REMOTE}" branch="${1:-}" shift else - remote="origin" - branch="master" + remote="${GIT_DEFAULT_REMOTE}" + branch="${GIT_DEFAULT_BRANCH}" fi ! git::check_remote_exists "$remote" "$@" && return 1 @@ -433,9 +435,9 @@ git::clone_branches() { local -r remote="${1:-origin}" [[ -n "${1:-}" ]] && shift - ! git::git "$@" remote get-url "$remote" > /dev/null 2>&1 && return 1 + ! git::git "$@" remote get-url "$remote" >/dev/null 2>&1 && return 1 - for remote_branch in $(git::git "$@" branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -I _ echo _ | grep "^remotes/${remote}"); do + for remote_branch in $(git::git "$@" branch -a | sed -n "/\/HEAD /d; /\/${GIT_DEFAULT_BRANCH}$/d; /remotes/p;" | xargs -I _ echo _ | grep "^remotes/${remote}"); do branch="${remote_branch//remotes\/${remote}\//}" git::clone_track_branch "$remote" "$branch" "$@" 1>&2 || true done @@ -445,7 +447,7 @@ git::clone_branches() { # git::current_branch_is_tracked() #" git::current_branch_is_tracked() { - git::git "$@" rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2> /dev/null + git::git "$@" rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null } #; @@ -458,20 +460,20 @@ git::current_branch_is_tracked() { git::pull_branch() { case $# in - 1) - local -r remote="origin" - local -r branch="${1:-master}" - shift - ;; - 0) - local -r remote="origin" - local -r branch="master" - ;; - *) - local -r remote="${1:-origin}" - local -r branch="${2:-master}" - shift 2 - ;; + 1) + local -r remote="${GIT_DEFAULT_REMOTE}" + local -r branch="${1:-$GIT_DEFAULT_BRANCH}" + shift + ;; + 0) + local -r remote="${GIT_DEFAULT_REMOTE}" + local -r branch="${GIT_DEFAULT_BRANCH}" + ;; + *) + local -r remote="${1:-"$GIT_DEFAULT_REMOTE"}" + local -r branch="${2:-"$GIT_DEFAULT_BRANCH"}" + shift 2 + ;; esac # Check if remote branch exists @@ -500,14 +502,14 @@ git::pull_branch() { # @param any args Additional arguments to pass to git command #" git::repository_pull_all() { - local -r remote="${1:-origin}" + local -r remote="${1:-"$GIT_DEFAULT_REMOTE"}" ! git::check_remote_exists "$remote" "$@" && return 1 git::git "$@" clean -f -q 1>&2 git::git "$@" reset --hard HEAD 1>&2 - for remote_branch in $(git::git "$@" branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -I _ echo _ | grep "^remotes/${remote}"); do + for remote_branch in $(git::git "$@" branch -a | sed -n "/\/HEAD /d; /\/${GIT_DEFAULT_BRANCH}$/d; /remotes/p;" | xargs -I _ echo _ | grep "^remotes/${remote}"); do branch="${remote_branch//remotes\/${remote}\//}" git::clone_track_branch "$remote" "$branch" "$@" 1>&2 git::git "$@" checkout --force "$branch" 1>&2 @@ -522,18 +524,18 @@ git::repository_pull_all() { # Initialize a git repository only if necessary only # @param string url Mandatory if git::is_in_repo fails # @param string remote origin by default -# @param string branch master by default. Only used if not any branch +# @param string branch main by default. Only used if not any branch # @param any args Additional arguments to pass to git command. Url, remote and branch arguments are mandatory if you want to pass arguments to git. #" git::init_repository_if_necessary() { local head_branch local -r url="${1:-}" - local -r remote="${2:-origin}" - local -r branch="${3:-master}" + local -r remote="${2:-$GIT_DEFAULT_REMOTE}" + local -r branch="${3:-$GIT_DEFAULT_BRANCH}" [[ -n "${url}" ]] && shift [[ -n "${1:-}" ]] && shift # remote [[ -n "${1:-}" ]] && shift # branch - git::is_in_repo "$@" > /dev/null 2>&1 && return + git::is_in_repo "$@" >/dev/null 2>&1 && return if [[ -n "$url" ]]; then git::git "$@" init 1>&2 @@ -542,7 +544,7 @@ git::init_repository_if_necessary() { git::git "$@" config "remote.${remote}.fetch" "+refs/heads/*:refs/remotes/${remote}/*" 1>&2 git::git "$@" fetch --all --tags --force 1>&2 git::git "$@" branch --set-upstream-to="${remote}/${branch}" "$branch" 1>&2 - git::git "$@" remote set-head "$remote" --auto > /dev/null 2>&1 1>&2 + git::git "$@" remote set-head "$remote" --auto >/dev/null 2>&1 1>&2 head_branch="$(git::get_remote_head_upstream_branch "$remote" "$@")" if [[ -z "$head_branch" ]]; then @@ -573,8 +575,8 @@ git::add_to_gitignore() { shift if [[ -n "$content" ]]; then - grep -q "^${content}$" "$gitignore_file_path" || echo "$content" | tee -a "$GITIGNORE_PATH" > /dev/null 2>&1 - echo > /dev/null 2>&1 + grep -q "^${content}$" "$gitignore_file_path" || echo "$content" | tee -a "$GITIGNORE_PATH" >/dev/null 2>&1 + echo >/dev/null 2>&1 fi if ! grep -q "^${content}$" "$gitignore_file_path"; then diff --git a/scripts/core/src/github.sh b/scripts/core/src/github.sh index 135c4c4b..6dd4d306 100644 --- a/scripts/core/src/github.sh +++ b/scripts/core/src/github.sh @@ -10,6 +10,7 @@ fi GITHUB_CACHE_PETITIONS_PERIOD_IN_DAYS="${GITHUB_CACHE_PETITIONS_PERIOD_IN_DAYS:-3}" # Maximum days a cache petition is cached GITHUB_USE_CACHE=${GITHUB_USE_CACHE:-true} # Default behaviour for use of cache +GIT_DEFAULT_BRANCH="${GIT_DEFAULT_BRANCH:-main}" # Non configurable variables (internal use only) JQ_CHECKED=false @@ -57,21 +58,21 @@ github::get_api_url() { while [ $# -gt 0 ]; do case ${1:-} in - --user | -u | --organization | -o) - user="${2:-}" - shift 2 - ;; - --repository | -r) - repository="${2:-}" - shift 2 - ;; - --branch | -b) - branch="/branches/${2:-}" - shift 2 - ;; - *) - break 2 - ;; + --user | -u | --organization | -o) + user="${2:-}" + shift 2 + ;; + --repository | -r) + repository="${2:-}" + shift 2 + ;; + --branch | -b) + branch="/branches/${2:-}" + shift 2 + ;; + *) + break 2 + ;; esac done @@ -99,25 +100,25 @@ github::get_api_url() { github::branch_raw_url() { local user repository branch arguments - branch="master" + branch="$GIT_DEFAULT_BRANCH" while [ $# -gt 0 ]; do case ${1:-} in - --user | -u | --organization | -o) - user="${2:-}" - shift 2 - ;; - --repository | -r) - repository="${2:-}" - shift 2 - ;; - --branch | -b) - branch="/branches/${2:-}" - shift 2 - ;; - *) - break 2 - ;; + --user | -u | --organization | -o) + user="${2:-}" + shift 2 + ;; + --repository | -r) + repository="${2:-}" + shift 2 + ;; + --branch | -b) + branch="/branches/${2:-}" + shift 2 + ;; + *) + break 2 + ;; esac done @@ -140,7 +141,7 @@ github::branch_raw_url() { [[ $# -gt 1 ]] && branch="$1" && shift [[ $# -gt 0 ]] && file="/$(str::join '/' "$*")" - echo "$GITHUB_RAW_FILES_URL/$user/$repository/${branch:-master}${file:-}" + echo "$GITHUB_RAW_FILES_URL/$user/$repository/${branch:-main}${file:-}" } github::clean_cache() { @@ -156,9 +157,9 @@ github::hash() { github::check_git if [ ! -t 0 ]; then - git hash-object --stdin < /dev/stdin + git hash-object --stdin /dev/null)" + url="$(github::get_api_url --branch "${default_branch}" "$1" | github::curl | yq -p=json -r '.commit.commit.tree.url' 2>/dev/null)" fi shift [[ -z "${url:-}" ]] && return 1 readarray -t file_paths < <(str::join "/" "$@" | tr "/" "\n") - json="$(github::curl "$url" | yq -p=json --arg file_path "${file_paths[0]}" '.tree[] | select(.path == $file_path)' 2> /dev/null)" + json="$(github::curl "$url" | yq -p=json --arg file_path "${file_paths[0]}" '.tree[] | select(.path == $file_path)' 2>/dev/null)" if [[ -n "$json" ]] && [[ ${#file_paths[@]} -gt 1 ]]; then github::get_remote_file_path_json "$(echo "$json" | yq -p=json -r '.url')" "$(str::join / "${file_paths[@]:1}")" && return diff --git a/scripts/core/src/sloth_update.sh b/scripts/core/src/sloth_update.sh index c8acea1b..9981aee9 100644 --- a/scripts/core/src/sloth_update.sh +++ b/scripts/core/src/sloth_update.sh @@ -29,13 +29,13 @@ if [[ -z "${SLOTH_GITMODULES_BRANCH:-}" && -f "${DOTFILES_PATH:-${HOME}/.dotfile fi # Defaults values if no values are provided -[[ -z "${SLOTH_DEFAULT_GIT_HTTP_URL:-}" ]] && readonly SLOTH_DEFAULT_GIT_HTTP_URL="https://github.com/gtrabanco/.Sloth" -[[ -z "${SLOTH_DEFAULT_GIT_SSH_URL:-}" ]] && readonly SLOTH_DEFAULT_GIT_SSH_URL="git+ssh://git@github.com:gtrabanco/.Sloth.git" +[[ -z "${SLOTH_DEFAULT_GIT_HTTP_URL:-}" ]] && readonly SLOTH_DEFAULT_GIT_HTTP_URL="https://github.com/gtrabanco/dotSloth" +[[ -z "${SLOTH_DEFAULT_GIT_SSH_URL:-}" ]] && readonly SLOTH_DEFAULT_GIT_SSH_URL="git+ssh://git@github.com:gtrabanco/dotSloth.git" [[ -z "${SLOTH_DEFAULT_REMOTE:-}" ]] && readonly SLOTH_DEFAULT_REMOTE="origin" # SLOTH_DEFAULT_BRANCH is not the same as SLOTH_GITMODULES_BRANCH # SLOTH_GITMODULES_BRANCH is the branch we want to use if we are using always latest version # SLOTH_GITMODULES_BRANCH is the HEAD branch of remote repository were Pull Request are merged -[[ -z "${SLOTH_DEFAULT_BRANCH:-}" ]] && readonly SLOTH_DEFAULT_BRANCH="master" +[[ -z "${SLOTH_DEFAULT_BRANCH:-}" ]] && readonly SLOTH_DEFAULT_BRANCH="main" SLOTH_DEFAULT_URL=${SLOTH_GITMODULES_URL:-$SLOTH_DEFAULT_GIT_SSH_URL} @@ -58,7 +58,7 @@ sloth_update::sloth_repository_set_ready() { fi if ! git::is_in_repo "${SLOTH_UPDATE_GIT_ARGS[@]}" || ! git::check_remote_exists "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_UPDATE_GIT_ARGS[@]}"; then - git::init_repository_if_necessary "${SLOTH_DEFAULT_URL:-${SLOTH_DEFAULT_GIT_SSH_URL:-git+ssh://git@github.com:gtrabanco/sloth.git}}" "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_DEFAULT_BRANCH:-master}" "${SLOTH_UPDATE_GIT_ARGS[@]}" + git::init_repository_if_necessary "${SLOTH_DEFAULT_URL:-${SLOTH_DEFAULT_GIT_SSH_URL:-git+ssh://git@github.com:gtrabanco/sloth.git}}" "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_DEFAULT_BRANCH:-main}" "${SLOTH_UPDATE_GIT_ARGS[@]}" fi # Set head branch @@ -68,7 +68,7 @@ sloth_update::sloth_repository_set_ready() { git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" config --bool core.autcrl false 1>&2 || true # Track default branch - git::clone_track_branch "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_DEFAULT_BRANCH:-master}" "${SLOTH_UPDATE_GIT_ARGS[@]}" 1>&2 || true + git::clone_track_branch "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_DEFAULT_BRANCH:-main}" "${SLOTH_UPDATE_GIT_ARGS[@]}" 1>&2 || true # Unshallow by the way git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" fetch --unshallow 1>&2 || true @@ -91,7 +91,7 @@ sloth_update::get_current_version() { return fi - git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" describe --tags --abbrev=0 2> /dev/null + git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" describe --tags --abbrev=0 2>/dev/null } #; @@ -141,8 +141,8 @@ sloth_update::local_sloth_repository_can_be_updated() { # If remote exists locally if git::check_remote_exists "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_UPDATE_GIT_ARGS[@]}"; then - git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" branch --set-upstream-to="${SLOTH_DEFAULT_REMOTE:-origin}/${SLOTH_DEFAULT_BRANCH:-master}" "${SLOTH_DEFAULT_BRANCH:-master}" > /dev/null 2>&1 - git::check_branch_is_ahead "${SLOTH_DEFAULT_BRANCH:-master}" "${SLOTH_UPDATE_GIT_ARGS[@]}" && HAS_UNPUSHED_COMMITS=true + git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" branch --set-upstream-to="${SLOTH_DEFAULT_REMOTE:-origin}/${SLOTH_DEFAULT_BRANCH:-main}" "${SLOTH_DEFAULT_BRANCH:-main}" >/dev/null 2>&1 + git::check_branch_is_ahead "${SLOTH_DEFAULT_BRANCH:-main}" "${SLOTH_UPDATE_GIT_ARGS[@]}" && HAS_UNPUSHED_COMMITS=true fi if $IS_WORKING_DIRECTORY_CLEAN && ! $HAS_UNPUSHED_COMMITS; then @@ -173,7 +173,7 @@ sloth_update::should_be_updated() { # .Sloth were installed using a package manager if ${HOMEBREW_SLOTH:-false}; then # False if there is an update & true if current version is the latest version - if brew outdated dot > /dev/null 2>&1; then + if brew outdated dot >/dev/null 2>&1; then return 1 else return 0 @@ -181,7 +181,7 @@ sloth_update::should_be_updated() { fi # Check if currently we want to pin to a fixed version but is more recent that current version - if platform::semver get major "$SLOTH_UPDATE_VERSION" > /dev/null 2>&1; then + if platform::semver get major "$SLOTH_UPDATE_VERSION" >/dev/null 2>&1; then # Different than current version & is not available in local & remote latest version is greater or equal that SLOTH_UPDATE_VERSION (pinned version) if [[ @@ -238,7 +238,7 @@ sloth_update::sloth_update() { local remote url default_branch head_branch force_update updated_version remote="${1:-${SLOTH_DEFAULT_REMOTE:-origin}}" url="${2:-${SLOTH_GITMODULES_URL:-${SLOTH_DEFAULT_GIT_SSH_URL:-git+ssh://git@github.com:gtrabanco/sloth.git}}}" - branch="${3:-${SLOTH_DEFAULT_BRANCH:-master}}" + branch="${3:-${SLOTH_DEFAULT_BRANCH:-main}}" default_remote_branch="${remote}/${branch}" force_update="${4:-false}" @@ -249,7 +249,7 @@ sloth_update::sloth_update() { # .Sloth were installed using a package manager if ${HOMEBREW_SLOTH:-false}; then # False if there is an update & true if current version is the latest version - if brew outdated dot > /dev/null 2>&1; then + if brew outdated dot >/dev/null 2>&1; then return else output::answer "Updating .Sloth by using brew" @@ -280,8 +280,8 @@ sloth_update::sloth_update() { git::pull_branch "$remote" "$head_branch" "${SLOTH_UPDATE_GIT_ARGS[@]}" 1>&2 && output::solution "Repository has been updated" || return 40 - git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" checkout --force "${SLOTH_GITMODULES_BRANCH:-${SLOTH_DEFAULT_BRANCH:-master}}" - git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" reset --hard HEAD "${SLOTH_GITMODULES_BRANCH:-${SLOTH_DEFAULT_BRANCH:-master}}" + git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" checkout --force "${SLOTH_GITMODULES_BRANCH:-${SLOTH_DEFAULT_BRANCH:-main}}" + git::git "${SLOTH_UPDATE_GIT_ARGS[@]}" reset --hard HEAD "${SLOTH_GITMODULES_BRANCH:-${SLOTH_DEFAULT_BRANCH:-main}}" touch "${SLOTH_UPDATED_FILE:-${DOTFILES_PATH:-${HOME}}/.sloth_updated}" rm -f "${SLOTH_UPDATE_AVAILABE_FILE:-"${DOTFILES_PATH:-${HOME}}/.sloth_update_available"}" @@ -312,7 +312,7 @@ sloth_update::gracefully() { fi # Force update - sloth_update::sloth_update "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_GITMODULES_URL:-${SLOTH_DEFAULT_GIT_SSH_URL:-git+ssh://git@github.com:gtrabanco/sloth.git}}" "${SLOTH_DEFAULT_BRANCH:-master}" true || exit_code=$? + sloth_update::sloth_update "${SLOTH_DEFAULT_REMOTE:-origin}" "${SLOTH_GITMODULES_URL:-${SLOTH_DEFAULT_GIT_SSH_URL:-git+ssh://git@github.com:gtrabanco/sloth.git}}" "${SLOTH_DEFAULT_BRANCH:-main}" true || exit_code=$? return $exit_code } @@ -372,41 +372,41 @@ sloth_update::async_success() { if [[ -f "${SLOTH_UPDATE_AVAILABE_FILE:-"${DOTFILES_PATH:-${HOME}}/.sloth_update_available"}" ]]; then case "$(str::to_lower "${SLOTH_AUTO_UPDATE_MODE:-auto}")" in - "silent") - rm -f "${SLOTH_UPDATE_AVAILABE_FILE:-"${DOTFILES_PATH:-${HOME}}/.sloth_update_available"}" - sloth_update::gracefully 2>&1 | log::file "Updating .Sloth" || status=$? + "silent") + rm -f "${SLOTH_UPDATE_AVAILABE_FILE:-"${DOTFILES_PATH:-${HOME}}/.sloth_update_available"}" + sloth_update::gracefully 2>&1 | log::file "Updating .Sloth" || status=$? - [[ $status -eq 0 ]] && sloth_update::exists_migration_script && - output::answer ".Sloth was updated but there are a migration script that must be executed" && - output::empty_line - - return $status - ;; - "info") - output::empty_line - output::write " ---------------------------------------------" - output::write "| πŸ₯³πŸŽ‰πŸΎ NEW .Sloth VERSION AVAILABLE πŸ₯³πŸŽ‰πŸΎ |" - output::write " ---------------------------------------------" + [[ $status -eq 0 ]] && sloth_update::exists_migration_script && + output::answer ".Sloth was updated but there are a migration script that must be executed" && output::empty_line - ;; - "prompt") - # Nothing to do here - ;; - *) # auto - output::answer "πŸš€ Updating .Sloth Automatically" - rm -f "${SLOTH_UPDATE_AVAILABE_FILE:-"${DOTFILES_PATH:-${HOME}}/.sloth_update_available"}" - sloth_update::gracefully 2>&1 | log::file "Updating .Sloth" || status=$? - if [[ $status -eq 0 ]]; then - - if sloth_update::exists_migration_script; then - output::answer ".Sloth was updated but there are a migration script that must be executed. Restart your terminal or execute \`dot core migration --updated\`" - else - output::solution ".Sloth was updated." - fi + + return $status + ;; + "info") + output::empty_line + output::write " ---------------------------------------------" + output::write "| πŸ₯³πŸŽ‰πŸΎ NEW .Sloth VERSION AVAILABLE πŸ₯³πŸŽ‰πŸΎ |" + output::write " ---------------------------------------------" + output::empty_line + ;; + "prompt") + # Nothing to do here + ;; + *) # auto + output::answer "πŸš€ Updating .Sloth Automatically" + rm -f "${SLOTH_UPDATE_AVAILABE_FILE:-"${DOTFILES_PATH:-${HOME}}/.sloth_update_available"}" + sloth_update::gracefully 2>&1 | log::file "Updating .Sloth" || status=$? + if [[ $status -eq 0 ]]; then + + if sloth_update::exists_migration_script; then + output::answer ".Sloth was updated but there are a migration script that must be executed. Restart your terminal or execute \`dot core migration --updated\`" else - output::error ".Sloth was not updated, something was wrong. Check log using \`dot core debug\`" + output::solution ".Sloth was updated." fi - ;; + else + output::error ".Sloth was not updated, something was wrong. Check log using \`dot core debug\`" + fi + ;; esac fi } diff --git a/scripts/core/version b/scripts/core/version index 8c89e74d..ef7dea7c 100755 --- a/scripts/core/version +++ b/scripts/core/version @@ -36,7 +36,7 @@ docs::parse "$@" if ${view_remote:-false}; then SLOTH_DEFAULT_URL="${SLOTH_DEFAULT_URL:-${SLOTH_DEFAULT_GIT_SSH_URL:-git+ssh://git@github.com:gtrabanco/sloth.git}}" - git::git "${SLOTH_UPDATE_GIT_ARGS[@]:-}" ls-remote --tags --refs "$SLOTH_DEFAULT_URL" "v*.*.*" 2> /dev/null | awk '{gsub("\\^{}","", $NF);gsub("refs/tags/v",""); print $NF}' | sort -Vur + git::git "${SLOTH_UPDATE_GIT_ARGS[@]:-}" ls-remote --tags --refs "$SLOTH_DEFAULT_URL" "v*.*.*" 2>/dev/null | awk '{gsub("\\^{}","", $NF);gsub("refs/tags/v",""); print $NF}' | sort -Vur exit 0 fi @@ -56,33 +56,33 @@ if platform::command_exists brew && brew list gtrabanco/tools/dot; then fi case "$selected_tag" in - "stable") - latest_stable="$(sloth_update::get_latest_stable_version)" - if [[ -n "$latest_stable" ]]; then - # "${SLOTH_UPDATE_GIT_ARGS[@]:-}" - git::sloth_repository_exec git checkout -q "$latest_stable" - output::solution "Switched to latest .Sloth stable version $latest_stable" - templating::modify_bash_file_variable "$DOTFILES_PATH/shell/exports.sh" "SLOTH_UPDATE_VERSION" "stable" - else - output::error "No releases locally yet" - fi - ;; - "latest") - templating::modify_bash_file_variable "$DOTFILES_PATH/shell/exports.sh" "SLOTH_UPDATE_VERSION" "latest" - git::git "${SLOTH_UPDATE_GIT_ARGS[@]:-}" checkout -q "${SLOTH_GITMODULES_BRANCH:-${SLOTH_DEFAULT_BRANCH:-master}}" - ;; - *) - if [[ -n "$selected_tag" ]] && check_local_tag_exists "$selected_tag"; then - templating::modify_bash_file_variable "$DOTFILES_PATH/shell/exports.sh" "SLOTH_UPDATE_VERSION" "$selected_tag" - git::git "${SLOTH_UPDATE_GIT_ARGS[@]:-}" checkout -q "$selected_tag" - output::solution "Switched to Sloth version $selected_tag" +"stable") + latest_stable="$(sloth_update::get_latest_stable_version)" + if [[ -n "$latest_stable" ]]; then + # "${SLOTH_UPDATE_GIT_ARGS[@]:-}" + git::sloth_repository_exec git checkout -q "$latest_stable" + output::solution "Switched to latest .Sloth stable version $latest_stable" + templating::modify_bash_file_variable "$DOTFILES_PATH/shell/exports.sh" "SLOTH_UPDATE_VERSION" "stable" + else + output::error "No releases locally yet" + fi + ;; +"latest") + templating::modify_bash_file_variable "$DOTFILES_PATH/shell/exports.sh" "SLOTH_UPDATE_VERSION" "latest" + git::git "${SLOTH_UPDATE_GIT_ARGS[@]:-}" checkout -q "${SLOTH_GITMODULES_BRANCH:-${SLOTH_DEFAULT_BRANCH:-main}}" + ;; +*) + if [[ -n "$selected_tag" ]] && check_local_tag_exists "$selected_tag"; then + templating::modify_bash_file_variable "$DOTFILES_PATH/shell/exports.sh" "SLOTH_UPDATE_VERSION" "$selected_tag" + git::git "${SLOTH_UPDATE_GIT_ARGS[@]:-}" checkout -q "$selected_tag" + output::solution "Switched to Sloth version $selected_tag" - output::answer ".Sloth is update locked. Which mean that can not be updated." - output::answer "To unlock updates type: \`dot self update --enable\` command" - touch "${SLOTH_FORCE_CURRENT_VERSION_FILE:-$DOTFILES_PATH/.sloth_force_current_version}" - elif [[ -n "$selected_tag" ]] && ! check_local_tag_exists "$selected_tag"; then - output::error "Selected version does not exists locally" - exit 1 - fi - ;; + output::answer ".Sloth is update locked. Which mean that can not be updated." + output::answer "To unlock updates type: \`dot self update --enable\` command" + touch "${SLOTH_FORCE_CURRENT_VERSION_FILE:-$DOTFILES_PATH/.sloth_force_current_version}" + elif [[ -n "$selected_tag" ]] && ! check_local_tag_exists "$selected_tag"; then + output::error "Selected version does not exists locally" + exit 1 + fi + ;; esac