From 911343287df4403bec82389e2cf39c9e838a6c48 Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Thu, 2 Jan 2025 13:49:43 +0530 Subject: [PATCH] Added new script 'scripts/add-upstream-git-config.sh' (#18) --- CHANGELOG.md | 4 +++ Extras.md | 4 +++ scripts/add-upstream-git-config.sh | 48 ++++++++++++++++++++++++++++++ scripts/fresh-install-of-osx.sh | 15 ++++------ 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100755 scripts/add-upstream-git-config.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 1caa88c..7c93a61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ As documented in the README's [adopting](README.md#how-to-adoptcustomize-the-scr For those who follow this repo, here's the changelog for ease of adoption: +### 1.0-41 + +* Added new script `scripts/add-upstream-git-config.sh`. + ### 1.0-40 * Fixed documentation and reduced hardcoding of upstream repo-owner's name. diff --git a/Extras.md b/Extras.md index a0ccb2d..f3fe6a2 100644 --- a/Extras.md +++ b/Extras.md @@ -8,6 +8,10 @@ Basically, to get started with the dotfiles, you just need to run the `/scr * If you do not want a specific file from the home folder to be overridden, simply delete it from this repo's `files` folder - and it will not be processed. * If you wish to add a new file to be tracked and managed via this backup mechanism, simply add it into the `files` folder with the requisite relative path (relative to your `HOME` folder) - and it will be processed. +## add-upstream-git-config.sh + +This script can be used to quickly add a new upstream remote to the specified git repo. The name of the new remote is hardcoded to `upstream`. The rest of the url remains the same with just the username switched to the specified username. + ## approve-fingerprint-sudo.sh This script is useful in macos to enable TouchId as an authentication mechanism even while running command-line tools. Before running this script, the usual mechanism is for a prompt to appear in the terminal window itself where one has to type in the whole long password. After this script is run, the user is prompted by the touchId modal dialog instead of having to type a really long password. diff --git a/scripts/add-upstream-git-config.sh b/scripts/add-upstream-git-config.sh new file mode 100755 index 0000000..b64082b --- /dev/null +++ b/scripts/add-upstream-git-config.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env zsh + +# vim:filetype=zsh syntax=zsh tabstop=2 shiftwidth=2 softtabstop=2 expandtab autoindent fileencoding=utf-8 + +# This script will check and add a new remote called 'upstream' to the specified git repo +# TODO: Need to decide whether this script is best kept standalone or converted to a function that's used only within the fresh-install script (or) moved into the global .gitconfig so as to be used as a git alias. Each of these has their own pros & cons which need to be analyzed. + +type red &> /dev/null 2>&1 || source "${HOME}/.shellrc" +type section_header &> /dev/null 2>&1 || source "${HOME}/.shellrc" + +usage() { + echo "$(red 'Usage'): $(yellow "${1} ")" + echo " $(yellow 'target-folder') --> The folder which has to be processed" + echo " $(yellow 'upstream-repo-owner') --> The upstream repo's owner" + exit 1 +} + +[ $# -ne 2 ] && usage "${0}" + +local target_folder="${1}" +local upstream_repo_owner="${2}" + +section_header "Adding new upstream to: '$(yellow "${target_folder}")'" + +! is_git_repo "${target_folder}" && warn "'${target_folder}' is not a git repo; Aborting!!!" && return + +git -C "${target_folder}" remote -vv | \grep "${upstream_repo_owner}" +if [ $? -eq 0 ]; then + warn "skipping setting new upstream remote for the repo in '$(yellow "${target_folder}")' since the existing remote(s) alerady point to the target owner '$(yellow "${upstream_repo_owner}")'" + return +fi + +existing_upstream="$(git -C "${target_folder}" config remote.upstream.url)" +if [ $? -eq 0 ]; then + warn "remote 'upstream' already exists for the repo in '$(yellow "${target_folder}")': '$(yellow "${existing_upstream}")'" + return +fi + +local origin_remote_url="$(git -C "${target_folder}" config remote.origin.url)" +if [[ "${origin_remote_url}" =~ 'git@' ]]; then + local cloned_repo_owner="$(echo "${origin_remote_url}" | cut -d '/' -f1 | cut -d ':' -f2)" +elif [[ "${origin_remote_url}" =~ 'https:' ]]; then + local cloned_repo_owner="$(echo "${origin_remote_url}" | cut -d '/' -f4)" +fi +local new_repo_url="$(echo "${origin_remote_url}" | sed "s/${cloned_repo_owner}/${upstream_repo_owner}/")" +git -C "${target_folder}" remote add upstream "${new_repo_url}" +git -C "${target_folder}" fetch --all +success "Successfully set new upstream remote for the repo in '$(yellow "${target_folder}")'" diff --git a/scripts/fresh-install-of-osx.sh b/scripts/fresh-install-of-osx.sh index f67b61d..de31327 100755 --- a/scripts/fresh-install-of-osx.sh +++ b/scripts/fresh-install-of-osx.sh @@ -210,14 +210,7 @@ if is_non_zero_string "${DOTFILES_DIR}" && ! is_git_repo "${DOTFILES_DIR}"; then approve-fingerprint-sudo.sh # Setup the DOTFILES_DIR repo's upstream if it doesn't already point to UPSTREAM_GH_USERNAME's repo - git -C "${DOTFILES_DIR}" remote -vv | grep "${UPSTREAM_GH_USERNAME}" - if [ $? -ne 0 ]; then - git -C "${DOTFILES_DIR}" remote add upstream "https://github.com/${UPSTREAM_GH_USERNAME}/dotfiles" - git -C "${DOTFILES_DIR}" fetch --all - success 'Successfully set new upstream remote for the dotfiles repo' - else - warn 'skipping setting new upstream remote for the dotfiles repo' - fi + add-upstream-git-config.sh "${DOTFILES_DIR}" "${UPSTREAM_GH_USERNAME}" else warn "skipping cloning the dotfiles repo since '${DOTFILES_DIR}' is either not defined or is already a git repo" fi @@ -360,7 +353,11 @@ if is_non_zero_string "${KEYBASE_USERNAME}"; then clone_repo_into "$(build_keybase_repo_url "${KEYBASE_PROFILES_REPO_NAME}")" "${PERSONAL_PROFILES_DIR}" # Clone the natsumi-browser repo into the ZenProfile/Profiles/chrome folder and switch to the 'dev' branch - is_directory "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/" && clone_repo_into "git@github.com:${UPSTREAM_GH_USERNAME}/natsumi-browser" "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/chrome" dev + if is_directory "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/"; then + clone_repo_into "git@github.com:${UPSTREAM_GH_USERNAME}/natsumi-browser" "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/chrome" dev + # Setup the zen chrome repo's upstream if it doesn't already point to UPSTREAM_GH_USERNAME's repo + add-upstream-git-config.sh "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/chrome" "${UPSTREAM_GH_USERNAME}" + fi else warn "skipping cloning of profiles repo since either the 'KEYBASE_PROFILES_REPO_NAME' or the 'PERSONAL_PROFILES_DIR' env var hasn't been set" fi