mirror of
https://github.com/prdlk/dotfiles.git
synced 2026-11-02 11:32:42 +00:00
36 lines
1.4 KiB
Bash
36 lines
1.4 KiB
Bash
# czs: chezmoi re-add drifted files, then commit + push the source repo.
|
|
# Port of functions/czs.fish. Optional targets scope the status view and the
|
|
# re-add (e.g. `czs ~/.config/fish`); no args = all managed files.
|
|
czs() {
|
|
local drift src ahead
|
|
drift=$(chezmoi status "$@") || return
|
|
|
|
if [[ -n $drift ]]; then
|
|
print -r -- $drift
|
|
# re-add takes the local (destination) contents as truth for modified
|
|
# files; templates are never overwritten, non-file entries are ignored.
|
|
chezmoi re-add "$@" || return
|
|
else
|
|
echo "chs: in sync — nothing to re-add"
|
|
fi
|
|
|
|
# Commit + sync the source repo (~/.local/share/chezmoi).
|
|
src=$(chezmoi source-path)
|
|
if [[ -n $(git -C $src status --porcelain) ]]; then
|
|
git -C $src add -A &&
|
|
git -C $src commit --quiet -m 'chs: re-add drifted files' || return
|
|
echo "chs: committed $(git -C $src rev-parse --short HEAD)"
|
|
fi
|
|
|
|
# Push anything unpushed: this commit and leftovers from a failed push.
|
|
# --autostash tolerates post-commit hooks re-dirtying the tree mid-sync.
|
|
ahead=$(git -C $src rev-list --count @{upstream}..HEAD 2>/dev/null || echo 0)
|
|
if (( ahead > 0 )); then
|
|
git -C $src pull --rebase --autostash --quiet &&
|
|
git -C $src push --quiet &&
|
|
echo "chs: pushed $ahead commit(s) to $(git -C $src remote get-url origin)"
|
|
else
|
|
echo "chs: remote up to date"
|
|
fi
|
|
}
|