summaryrefslogtreecommitdiff
path: root/lib/git
diff options
context:
space:
mode:
Diffstat (limited to 'lib/git')
-rw-r--r--lib/git/index.py6
-rw-r--r--lib/git/objects/base.py8
-rw-r--r--lib/git/objects/tree.py3
-rw-r--r--lib/git/refs.py12
-rw-r--r--lib/git/utils.py105
5 files changed, 85 insertions, 49 deletions
diff --git a/lib/git/index.py b/lib/git/index.py
index e368f531..747ccc88 100644
--- a/lib/git/index.py
+++ b/lib/git/index.py
@@ -19,7 +19,7 @@ import subprocess
import git.diff as diff
from git.objects import Blob, Tree, Object, Commit
-from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation
+from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation, join_path_native
class _TemporaryFileSwap(object):
@@ -260,7 +260,7 @@ class IndexFile(LazyMixin, diff.Diffable):
super(IndexFile, self)._set_cache_(attr)
def _index_path(self):
- return os.path.join(self.repo.path, "index")
+ return join_path_native(self.repo.path, "index")
@property
@@ -440,7 +440,7 @@ class IndexFile(LazyMixin, diff.Diffable):
# as it considers existing entries. moving it essentially clears the index.
# Unfortunately there is no 'soft' way to do it.
# The _TemporaryFileSwap assure the original file get put back
- index_handler = _TemporaryFileSwap(os.path.join(repo.path, 'index'))
+ index_handler = _TemporaryFileSwap(join_path_native(repo.path, 'index'))
try:
repo.git.read_tree(*arg_list, **kwargs)
index = cls(repo, tmp_index)
diff --git a/lib/git/objects/base.py b/lib/git/objects/base.py
index b0989a43..c66263c0 100644
--- a/lib/git/objects/base.py
+++ b/lib/git/objects/base.py
@@ -4,7 +4,7 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
import os
-from git.utils import LazyMixin
+from git.utils import LazyMixin, join_path_native
import utils
_assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r"
@@ -209,7 +209,9 @@ class IndexObject(Object):
"""
Returns
Absolute path to this index object in the file system ( as opposed to the
- .path field which is a path relative to the git repository )
+ .path field which is a path relative to the git repository ).
+
+ The returned path will be native to the system and contains '\' on windows.
"""
- return os.path.join(self.repo.git.git_dir, self.path)
+ return join_path_native(self.repo.git.git_dir, self.path)
diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py
index 27bd84d0..f88096b3 100644
--- a/lib/git/objects/tree.py
+++ b/lib/git/objects/tree.py
@@ -9,6 +9,7 @@ import blob
import base
import binascii
import git.diff as diff
+from git.utils import join_path
def sha_to_hex(sha):
"""Takes a string and returns the hex of the sha within"""
@@ -110,7 +111,7 @@ class Tree(base.IndexObject, diff.Diffable):
i += 1
# END while not reached NULL
name = data[ns:i]
- path = os.path.join(self.path, name)
+ path = join_path(self.path, name)
# byte is NULL, get next 20
i += 1
diff --git a/lib/git/refs.py b/lib/git/refs.py
index 5b94ea07..b0996878 100644
--- a/lib/git/refs.py
+++ b/lib/git/refs.py
@@ -8,7 +8,7 @@
import os
from objects import Object, Commit
from objects.utils import get_object_type_by_name
-from utils import LazyMixin, Iterable
+from utils import LazyMixin, Iterable, join_path, join_path_native, to_native_path_linux
class Reference(LazyMixin, Iterable):
"""
@@ -136,15 +136,15 @@ class Reference(LazyMixin, Iterable):
# walk loose refs
# Currently we do not follow links
- for root, dirs, files in os.walk(os.path.join(repo.path, common_path)):
+ for root, dirs, files in os.walk(join_path_native(repo.path, common_path)):
for f in files:
- abs_path = os.path.join(root, f)
- rela_paths.add(abs_path.replace(repo.path + '/', ""))
+ abs_path = to_native_path_linux(join_path(root, f))
+ rela_paths.add(abs_path.replace(to_native_path_linux(repo.path) + '/', ""))
# END for each file in root directory
# END for each directory to walk
# read packed refs
- packed_refs_path = os.path.join(repo.path, 'packed-refs')
+ packed_refs_path = join_path_native(repo.path, 'packed-refs')
if os.path.isfile(packed_refs_path):
fp = open(packed_refs_path, 'r')
try:
@@ -230,7 +230,7 @@ class SymbolicReference(object):
return hash(self.name)
def _get_path(self):
- return os.path.join(self.repo.path, self.name)
+ return join_path_native(self.repo.path, self.name)
def _get_commit(self):
"""
diff --git a/lib/git/utils.py b/lib/git/utils.py
index 48427ff2..4397fbb2 100644
--- a/lib/git/utils.py
+++ b/lib/git/utils.py
@@ -9,55 +9,88 @@ import sys
import tempfile
try:
- import hashlib
+ import hashlib
except ImportError:
- import sha
+ import sha
def make_sha(source=''):
- """
- A python2.4 workaround for the sha/hashlib module fiasco
-
+ """
+ A python2.4 workaround for the sha/hashlib module fiasco
+
Note
From the dulwich project
"""
- try:
- return hashlib.sha1(source)
- except NameError:
- sha1 = sha.sha(source)
- return sha1
+ try:
+ return hashlib.sha1(source)
+ except NameError:
+ sha1 = sha.sha(source)
+ return sha1
+
+def join_path(a, *p):
+ """Join path tokens together similar to os.path.join, but always use
+ '/' instead of possibly '\' on windows."""
+ path = a
+ for b in p:
+ if b.startswith('/'):
+ path += b[1:]
+ elif path == '' or path.endswith('/'):
+ path += b
+ else:
+ path += '/' + b
+ return path
+
+def to_native_path_windows(path):
+ return path.replace('/','\\')
+
+def to_native_path_linux(path):
+ return path.replace('\\','/')
+
+if sys.platform.startswith('win'):
+ to_native_path = to_native_path_windows
+else:
+ # no need for any work on linux
+ def to_native_path_linux(path):
+ return path
+ to_native_path = to_native_path_linux
+
+def join_path_native(a, *p):
+ """As join path, but makes sure an OS native path is returned. This is only
+ needed to play it safe on my dear windows and to assure nice paths that only
+ use '\'"""
+ return to_native_path(join_path(a, *p))
class SHA1Writer(object):
- """
- Wrapper around a file-like object that remembers the SHA1 of
- the data written to it. It will write a sha when the stream is closed
- or if the asked for explicitly usign write_sha.
-
- Note:
- Based on the dulwich project
- """
- __slots__ = ("f", "sha1")
-
- def __init__(self, f):
- self.f = f
- self.sha1 = make_sha("")
+ """
+ Wrapper around a file-like object that remembers the SHA1 of
+ the data written to it. It will write a sha when the stream is closed
+ or if the asked for explicitly usign write_sha.
+
+ Note:
+ Based on the dulwich project
+ """
+ __slots__ = ("f", "sha1")
+
+ def __init__(self, f):
+ self.f = f
+ self.sha1 = make_sha("")
- def write(self, data):
- self.sha1.update(data)
- self.f.write(data)
+ def write(self, data):
+ self.sha1.update(data)
+ self.f.write(data)
- def write_sha(self):
- sha = self.sha1.digest()
- self.f.write(sha)
- return sha
+ def write_sha(self):
+ sha = self.sha1.digest()
+ self.f.write(sha)
+ return sha
- def close(self):
- sha = self.write_sha()
- self.f.close()
- return sha
+ def close(self):
+ sha = self.write_sha()
+ self.f.close()
+ return sha
- def tell(self):
- return self.f.tell()
+ def tell(self):
+ return self.f.tell()
class LockFile(object):