Skip to content

Commit 546eb41

Browse files
committed
default local cache and relative symlinks
1 parent 6105722 commit 546eb41

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

git-sym

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python2.7
22
"""
33
Environment variables used:
4-
GIT_SYM_DIR
54
GIT_SYM_CACHE_DIR
65
76
We use 3 levels of symlink.
@@ -10,6 +9,9 @@ We use 3 levels of symlink.
109
3. That points to GIT_DIR/git-sym/links
1110
4. Those point to GIT_SYM_CACHE_DIR
1211
12+
If cache-dir changes, old links are *not* updated unless broken.
13+
14+
## Changes in .git/config
1315
We expect this in .gitconfig:
1416
1517
[alias]
@@ -19,6 +21,7 @@ That facilitates running this script, and is also used within this script.
1921
(Git aliases are always run in the root of a Git tree.)
2022
http://stackoverflow.com/questions/957928/is-there-a-way-to-get-the-git-root-directory-in-one-command
2123
24+
If missing, we add that to .git/config instead.
2225
"""
2326
from contextlib import contextmanager
2427
import sys, os, re, subprocess, argparse, traceback, ConfigParser
@@ -102,7 +105,7 @@ def get_GIT_SYM_LINK():
102105
def get_GIT_SYM_CACHE_DIR():
103106
result = os.environ.get('GIT_SYM_CACHE_DIR', None)
104107
if not result:
105-
result = shell('echo ${HOME}/git-sym-cache').strip()
108+
result = os.path.join(GIT_SYM_DIR, 'cache')
106109
result = os.path.abspath(result)
107110
if not os.path.isdir(result):
108111
make_dirs(result)
@@ -140,10 +143,10 @@ def global_setup():
140143
"""
141144
global GIT_SYM_LINK, GIT_SYM_DIR, GIT_SYM_CACHE_DIR, GIT_DIR, GIT_ROOT_DIR
142145
shell("git config alias.gsexec '!exec '")
143-
GIT_SYM_CACHE_DIR = get_GIT_SYM_CACHE_DIR()
144-
GIT_ROOT_DIR = get_GIT_ROOT_DIR()
145146
GIT_DIR = os.path.abspath(get_GIT_DIR())
146-
GIT_SYM_DIR = os.path.join(GIT_DIR, 'git-sym')
147+
GIT_SYM_DIR = os.path.join(GIT_DIR, 'git-sym-local')
148+
GIT_SYM_CACHE_DIR = get_GIT_SYM_CACHE_DIR() # might depend on GIT_SYM_DIR
149+
GIT_ROOT_DIR = get_GIT_ROOT_DIR()
147150
debug("GIT_SYM_DIR=%r" %GIT_SYM_DIR)
148151
debug("GIT_SYM_CACHE_DIR=%r" %GIT_SYM_CACHE_DIR)
149152
debug("GIT_DIR=%r" %GIT_DIR)
@@ -243,15 +246,22 @@ def retrieve(paths):
243246
makefilename = os.path.join(GIT_ROOT_DIR, 'git-sym.makefile')
244247
with cd(GIT_SYM_CACHE_DIR):
245248
retrieve_using_make(makefilename, paths)
249+
if (os.path.commonprefix([GIT_SYM_CACHE_DIR, GIT_SYM_DIR]) ==
250+
GIT_SYM_DIR):
251+
cache_dir = os.path.relpath(GIT_SYM_CACHE_DIR)
252+
else:
253+
cache_dir = GIT_SYM_CACHE_DIR
246254
for path in paths:
247-
cached_path = os.path.join(GIT_SYM_CACHE_DIR, path)
255+
cached_path = os.path.join(cache_dir, path)
248256
debug('Checking %r -> %r' %(path, cached_path))
249257
assert os.path.exists(cached_path), cached_path
250-
if not os.path.islink(path):
258+
if not os.path.exists(path):
259+
if os.path.lexists(path):
260+
os.remove(path)
251261
log("$ ln -sf %r %r" %(cached_path, path))
252262
os.symlink(cached_path, path)
263+
# TODO: Remove these extra checks, unless debug mode?
253264
assert os.path.exists(path), path
254-
assert os.path.exists(cached_path), cached_path
255265
assert os.path.samefile(cached_path, path), "%r != %r" %(
256266
cached_path, path)
257267
def get_linked_path(symlink, via):
@@ -311,6 +321,7 @@ def git_sym_update(symlinks, **args):
311321
with cd(os.path.join(GIT_SYM_DIR, 'links')):
312322
retrieve(needed)
313323
git_sym_check(symlinks)
324+
# TODO Print on failure, unless silent mode.
314325
def git_sym_add(paths, force=False, **args):
315326
needed = set()
316327
for path in paths:

0 commit comments

Comments
 (0)