-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotfiles.sh
More file actions
executable file
·86 lines (74 loc) · 2.48 KB
/
dotfiles.sh
File metadata and controls
executable file
·86 lines (74 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
set -e
REPO_DIR=${DOTFILES:-"$HOME/.dotfiles"}
STOW_DIR="$REPO_DIR/home"
TARGET_DIR="$HOME"
# Parse arguments
DRY_RUN=""
MODE="install"
ADOPT=""
while [[ "$1" =~ ^- ]]; do
if [[ "$1" == "--dry-run" || "$1" == "-n" ]]; then
DRY_RUN="--no"
echo "DRY RUN MODE - No changes will be made"
shift
elif [[ "$1" == "--clean" || "$1" == "-c" ]]; then
MODE="clean"
echo "CLEAN MODE - Removing dotfile symlinks"
shift
elif [[ "$1" == "--adopt" ]]; then
ADOPT="--adopt"
echo "ADOPT MODE - Existing files will be adopted, then repo versions restored"
shift
elif [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Usage: $0 [--dry-run|-n] [--clean|-c] [--adopt]"
exit 0
else
echo "Unknown option: $1"
echo "Usage: $0 [--dry-run|-n] [--clean|-c] [--adopt]"
exit 1
fi
done
# Check if stow is installed
if ! command -v stow &> /dev/null; then
echo "Error: stow is not installed. Please install it first."
exit 1
fi
# Ensure STOW_DIR exists
if [ ! -d "$STOW_DIR" ]; then
echo "Error: Dotfiles directory $STOW_DIR does not exist."
exit 1
fi
cd "$STOW_DIR"
echo "Working with dotfiles from $STOW_DIR to $TARGET_DIR"
if [[ "$MODE" == "clean" ]]; then
stow -D -v $DRY_RUN -t "$TARGET_DIR" -d "$STOW_DIR" .
echo "Clean operation complete"
exit 0
fi
stow -R -v $ADOPT $DRY_RUN -t "$TARGET_DIR" -d "$STOW_DIR" .
# When --adopt is used, stow pulls existing files into the repo to resolve
# conflicts. Show what changed and ask before restoring repo versions.
if [[ -n "$ADOPT" && -z "$DRY_RUN" ]]; then
changes=$(git -C "$REPO_DIR" diff --stat -- home)
if [[ -n "$changes" ]]; then
echo ""
echo "Local files differ from repo versions after adopt:"
echo "$changes"
echo ""
if [[ -t 0 ]]; then
printf "Discard local changes and restore repo versions? [y/N] "
read -r confirm
if [[ "$confirm" == [yY] ]]; then
git -C "$REPO_DIR" checkout -- home
echo "Restored repo versions after adopt"
else
echo "Kept adopted local versions (repo now has your local files)"
echo "Run 'git diff -- home' to review, 'git checkout -- home' to restore repo versions"
fi
else
echo "Non-interactive: skipping checkout. Review with 'git diff -- home'"
fi
fi
fi
echo "Operation complete"