Skip to content

Commit

Permalink
dorothy: rejig clone and update to all support slug/branch/reference/…
Browse files Browse the repository at this point in the history
…remote-name

also remove update technique for legacy dorothy installations
  • Loading branch information
balupton committed Oct 10, 2024
1 parent 47f29cb commit b60ef24
Showing 1 changed file with 57 additions and 46 deletions.
103 changes: 57 additions & 46 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,6 @@ function dorothy_() (
XDG_STATE_HOME="$HOME/.local/state"
fi

# =====================================
# Variables

# vars that are local to this script and shared between functions
local dorothy_slug='bevry/dorothy'
local dorothy_upstream_git_https="https://github.com/$dorothy_slug.git"
local dorothy_upstream_branch='master'
local dorothy_upstream_remote_name='bevry'
local dorothy_homepage="https://github.com/$dorothy_slug"
local dorothy_install="$dorothy_homepage#install"
local dorothy_worker='https://dorothy.bevry.me'
local dorothy_worker_run="$dorothy_worker/run"

# =====================================
# Arguments

Expand All @@ -103,11 +90,17 @@ function dorothy_() (
--help, -h
Show this help information.
--slug=<slug>
If Dorothy needs to be installed/updated, checkout this slug.
--branch=<branch>
If Dorothy needs to be installed, checkout this branch.
If Dorothy needs to be installed/updated, checkout this branch.
--reference=<reference> | --commit=<reference>
If Dorothy needs to be installed/updated, checkout this reference.
--commit=<commit>
If Dorothy needs to be installed, checkout this commit.
--remote-name=<remote>
If Dorothy needs to be installed/updated, use this remote name.
--xdg | --no-xdg
Expand Down Expand Up @@ -197,20 +190,21 @@ function dorothy_() (

# process action arguments
# @todo prevent double-action overwrite
local item action='' option_xdg='' option_branch='' option_reference='HEAD' option_slug="$dorothy_slug" option_args=()
local item action='' option_xdg='' option_slug option_branch option_reference option_remote_name option_args=()
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--xdg' | '--xdg=yes') option_xdg='yes' ;;
'--no-xdg' | '--xdg=no') option_xdg='no' ;;
'--slug='*) option_slug="${item#*=}" ;;
'--branch='*)
option_branch="${item#*=}"
option_reference="$option_branch"
;;
'--commit='* | '--ref='* | '--reference='*) option_reference="${item#*=}" ;;
'--slug='*) option_slug="${item#*=}" ;;
'--remote-name='* | '--remote='*) option_remote_name="${item#*=}" ;;
'repl')
action='repl'
option_args+=("$@")
Expand Down Expand Up @@ -275,6 +269,31 @@ function dorothy_() (
help "<action> must be provided."
fi

# =====================================
# Variables

# defaults
local dorothy_slug='bevry/dorothy'
local dorothy_reference='HEAD'
local dorothy_homepage="https://github.com/$dorothy_slug"
local dorothy_install="$dorothy_homepage#install"
local dorothy_worker='https://dorothy.bevry.me'
local dorothy_worker_run="$dorothy_worker/run"

# options
local the_slug="${option_slug:-"$dorothy_slug"}"
local the_branch="${option_branch-}"
local the_reference="${option_reference:-"$dorothy_reference"}"
local the_upstream_remote_name="${option_remote_name:-"${the_slug##*/}"}"
local the_upstream_git_https="https://github.com/$the_slug.git"
local the_bash_url="https://raw.githubusercontent.com/$the_slug/$the_reference/sources/bash.bash"
local the_browse_url
if test "$the_slug" = "$dorothy_slug" -a "$the_reference" = "$dorothy_reference"; then
the_browse_url="https://github.com/$the_slug"
else
the_browse_url="https://github.com/$the_slug/tree/$the_reference"
fi

# =====================================
# Sourcing

Expand All @@ -284,7 +303,7 @@ function dorothy_() (
source "$DOROTHY/sources/bash.bash"
else
# we are the remote [dorothy] command, so match with the remote [bash.bash]
eval "$(curl -fsSL "https://raw.githubusercontent.com/$option_slug/$option_reference/sources/bash.bash")"
eval "$(curl -fsSL "$the_bash_url")"
fi

# =====================================
Expand Down Expand Up @@ -804,50 +823,42 @@ function dorothy_() (

# helpers
function install_dorothy {
local clone_args=('--no-tags') # don't do [--depth 1] and [--single-branch] as a full history is required to do any checkouts
__print_lines "Installing Dorothy into $DOROTHY"
if test -n "$option_branch"; then
local clone_args=(
'--quiet' # --quiet to suppress 'Cloning into...' feedback
'--no-tags' # don't do [--depth 1] and [--single-branch] as a full history is required to do any checkouts
)
__print_lines "Installing Dorothy into $DOROTHY from $the_browse_url"

# setup repo with branch
if test -n "$the_branch"; then
clone_args+=(
'--branch'
"$option_branch"
)
fi
__eval_wrap -- git clone "${clone_args[@]}" "https://github.com/$option_slug.git" "$DOROTHY"
if test -n "$option_reference"; then
(
cd "$DOROTHY"
__eval_wrap -- git checkout --quiet "$option_reference" # --quiet to suppress "detached HEAD" feedback
"$the_branch"
)
fi
__eval_wrap -- git clone "${clone_args[@]}" "$the_upstream_git_https" "$DOROTHY"

# setup repo with reference
(
cd "$DOROTHY"
__eval_wrap -- git checkout --quiet "$the_reference" # --quiet to suppress "detached HEAD" feedback
)
}
function __update_modern_dorothy {
if __command_exists -- echo-style git-helper; then
echo-style --h2="Updating the Dorothy installation at $DOROTHY" || return
echo-style --h2="Updating the Dorothy installation at $DOROTHY from $the_browse_url" || return
# update origin first
git-helper update --path="$DOROTHY" || return
# update upstream second
git-helper update --path="$DOROTHY" -- --remote-name="$dorothy_upstream_remote_name" --remote-url="$dorothy_upstream_git_https" || return
git-helper update --path="$DOROTHY" -- --remote-name="$the_upstream_remote_name" --remote-url="$the_upstream_git_https" || return
echo-style --g2="Updating the Dorothy installation at $DOROTHY" || return
else
return 1
fi
}
function __update_legacy_dorothy {
(
__print_lines "Updating Dorothy installation at $DOROTHY" || return
cd "$DOROTHY" || return
git config pull.ff only || return # avoid merge conflicts
# in case they have forked dorothy, we need to pull from the upstream
# we do not have the intelligence yet to update origin and upstream, so we will just update upstream
git remote remove "$dorothy_upstream_remote_name" 2>/dev/null || : # don't care if doesn't exist, and remove to ensure desired URL is applied
git remote add --fetch "$dorothy_upstream_remote_name" "$dorothy_upstream_git_https" || return
__eval_wrap -- git pull "$dorothy_upstream_remote_name" "$dorothy_upstream_branch" || return
__print_lines "Successfully updated Dorothy installation at $DOROTHY" || return
) || return
}
function update_dorothy {
# https://github.com/bevry/dorothy/issues/215
__update_modern_dorothy || __update_legacy_dorothy || {
__update_modern_dorothy || {
__print_lines '...Dorothy was unable to be automatically updated. Please manually update Dorothy and try again.'
return 1
}
Expand Down

0 comments on commit b60ef24

Please sign in to comment.