The alias only existed where it was typed by hand; a fresh VM with dots cloned had no cfg command. The module now always ensures: - ~/.bash_aliases holds the cfg alias (created if absent) - ~/.bashrc sources ~/.bash_aliases (appended if absent) Runs before the already-installed skip so existing machines get it too. Idempotent both ways (harness-proven: single alias line, single source line after two runs; verified working in an interactive shell: 'type cfg' resolves and 'cfg status' runs). ~/.bash_aliases is the right file to TRACK in fedora-dots next: it will then sync further aliases across machines.
83 lines
2.8 KiB
Bash
Executable File
83 lines
2.8 KiB
Bash
Executable File
#!/bin/bash
|
|
set -uo pipefail
|
|
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
# shellcheck source=../../lib/common.sh
|
|
source "$HERE/../../lib/common.sh"
|
|
|
|
if is_debug; then set +e; else set -e; fi
|
|
|
|
DOTFILES_REPO="${DOTFILES_REPO:-https://git.ku-it.org/yuri/fedora-dots.git}"
|
|
CFG_DIR="$HOME/.cfg.git"
|
|
|
|
step "Personal dotfiles: fedora-dots"
|
|
rule
|
|
|
|
if [[ ! -d "$HOME/.config" ]]; then
|
|
mkdir -p "$HOME/.config"
|
|
fi
|
|
|
|
# --- Install the cfg command, ALWAYS (even if the repo exists already) ---
|
|
# The alias lives in ~/.bash_aliases (user-owned, trackable in fedora-dots),
|
|
# and ~/.bashrc sources that file. Without this, `cfg` exists only on the
|
|
# machine where it was typed by hand (VM-proven: "cfg pull: command not found").
|
|
ALIASES_FILE="$HOME/.bash_aliases"
|
|
BASHRC="$HOME/.bashrc"
|
|
|
|
if ! grep -q "alias cfg=" "$ALIASES_FILE" 2>/dev/null; then
|
|
info "Installing cfg alias into ~/.bash_aliases..."
|
|
cat >> "$ALIASES_FILE" <<'EOF'
|
|
# cfg: manage the bare dotfiles repo (~/.cfg.git, work tree $HOME)
|
|
alias cfg='git --git-dir=$HOME/.cfg.git --work-tree=$HOME'
|
|
EOF
|
|
fi
|
|
|
|
if ! grep -q "bash_aliases" "$BASHRC" 2>/dev/null; then
|
|
info "Making ~/.bashrc load ~/.bash_aliases..."
|
|
touch "$BASHRC"
|
|
cat >> "$BASHRC" <<'EOF'
|
|
|
|
# user aliases
|
|
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases; fi
|
|
EOF
|
|
fi
|
|
|
|
if [[ -d "$CFG_DIR" ]]; then
|
|
ok "Dotfiles already set up ($CFG_DIR exists)."
|
|
ok "cfg alias ensured. Open a new shell or run: source ~/.bashrc"
|
|
exit 0
|
|
fi
|
|
|
|
# A bare repo is the storage; $HOME is the work tree. Checkout happens through
|
|
# the alias form, never a plain `git checkout` inside $HOME.
|
|
info "Cloning dotfiles (bare) into $CFG_DIR..."
|
|
# --bare is what makes ~/.cfg.git the storage; the plain clone YuKu first ran
|
|
# created a working copy instead and broke the alias (proven the hard way).
|
|
git clone --quiet --bare "$DOTFILES_REPO" "$CFG_DIR"
|
|
|
|
cfg() {
|
|
git --git-dir="$CFG_DIR" --work-tree="$HOME" "$@"
|
|
}
|
|
|
|
# Empty repo (no commits yet): nothing to check out, but leave everything
|
|
# configured so the first `cfg add` on this machine just works.
|
|
if ! git --git-dir="$CFG_DIR" rev-parse --verify -q HEAD >/dev/null; then
|
|
warn "Dotfiles repo has no commits yet — checkout skipped."
|
|
warn "Add folders on your main machine first; here you can start fresh."
|
|
cfg config status.showUntrackedFiles no
|
|
ok "cfg alias ready: git --git-dir=$CFG_DIR --work-tree=$HOME"
|
|
exit 0
|
|
fi
|
|
|
|
info "Checking dotfiles out into \$HOME..."
|
|
# -f on purpose: a fresh machine HAS stock files (e.g. .bashrc); the repo's
|
|
# versions win on first setup. Untracked files are never touched.
|
|
cfg checkout -f
|
|
cfg config status.showUntrackedFiles no
|
|
|
|
ok "Dotfiles checked out. Tracked paths:"
|
|
git --git-dir="$CFG_DIR" ls-tree -r --name-only HEAD | cut -d/ -f1-2 | sort -u | sed 's/^/ /'
|
|
|
|
info "Suggested shell alias for managing them:"
|
|
info " alias cfg='git --git-dir=$CFG_DIR --work-tree=\$HOME'"
|