Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions bin/omarchy-plugin-remove
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ valid_plugin_id() {
[[ $1 =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ && $1 != *..* ]]
}

# True only for a git checkout that holds nothing upstream lacks: no
# uncommitted, untracked or ignored files, and no commit or stash of its own.
# `omarchy plugin update` merges FETCH_HEAD without moving the remote-tracking
# refs, so FETCH_HEAD counts as upstream too. Anything git cannot answer counts
# as local.
upstream_only_checkout() {
local dir="$1"
local upstream=(--remotes)
local changes

[[ -d $dir/.git ]] || return 1
changes=$(git -C "$dir" status --porcelain --untracked-files=normal --ignored) || return 1
[[ -z $changes ]] || return 1
if git -C "$dir" rev-parse -q --verify FETCH_HEAD >/dev/null; then
upstream+=(FETCH_HEAD)
fi
changes=$(git -C "$dir" rev-list -n 1 --all --not "${upstream[@]}") || return 1
[[ -z $changes ]]
}

id=""

while (( $# > 0 )); do
Expand Down Expand Up @@ -84,9 +104,14 @@ if [[ -f $target/manifest.json ]]; then
cloned_from=$(jq -r '.omarchy.clonedFrom // empty' "$target/manifest.json")
fi

disposable=0
if [[ ! -L $target ]] && upstream_only_checkout "$target"; then
disposable=1
fi

if [[ -L $target ]]; then
confirm "$(omarchy-i18n plugin.confirmUnlink "$id" "$(readlink "$target")")" || fail "aborted"
elif [[ -d $target/.git ]]; then
elif (( disposable )); then
confirm "$(omarchy-i18n plugin.confirmDelete "$id")" || fail "aborted"
else
confirm "$(omarchy-i18n plugin.confirmRemove "$id")" || fail "aborted"
Expand All @@ -99,7 +124,7 @@ fi
if [[ -L $target ]]; then
rm -f "$target"
echo "Unlinked $id."
elif [[ -d $target/.git ]]; then
elif (( disposable )); then
rm -rf "$target"
echo "Removed $id."
else
Expand Down
2 changes: 1 addition & 1 deletion manual/32-shell-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ With no id it updates every git-managed plugin you have. It shows you the diff b
omarchy plugin remove acme.weather
```

Removal disables the plugin first, then deletes it if it's a git checkout (the repo is still upstream) or unlinks it if it's a symlink. A hand-made plugin folder with no git repo gets moved to a timestamped backup inside the plugins directory instead of being deleted outright.
Removal disables the plugin first, then deletes it if it's a git checkout holding nothing that isn't also upstream, or unlinks it if it's a symlink. A checkout with uncommitted, untracked or ignored files, unpushed commits, or stashes, and a hand-made plugin folder with no git repo, get moved to a timestamped backup inside the plugins directory instead of being deleted outright.

## Cloning a built-in to modify it

Expand Down
124 changes: 124 additions & 0 deletions test/shell.d/plugin-remove-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT

stub_dir="$TMPDIR/stubs"
mkdir -p "$stub_dir"
cat >"$stub_dir/omarchy-shell" <<'STUB'
#!/bin/bash
exit 0
STUB
chmod +x "$stub_dir/omarchy-shell"

test_home="$TMPDIR/home"
plugins_dir="$test_home/.config/omarchy/plugins"

run() {
HOME="$test_home" OMARCHY_PATH="$ROOT" PATH="$stub_dir:$ROOT/bin:$PATH" "$@" 2>&1
}

git_test() {
git -c user.name=Test -c user.email=test@example.com "$@"
}

# Each plugin is added from an upstream repo of its own, as `omarchy plugin add`
# would clone it from a forge or a local checkout.
add_plugin() {
local id="$1"
local upstream="$TMPDIR/upstream/$id"

mkdir -p "$upstream"
cat >"$upstream/manifest.json" <<JSON
{
"schemaVersion": 1,
"id": "$id",
"name": "$id",
"version": "1.0.0",
"kinds": ["bar-widget"],
"entryPoints": { "barWidget": "Widget.qml" },
"barWidget": {
"displayName": "$id",
"category": "Test",
"allowMultiple": false
}
}
JSON
printf 'import QtQuick\nItem {}\n' >"$upstream/Widget.qml"
git -C "$upstream" init -q
git -C "$upstream" add .
git_test -C "$upstream" commit -qm "Initial"

output=$(run omarchy-plugin-add "$upstream" --yes) || fail "plugin add installs $id" "$output"
}

backup_of() {
find "$plugins_dir" -mindepth 1 -maxdepth 1 -name ".$1.bak.*" -print -quit
}

remove_plugin() {
output=$(run omarchy-plugin-remove "$1" --yes) || fail "plugin remove removes $1" "$output"
[[ ! -e $plugins_dir/$1 ]] || fail "plugin remove leaves $1 installed" "$output"
}

add_plugin acme.clean
remove_plugin acme.clean
[[ -z $(backup_of acme.clean) ]] || fail "plugin remove backs up a checkout with nothing local"
pass "plugin remove deletes a checkout whose commits are all upstream"

# `omarchy plugin update` merges FETCH_HEAD without moving the remote-tracking
# refs, so the commits it brought in are upstream too.
add_plugin acme.updated
printf '// upstream\n' >>"$TMPDIR/upstream/acme.updated/Widget.qml"
git_test -C "$TMPDIR/upstream/acme.updated" commit -qam "Upstream change"
output=$(run omarchy-plugin-update acme.updated --yes) || fail "plugin update fast-forwards acme.updated" "$output"
remove_plugin acme.updated
[[ -z $(backup_of acme.updated) ]] || fail "plugin remove backs up a checkout that only holds updates from upstream"
pass "plugin remove deletes a checkout updated from upstream"

add_plugin acme.edited
printf '// local edit\n' >>"$plugins_dir/acme.edited/Widget.qml"
remove_plugin acme.edited
backup=$(backup_of acme.edited)
[[ -n $backup ]] && grep -qF '// local edit' "$backup/Widget.qml" ||
fail "plugin remove drops uncommitted changes" "$output"
pass "plugin remove backs up a checkout with uncommitted changes"

add_plugin acme.untracked
printf 'import QtQuick\nItem {}\n' >"$plugins_dir/acme.untracked/Extra.qml"
remove_plugin acme.untracked
backup=$(backup_of acme.untracked)
[[ -n $backup && -f $backup/Extra.qml ]] || fail "plugin remove drops untracked files" "$output"
pass "plugin remove backs up a checkout with untracked files"

add_plugin acme.ignored
printf 'secret.json\n' >"$TMPDIR/upstream/acme.ignored/.gitignore"
git -C "$TMPDIR/upstream/acme.ignored" add .gitignore
git_test -C "$TMPDIR/upstream/acme.ignored" commit -qm "Ignore local secrets"
output=$(run omarchy-plugin-update acme.ignored --yes) || fail "plugin update fast-forwards acme.ignored" "$output"
printf '{}\n' >"$plugins_dir/acme.ignored/secret.json"
remove_plugin acme.ignored
backup=$(backup_of acme.ignored)
[[ -n $backup && -f $backup/secret.json ]] || fail "plugin remove drops ignored files" "$output"
pass "plugin remove backs up a checkout with ignored files"

add_plugin acme.committed
printf '// local commit\n' >>"$plugins_dir/acme.committed/Widget.qml"
git_test -C "$plugins_dir/acme.committed" commit -qam "Local change"
remove_plugin acme.committed
backup=$(backup_of acme.committed)
[[ -n $backup && $(git -C "$backup" log -1 --format=%s) == "Local change" ]] ||
fail "plugin remove drops commits that were never pushed" "$output"
pass "plugin remove backs up a checkout with unpushed commits"

add_plugin acme.stashed
printf '// stashed\n' >>"$plugins_dir/acme.stashed/Widget.qml"
git_test -C "$plugins_dir/acme.stashed" stash push -q
remove_plugin acme.stashed
backup=$(backup_of acme.stashed)
[[ -n $backup && -n $(git -C "$backup" stash list) ]] || fail "plugin remove drops stashed changes" "$output"
pass "plugin remove backs up a checkout with stashed changes"