Skip to content

Commit

Permalink
Added new script 'scripts/add-upstream-git-config.sh' (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
vraravam committed Jan 2, 2025
1 parent 271031f commit 9113432
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions Extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Basically, to get started with the dotfiles, you just need to run the `<pwd>/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.
Expand Down
48 changes: 48 additions & 0 deletions scripts/add-upstream-git-config.sh
Original file line number Diff line number Diff line change
@@ -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} <target-folder> <upstream-repo-owner>")"
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}")'"
15 changes: 6 additions & 9 deletions scripts/fresh-install-of-osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9113432

Please sign in to comment.