Skip to content

Commit

Permalink
Add castle root mode when repository root is used instead of home/
Browse files Browse the repository at this point in the history
    Files are symlinked directry from repository root, instead of
    a home/ subdirectory under root.

    Use HOMESHICK_USE_CASTLE_ROOT for clone and generate to enable
    this mode on per castle basis
  • Loading branch information
antontsv committed Aug 15, 2016
1 parent e4d48f4 commit a2fe048
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
12 changes: 9 additions & 3 deletions lib/commands/clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function clone {
fi
git_repo="https://github.com/$git_repo.git"
fi
local repo_path=$repos"/"$(repo_basename "$git_repo")
local cloned_castle_name=$(repo_basename "$git_repo")
local repo_path=$repos"/"$cloned_castle_name
pending 'clone' "$git_repo"
test -e "$repo_path" && err $EX_ERR "$repo_path already exists"

Expand All @@ -32,6 +33,7 @@ function clone {
[[ $? == 0 ]] || err $EX_SOFTWARE "Unable to clone submodules for $git_repo. Git says:" "$git_out"
success
fi
[ -n "$HOMESHICK_USE_CASTLE_ROOT" ] && set_castle_root_mode "$cloned_castle_name"
return $EX_SUCCESS
}

Expand All @@ -44,10 +46,14 @@ function symlink_cloned_files {
local castle=$(repo_basename "$git_repo")
shift
local repo="$repos/$castle"
if [[ ! -d $repo/home ]]; then
if is_castle_root_mode_enabled "$castle"; then
local search_dir="$repo";
elif [ -d "$repo/home" ]; then
local search_dir="$repo/home";
else
continue;
fi
local num_files=$(find "$repo/home" -mindepth 1 -maxdepth 1 | wc -l | tr -dc "0123456789")
local num_files=$(find "$search_dir" -mindepth 1 -maxdepth 1 | wc -l | tr -dc "0123456789")
if [[ $num_files > 0 ]]; then
cloned_castles+=("$castle")
fi
Expand Down
6 changes: 5 additions & 1 deletion lib/commands/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ function generate {
local git_out
git_out=$(cd "$repo"; git init 2>&1)
[[ $? == 0 ]] || err $EX_SOFTWARE "Unable to initialize repository $repo. Git says:" "$git_out"
mkdir "$repo/home"
if [ -n "$HOMESHICK_USE_CASTLE_ROOT" ]; then
set_castle_root_mode "$castle"
else
mkdir "$repo/home"
fi;
success
return $EX_SUCCESS
}
27 changes: 19 additions & 8 deletions lib/commands/link.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function symlink {
local castle=$1
castle_exists 'link' "$castle"
local repo="$repos/$castle"
if [[ ! -d $repo/home ]]; then
if ! is_castle_root_mode_enabled "$castle" && [ ! -d "$repo/home" ]; then
ignore 'ignored' "$castle"
return $EX_SUCCESS
fi
Expand All @@ -17,7 +17,11 @@ function symlink {
# using the real stdin for prompting the user whether he wants to overwrite or skip
# on conflicts.
while IFS= read -d $'\0' -r filename <&3 ; do
remote="$repo/home/$filename"
if is_castle_root_mode_enabled "$castle"; then
remote="$repo/$filename"
else
remote="$repo/home/$filename"
fi;
local="$HOME/$filename"

if [[ -e $local || -L $local ]]; then
Expand Down Expand Up @@ -69,7 +73,7 @@ function symlink {

success
# Fetch the repo files and redirect the output into file descriptor 3
done 3< <(get_repo_files "$repo")
done 3< <(get_repo_files "$repo" "$castle")
return $EX_SUCCESS
}

Expand All @@ -82,17 +86,24 @@ function get_repo_files {
# We do this so that the root part of $toplevel can be replaced
# git resolves symbolic links before it outputs $toplevel
local root=$(cd "$1"; pwd -P)
if is_castle_root_mode_enabled "$2"; then
local search="";
else
local search="home/";
fi
(
local path
while IFS= read -d $'\n' -r path; do
# Remove quotes from ls-files
# (used when there are newlines in the path)
path=${path/#\"/}
path=${path/%\"/}
# Check if home/ is a submodule
[[ $path == 'home' ]] && continue
# Remove the home/ part
path=${path/#home\//}
if [ -n "$search" ];then
# Check if home/ is a submodule
[[ $path == 'home' ]] && continue
# Remove the home/ part
path=${path/#home\//}
fi;
# Print the file path (NUL separated because \n can be used in filenames)
printf "$path\0"
# Get the path of all the parent directories
Expand All @@ -107,7 +118,7 @@ function get_repo_files {
# Enter the repo, list the repo root files in home
# and do the same for any submodules
done < <(cd "$root" &&
git ls-files 'home/' &&
git ls-files "$search" &&
git submodule --quiet foreach --recursive \
"$homeshick/lib/submodule_files.sh \"$root\" \"\$toplevel\" \"\$path\"")
# Unfortunately we have to use an external script for `git submodule foreach'
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/pull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function symlink_new_files {
local castle=$1
shift
local repo="$repos/$castle"
if [[ ! -d $repo/home ]]; then
if ! is_castle_root_mode_enabled "$castle" && [[ ! -d $repo/home ]]; then
continue;
fi
local git_out
Expand Down
8 changes: 7 additions & 1 deletion lib/commands/track.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ function track {
local repo="$repos/$castle"
oldIFS=$IFS
IFS=$'\n'
root_dir_mode= is_castle_root_mode_enabled "$castle" && true
if is_castle_root_mode_enabled "$castle"; then
local prefix="";
else
local prefix="home/";
fi;
for local in $files_to_track; do
IFS=$oldIFS

local filepath=${local#$HOME/}
pending 'track' "$filepath"
local rel_remote="home/$filepath"
local rel_remote="${prefix}$filepath"
local remote="$repo/$rel_remote"

if [[ -e $remote ]]; then
Expand Down
33 changes: 30 additions & 3 deletions lib/fs.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
#!/bin/bash

function homeshick_config_set {
local castle="$1"
local config_key="$2"
local config_value="$3"
git config -f "$repos/../${castle}.config" "homeshick.$config_key" "$config_value"
}

function homeshick_config_get {
local castle="$1"
local config_key="$2"
git config -f "$repos/../${castle}.config" "homeshick.$config_key"
}

function set_castle_root_mode {
local castle="$1"
homeshick_config_set "$1" "use-castle-root" 1
}

function is_castle_root_mode_enabled {
config_value=$(homeshick_config_get "$1" "use-castle-root")
if [ $? -eq 0 ] && [ "$config_value" = "1" ];then
return 0
else
return 1
fi
}

function castle_exists {
local action=$1
local castle=$2
Expand All @@ -13,9 +40,9 @@ function home_exists {
local action=$1
local castle=$2
local repo="$repos/$castle"
if [[ ! -d $repo/home ]]; then
err $EX_ERR "Could not $action $castle, expected $repo to contain a home folder"
fi
if ! is_castle_root_mode_enabled "$castle" && [ ! -d "$repo/home" ]; then
err $EX_ERR "Could not $action $castle, expected $repo to contain a home folder"
fi
}

function list_castle_names {
Expand Down

0 comments on commit a2fe048

Please sign in to comment.