Repository navigation
Repair Copy URL after install-time migration stamping #8153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
chessing234
wants to merge
8
commits into
omacom:quattro
from
chessing234:fix/copy-url-repair-fresh-install
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c5c9152
Repair Copy URL after install-time migration stamping
chessing234 40fc364
Merge quattro and retire verified Copy URL repair backups
chessing234 9d17935
Preserve recovery backups and honor browser sockets during repair
chessing234 213ccc0
Distinguish live browser sockets from abandoned paths
chessing234 0813b2b
Merge upstream quattro.
chessing234 2001636
Merge Copy URL repair with current first-run coverage
chessing234 e9cee47
Merge remote-tracking branch 'origin/quattro' into fix/copy-url-repai…
chessing234 2ebea4f
Keep extracted Copy URL repair compatible with existing profiles
omarchybot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,314 @@ | ||
| #!/bin/bash | ||
|
|
||
| # omarchy:summary=Repair a stale Copy URL shortcut in Chromium-family Preferences | ||
| # omarchy:hidden=true | ||
| # omarchy:args=[--prompt] | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Chromium never hands a suggested shortcut to one extension while another — | ||
| # even a long-gone one — still holds the registration. Profiles that first | ||
| # loaded Copy URL before its id was pinned registered Alt+Shift+L under an id | ||
| # derived from the extension's load path at the time, so the pinned extension | ||
| # never receives the shortcut and the keypress does nothing. Those historical | ||
| # ids are unknowable in general (they hash long-gone absolute paths), but the | ||
| # registration itself names the command: rebind any copy-url command that | ||
| # points away from the pinned id, unless that id belongs to an extension that | ||
| # is actually installed. | ||
| # | ||
| # Fresh installs stamp every shipped migration complete, so this cannot live | ||
| # only as a migration: a Chromium profile that arrives later (sync, restored | ||
| # ~/.config/chromium) would never be repaired. Callers that are not | ||
| # migration-stamped invoke this without --prompt. --prompt is for omarchy-migrate | ||
| # on unstamped upgrades, where a running browser can be asked to close. | ||
|
|
||
| prompt=0 | ||
| if [[ ${1:-} == "--prompt" ]]; then | ||
| prompt=1 | ||
| fi | ||
|
|
||
| pending=() | ||
|
|
||
| pinned_id="bgpiichlckmfanooecilcjemknkcpngb" | ||
|
|
||
| repair_py=$(cat <<'PY' | ||
| import json | ||
| import os | ||
| import shutil | ||
| import sys | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| path = Path(sys.argv[1]) | ||
| pinned_id = sys.argv[2] | ||
| mode = sys.argv[3] | ||
|
|
||
| try: | ||
| preferences = json.loads(path.read_text()) | ||
| except (OSError, ValueError): | ||
| sys.exit(1) | ||
|
|
||
| if not isinstance(preferences, dict): | ||
| sys.exit(1) | ||
| if mode == "readable": | ||
| sys.exit(0) | ||
|
|
||
| extensions = preferences.get("extensions", {}) | ||
| commands = extensions.get("commands", {}) | ||
| settings = extensions.get("settings", {}) | ||
|
|
||
| def ghost_id(command): | ||
| if command.get("command_name") != "copy-url": | ||
| return None | ||
| extension = command.get("extension") | ||
| if not extension or extension == pinned_id: | ||
| return None | ||
| # An installed extension records its install path in settings; only the | ||
| # ghosts of Copy URL itself may be rebound. | ||
| install_path = settings.get(extension, {}).get("path", "") | ||
| if install_path and not install_path.endswith("/default/chromium/extensions/copy-url"): | ||
| return None | ||
| return extension | ||
|
|
||
| if mode == "check": | ||
| sys.exit(0 if any(ghost_id(c) for c in commands.values()) else 1) | ||
|
|
||
| if mode == "verify": | ||
| if any(ghost_id(c) for c in commands.values()): | ||
| sys.exit(1) | ||
| path.with_name(path.name + ".omarchy-copy-url-repair.pending.bak").unlink(missing_ok=True) | ||
| sys.exit(0) | ||
|
|
||
| # Chromium keeps one shortcut per command, so a ghost may only be rebound | ||
| # when the pinned extension holds no copy-url binding of its own; further | ||
| # ghosts are dropped rather than doubled up. | ||
| pinned_bound = any( | ||
| c.get("command_name") == "copy-url" and c.get("extension") == pinned_id | ||
| for c in commands.values() | ||
| ) | ||
|
|
||
| changed = False | ||
| for accelerator in list(commands): | ||
| ghost = ghost_id(commands[accelerator]) | ||
| if not ghost: | ||
| continue | ||
| if pinned_bound: | ||
| del commands[accelerator] | ||
| else: | ||
| commands[accelerator]["extension"] = pinned_id | ||
| pinned_bound = True | ||
| pinned_command = settings.get(pinned_id, {}).get("commands", {}).get("copy-url", {}) | ||
| if pinned_command: | ||
| pinned_command["was_assigned"] = True | ||
| settings.pop(ghost, None) | ||
| changed = True | ||
|
|
||
| if changed: | ||
| shutil.copy2(path, path.with_name(path.name + ".omarchy-copy-url-repair.pending.bak")) | ||
| text = json.dumps(preferences, separators=(",", ":")) | ||
| fd, tmp_name = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.") | ||
| try: | ||
| with os.fdopen(fd, "w") as tmp: | ||
| tmp.write(text) | ||
| tmp.flush() | ||
| os.fsync(tmp.fileno()) | ||
| os.chmod(tmp_name, path.stat().st_mode) | ||
| os.replace(tmp_name, path) | ||
| except Exception: | ||
| try: | ||
| os.unlink(tmp_name) | ||
| except OSError: | ||
| pass | ||
| raise | ||
| PY | ||
| ) | ||
|
|
||
| profile_roots=( | ||
| "$HOME/.config/chromium" | ||
| "$HOME/.config/google-chrome" | ||
| "$HOME/.config/google-chrome-beta" | ||
| "$HOME/.config/google-chrome-unstable" | ||
| "$HOME/.config/BraveSoftware/Brave-Browser" | ||
| "$HOME/.config/BraveSoftware/Brave-Browser-Beta" | ||
| "$HOME/.config/BraveSoftware/Brave-Browser-Nightly" | ||
| "$HOME/.config/BraveSoftware/Brave-Origin" | ||
| "$HOME/.config/BraveSoftware/Brave-Origin-Beta" | ||
| "$HOME/.config/BraveSoftware/Brave-Origin-Nightly" | ||
| "$HOME/.config/microsoft-edge" | ||
| "$HOME/.config/microsoft-edge-beta" | ||
| "$HOME/.config/microsoft-edge-dev" | ||
| "$HOME/.config/vivaldi" | ||
| "$HOME/.config/opera" | ||
| "$HOME/.config/helium" | ||
| ) | ||
|
|
||
| find_pending() { | ||
| pending=() | ||
| for profile_root in "${profile_roots[@]}"; do | ||
| [[ -d $profile_root ]] || continue | ||
|
|
||
| for preferences in "$profile_root"/*/Preferences; do | ||
| [[ -f $preferences ]] || continue | ||
| python3 -c "$repair_py" "$preferences" "$pinned_id" check || continue | ||
| pending+=("$preferences") | ||
| done | ||
| done | ||
| } | ||
|
|
||
| # Pending backups mark repairs this command has not verified. Ordinary .bak | ||
| # files came from shipped migrations and do not mean a repair is still pending. | ||
| unverified_repairs_exist() { | ||
| local profile_root backup | ||
|
|
||
| for profile_root in "${profile_roots[@]}"; do | ||
| for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.pending.bak; do | ||
| [[ -f $backup ]] || continue | ||
| python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.pending.bak}" "$pinned_id" readable && return 0 | ||
| done | ||
| done | ||
|
|
||
| return 1 | ||
| } | ||
|
|
||
| # A browser only rewrites its own Preferences on exit, so a repair can only be | ||
| # reverted by a browser attached to a profile this command has to touch. | ||
| # Chromium writes SingletonLock as a symlink to <hostname>-<pid>; a lock left | ||
| # behind by SIGTERM still looks like a lock if we only test for the symlink. | ||
| # Trust a live PID (or a non-symlink lock / live socket), not a dangling one. | ||
| socket_open() { | ||
| [[ -S $1 ]] || return 1 | ||
| python3 - "$1" <<'PY_SOCKET' | ||
| import errno | ||
| import socket | ||
| import sys | ||
|
|
||
| try: | ||
| with socket.socket(socket.AF_UNIX) as sock: | ||
| sock.settimeout(0.2) | ||
| sock.connect(sys.argv[1]) | ||
| except OSError as error: | ||
| # A refused connection proves the socket is abandoned. Other failures, | ||
| # including permission denial or timeout, cannot prove the profile idle. | ||
| sys.exit(1 if error.errno in (errno.ECONNREFUSED, errno.ENOENT) else 0) | ||
| PY_SOCKET | ||
| } | ||
|
|
||
| profile_open() { | ||
| local profile=$1 | ||
| local lock=$profile/SingletonLock | ||
|
|
||
| if [[ -L $lock ]]; then | ||
| local target pid | ||
| target=$(readlink "$lock") || return 1 | ||
| pid=${target##*-} | ||
| if [[ $pid =~ ^[0-9]+$ ]] && kill -0 "$pid" 2>/dev/null; then | ||
| return 0 | ||
| fi | ||
| socket_open "$profile/SingletonSocket" | ||
| return | ||
| fi | ||
|
|
||
| [[ -e $lock ]] || socket_open "$profile/SingletonSocket" | ||
| } | ||
|
|
||
| # Gate on a pending — or to-be-verified — profile actually being open, not on | ||
| # browsers in general. Waiting on every browser process deadlocks the update on | ||
| # machines whose main browser is effectively never closed: the pending ghosts | ||
| # commonly sit in a stale profile nobody has open, yet the update blocks on the | ||
| # always-open one. | ||
| affected_profile_open() { | ||
| local preferences backup profile_root | ||
|
|
||
| if (( ${#pending[@]} > 0 )); then | ||
| for preferences in "${pending[@]}"; do | ||
| profile_open "$(dirname "$(dirname "$preferences")")" && return 0 | ||
| done | ||
| fi | ||
|
|
||
| for profile_root in "${profile_roots[@]}"; do | ||
| for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.pending.bak; do | ||
| [[ -f $backup ]] || continue | ||
| python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.pending.bak}" "$pinned_id" readable || continue | ||
| profile_open "$(dirname "$(dirname "$backup")")" && return 0 | ||
| done | ||
| done | ||
|
|
||
| return 1 | ||
| } | ||
|
|
||
| defer_because_browser_running() { | ||
| if (( prompt )); then | ||
| echo "A running browser would undo the Copy URL shortcut repair." >&2 | ||
| echo "Close the browser windows, then run: omarchy-migrate" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| exit 0 | ||
| } | ||
|
|
||
| find_pending | ||
| if (( ! ${#pending[@]} )) && ! unverified_repairs_exist; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # A running browser holds Preferences in memory and rewrites the file on exit, | ||
| # reverting the repair. Quiet callers (login and first-run) skip | ||
| # and retry later. Prompted migrations ask for the affected windows to close. | ||
| # gum draws the prompt on stderr, so it must stay attached: suppressing it | ||
| # leaves gum waiting for a keypress behind an unpainted screen. Without a | ||
| # terminal to ask in, gum fails; then — as on decline — fail so the migration | ||
| # stays pending, and the login notifier keeps prompting until a rerun goes | ||
| # through with the affected profiles closed. | ||
| while affected_profile_open; do | ||
| if (( ! prompt )); then | ||
| defer_because_browser_running | ||
| fi | ||
|
|
||
| if ! gum confirm "Close the browser windows to repair the Copy URL shortcut, then continue"; then | ||
| defer_because_browser_running | ||
| fi | ||
| done | ||
|
|
||
| # A browser that just closed may have restored ghosts an earlier repair had | ||
| # already removed, so only now is the pending list authoritative. | ||
| find_pending | ||
|
|
||
| if (( ${#pending[@]} > 0 )); then | ||
| for preferences in "${pending[@]}"; do | ||
| python3 -c "$repair_py" "$preferences" "$pinned_id" repair | ||
| done | ||
| fi | ||
|
|
||
| # A browser that started mid-repair read the stale Preferences and will write | ||
| # them back on exit; stay pending so the next browser-free run can verify the | ||
| # repair stuck. | ||
| if affected_profile_open; then | ||
| if (( prompt )); then | ||
| echo "A browser started during the Copy URL shortcut repair and may undo it on exit." >&2 | ||
| echo "Close the browser windows, then run: omarchy-migrate" >&2 | ||
| exit 1 | ||
| fi | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Verify all attempted repairs while the profiles are closed, including backups | ||
| # left by an interrupted earlier run. Once the file is clean, the backup is no | ||
| # longer a pending marker: keeping it would prompt on every later browser run. | ||
| for profile_root in "${profile_roots[@]}"; do | ||
| for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.pending.bak; do | ||
| [[ -f $backup ]] || continue | ||
| preferences=${backup%.omarchy-copy-url-repair.pending.bak} | ||
| # Missing/corrupt profiles are outside this repair. Preserve their recovery | ||
| # backup without blocking unrelated migrations; a restored profile is | ||
| # checked again on the next login or explicit repair. | ||
| python3 -c "$repair_py" "$preferences" "$pinned_id" readable || continue | ||
| if ! python3 -c "$repair_py" "$preferences" "$pinned_id" verify; then | ||
| if (( prompt )); then | ||
| echo "The Copy URL shortcut repair could not be verified." >&2 | ||
| echo "Close the browser windows, then run: omarchy-migrate" >&2 | ||
| exit 1 | ||
| fi | ||
| exit 0 | ||
| fi | ||
| done | ||
| done | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Browser profiles can arrive after shipped migrations were stamped complete. | ||
| # Quiet first-run repair leaves open profiles for a later login to repair. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| omarchy-cmd-repair-chromium-copy-url |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.