Skip to content

Commit

Permalink
Merge pull request #219 from yut23/fix-pull-symlinking
Browse files Browse the repository at this point in the history
Fix pull not symlinking new and renamed files
  • Loading branch information
andsens authored Nov 30, 2023
2 parents ca12b06 + bed4cfb commit 2cb82d1
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bin/homeshick
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ case $cmd in
refresh)
(refresh $threshhold "$param") ;;
pull)
(pull "$param") ;;
(pull "$param") && pull_completed+=("$param") ;;
symlink|link)
symlink "$param" ;;
track)
Expand All @@ -202,7 +202,7 @@ case $cmd in
refresh)
pull_outdated $threshhold "${params[@]}" ;;
pull)
symlink_new_files "${params[@]}" ;;
symlink_new_files "${pull_completed[@]}" ;;
esac
result=$?
if [[ $exit_status == 0 && $result != 0 ]]; then
Expand Down
25 changes: 19 additions & 6 deletions lib/commands/pull.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

BEFORE_PULL_TAG=__homeshick-before-pull__
pull() {
[[ ! $1 ]] && help_err pull
local castle=$1
Expand All @@ -13,6 +14,15 @@ pull() {
return "$EX_SUCCESS"
fi

# this tag is exceedingly unlikely to already exist, but if it does, error
# out and let the user resolve it
(cd "$repo" && git rev-parse --verify "refs/tags/$BEFORE_PULL_TAG" &>/dev/null) && \
err "$EX_DATAERR" "Pull marker tag ($BEFORE_PULL_TAG) already exists in $repo. Please resolve this before pulling."
# make a tag at the current commit, so we can compare against it below
(cd "$repo" && git tag "$BEFORE_PULL_TAG" 2>&1)
# remove the tag if one of the git operations fails
trap 'cd "$repo" && git tag -d "$BEFORE_PULL_TAG" &>/dev/null' EXIT

local git_out
git_out=$(cd "$repo" && git pull 2>&1) || \
err "$EX_SOFTWARE" "Unable to pull $repo. Git says:" "$git_out"
Expand All @@ -26,6 +36,7 @@ pull() {
err "$EX_SOFTWARE" "Unable update submodules for $repo. Git says:" "$git_out"
fi
success
trap - EXIT
return "$EX_SUCCESS"
}

Expand All @@ -35,15 +46,17 @@ symlink_new_files() {
local castle=$1
shift
local repo="$repos/$castle"
if [[ ! -d $repo/home ]]; then
continue;
fi
local git_out
local now
now=$(date +%s)
if ! git_out=$(cd "$repo" && git diff --name-only --diff-filter=A "HEAD@{(($now-$T_START+1)).seconds.ago}" HEAD -- home 2>/dev/null | wc -l 2>&1); then
git_out=$(cd "$repo" && git diff --name-only --diff-filter=AR "$BEFORE_PULL_TAG" HEAD -- home 2>/dev/null | wc -l 2>&1)
local result=$?
# Remove the tag before doing anything else
(cd "$repo" && git tag -d "$BEFORE_PULL_TAG" &>/dev/null)
if [[ $result -ne 0 ]]; then
continue # Ignore errors, this operation is not mission critical
fi
if [[ ! -d $repo/home ]]; then
continue;
fi
if [[ $git_out -gt 0 ]]; then
updated_castles+=("$castle")
fi
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/pull-renamed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# shellcheck disable=2164
fixture_pull_renamed() {
local git_username="Homeshick user"
local git_useremail="homeshick@example.com"
local repo="$REPO_FIXTURES/pull-renamed"
git init "$repo"
cd "$repo"
git config user.name "$git_username"
git config user.email "$git_useremail"
mkdir home
cd home

cat > .bashrc-wrong-name <<EOF
#!/usr/bin/env bash
PS1='\[33[01;32m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\'
EOF
git add .bashrc-wrong-name
git commit -m '.bashrc file for my new repo'

git mv .bashrc-wrong-name .bashrc
git commit -m 'fixed .bashrc file name'

cat >> .bashrc <<EOF
export IMPORTANT_VARIABLE=1
EOF
git add .bashrc
git commit -m 'Modified .bashrc to set IMPORTANT_VARIABLE'
}

fixture_pull_renamed > /dev/null
36 changes: 36 additions & 0 deletions test/fixtures/pull-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

# shellcheck disable=2164
fixture_pull_test() {
local git_username="Homeshick user"
local git_useremail="homeshick@example.com"
local repo="$REPO_FIXTURES/pull-test"
git init "$repo"
cd "$repo"
git config user.name "$git_username"
git config user.email "$git_useremail"
mkdir home
cd home

cat > .bashrc <<EOF
#!/usr/bin/env bash
PS1='\[33[01;32m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\'
EOF
git add .bashrc
git commit -m '.bashrc file for my new pull-test repo'

cat > .gitignore <<EOF
.DS_Store
*.swp
EOF
git add .gitignore
git commit -m 'Added .gitignore file'

cat >> .bashrc <<EOF
export IMPORTANT_VARIABLE=1
EOF
git add .bashrc
git commit -m 'Modified .bashrc to set IMPORTANT_VARIABLE'
}

fixture_pull_test > /dev/null
Loading

0 comments on commit 2cb82d1

Please sign in to comment.