summaryrefslogtreecommitdiff
path: root/mac/.local/bin/syncdot
blob: 5e560e9ac62f1d522dc88c13eb6433d8d544e3ef (plain)
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
#!/bin/sh

# Define ANSI color codes for better readability in script output
GREEN='\033[1;32m'
BLUE='\033[1;34m'
RED='\033[1;31m'
NC='\033[0m' # No Color

# Ensure the script exits on any error
set -e

# Navigate to the dotfiles directory
cd "${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}" || exit

echo "${BLUE}Stashing existing changes...${NC}"
stash_result=$(git stash push -m "sync-dotfiles: Before syncing dotfiles" 2>&1)
needs_pop=0
if ! echo "$stash_result" | grep -q "No local changes to save"; then
  needs_pop=1
fi

echo "${BLUE}Pulling updates from dotfiles repo...${NC}"
git pull origin master

if [ "$needs_pop" -eq 1 ]; then
  echo "${BLUE}Popping stashed changes...${NC}"
  git stash pop
fi

# Check for merge conflicts after attempting to pop the stash
unmerged_files=$(git diff --name-only --diff-filter=U)
if [ -n "$unmerged_files" ]; then
  echo "${RED}The following files have merge conflicts after popping the stash:${NC}"
  printf "%s\n" "$unmerged_files"
  exit 1
else
  echo "${GREEN}No merge conflicts detected. Proceeding with dotfiles linkage...${NC}"
  # Ensure GNU Stow is installed
  if ! command -v stow >/dev/null 2>&1; then
    echo "${RED}GNU Stow not found. Please install GNU Stow to continue.${NC}"
    exit 1
  fi
  # Run stow to ensure all new dotfiles are linked, targeting directories named after the OS
  stow -v --no-folding -S "$(whereami)" # Verbose output
  echo "${GREEN}Dotfiles successfully linked.${NC}"
fi