-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-relative-worktree.zsh
68 lines (57 loc) · 1.69 KB
/
git-relative-worktree.zsh
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
#!/usr/bin/env zsh
# Script to convert absolute paths in git worktree configurations for gitdir to relative paths.
# This is particularly useful for the case where the worktree was created in WSL with a mapped
# drive. The absolute gitdir paths would not work with native Windows applications like Vim, etc.
# Usage:
# 1. Create a git-worktree with either WSL or Git Bash or Git-For-Windows in cmd.
# 2. Invoke git-relative-worktree
# `git-relative-worktree <main-directory> <worktree>
function echoerr() {
echo "ERROR: $@" 1>&2;
}
function usage() {
echo "Usage: $@ <primary directory> <worktree dir>";
}
argn=$#
if [[ $argn -lt 2 ]]; then
if [[ $argn -lt 1 ]]; then
echoerr "Missing arguments"
elif [[ ! ${1:l} =~ "-h$|--help$" ]]; then
echoerr "Invalid argument: $1"
fi
usage $0
exit 1
fi
for x in $1 $2; do
if [ ! -d "$x" ]; then
echoerr "Not a valid directory: $x";
exit 1
fi
done
if [ ! -d "$1/.git" ]; then
echoerr "Could not find directory named '$1/.git/'"
exit 1
fi
if [ ! -f "$2/.git" ]; then
echoerr "Could not find file named '$2/.git'"
exit 1
fi
abs1=`realpath $1`
abs2=`realpath $2`
relp=`realpath --relative-to=$abs2 $abs1`
for line in "${(@f)"$(<$abs2/.git)"}"; do
echo "Replacing '$abs1' with '$relp' in '$abs2/.git'"
echo ${line//$abs1/$relp} >! "$abs2/.git"
done
bname=`basename $abs2`
gitwdir="$abs1/.git/worktrees/$bname"
gitwgitdir="$gitwdir/gitdir"
if [ ! -f "$gitwgitdir" ]; then
echoerr "Could not find file '$gitwgitdir'"
exit 1
fi
revrelp=`realpath --relative-to="$gitwdir" "$abs2"`
for line in "${(@f)"$(<$gitwgitdir)"}"; do
echo "Replacing '$abs2' with '$revrelp' in '$gitwgitdir'"
echo ${line//$abs2/$revrelp} >! "$gitwgitdir"
done