Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ee861a

Browse files
committed
Added utilities helping to create proper paths either with slashes or backslashes depending on the operating system
fixed test_refs and test_trees Many more issues remain though, this is just a first backup commit
1 parent 453995f commit 9ee861a

File tree

5 files changed

+85
-49
lines changed

5 files changed

+85
-49
lines changed

lib/git/index.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import git.diff as diff
2020

2121
from git.objects import Blob, Tree, Object, Commit
22-
from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation
22+
from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation, join_path_native
2323

2424

2525
class _TemporaryFileSwap(object):
@@ -260,7 +260,7 @@ def _set_cache_(self, attr):
260260
super(IndexFile, self)._set_cache_(attr)
261261

262262
def _index_path(self):
263-
return os.path.join(self.repo.path, "index")
263+
return join_path_native(self.repo.path, "index")
264264

265265

266266
@property
@@ -440,7 +440,7 @@ def from_tree(cls, repo, *treeish, **kwargs):
440440
# as it considers existing entries. moving it essentially clears the index.
441441
# Unfortunately there is no 'soft' way to do it.
442442
# The _TemporaryFileSwap assure the original file get put back
443-
index_handler = _TemporaryFileSwap(os.path.join(repo.path, 'index'))
443+
index_handler = _TemporaryFileSwap(join_path_native(repo.path, 'index'))
444444
try:
445445
repo.git.read_tree(*arg_list, **kwargs)
446446
index = cls(repo, tmp_index)

lib/git/objects/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
import os
7-
from git.utils import LazyMixin
7+
from git.utils import LazyMixin, join_path_native
88
import utils
99

1010
_assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r"
@@ -209,7 +209,9 @@ def abspath(self):
209209
"""
210210
Returns
211211
Absolute path to this index object in the file system ( as opposed to the
212-
.path field which is a path relative to the git repository )
212+
.path field which is a path relative to the git repository ).
213+
214+
The returned path will be native to the system and contains '\' on windows.
213215
"""
214-
return os.path.join(self.repo.git.git_dir, self.path)
216+
return join_path_native(self.repo.git.git_dir, self.path)
215217

lib/git/objects/tree.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import base
1010
import binascii
1111
import git.diff as diff
12+
from git.utils import join_path
1213

1314
def sha_to_hex(sha):
1415
"""Takes a string and returns the hex of the sha within"""
@@ -110,7 +111,7 @@ def _iter_from_data(self):
110111
i += 1
111112
# END while not reached NULL
112113
name = data[ns:i]
113-
path = os.path.join(self.path, name)
114+
path = join_path(self.path, name)
114115

115116
# byte is NULL, get next 20
116117
i += 1

lib/git/refs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
from objects import Object, Commit
1010
from objects.utils import get_object_type_by_name
11-
from utils import LazyMixin, Iterable
11+
from utils import LazyMixin, Iterable, join_path, join_path_native, to_native_path_linux
1212

1313
class Reference(LazyMixin, Iterable):
1414
"""
@@ -136,15 +136,15 @@ def iter_items(cls, repo, common_path = None, **kwargs):
136136

137137
# walk loose refs
138138
# Currently we do not follow links
139-
for root, dirs, files in os.walk(os.path.join(repo.path, common_path)):
139+
for root, dirs, files in os.walk(join_path_native(repo.path, common_path)):
140140
for f in files:
141-
abs_path = os.path.join(root, f)
142-
rela_paths.add(abs_path.replace(repo.path + '/', ""))
141+
abs_path = to_native_path_linux(join_path(root, f))
142+
rela_paths.add(abs_path.replace(to_native_path_linux(repo.path) + '/', ""))
143143
# END for each file in root directory
144144
# END for each directory to walk
145145

146146
# read packed refs
147-
packed_refs_path = os.path.join(repo.path, 'packed-refs')
147+
packed_refs_path = join_path_native(repo.path, 'packed-refs')
148148
if os.path.isfile(packed_refs_path):
149149
fp = open(packed_refs_path, 'r')
150150
try:
@@ -230,7 +230,7 @@ def __hash__(self):
230230
return hash(self.name)
231231

232232
def _get_path(self):
233-
return os.path.join(self.repo.path, self.name)
233+
return join_path_native(self.repo.path, self.name)
234234

235235
def _get_commit(self):
236236
"""

lib/git/utils.py

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,88 @@
99
import tempfile
1010

1111
try:
12-
import hashlib
12+
import hashlib
1313
except ImportError:
14-
import sha
14+
import sha
1515

1616
def make_sha(source=''):
17-
"""
18-
A python2.4 workaround for the sha/hashlib module fiasco
19-
17+
"""
18+
A python2.4 workaround for the sha/hashlib module fiasco
19+
2020
Note
2121
From the dulwich project
2222
"""
23-
try:
24-
return hashlib.sha1(source)
25-
except NameError:
26-
sha1 = sha.sha(source)
27-
return sha1
23+
try:
24+
return hashlib.sha1(source)
25+
except NameError:
26+
sha1 = sha.sha(source)
27+
return sha1
28+
29+
def join_path(a, *p):
30+
"""Join path tokens together similar to os.path.join, but always use
31+
'/' instead of possibly '\' on windows."""
32+
path = a
33+
for b in p:
34+
if b.startswith('/'):
35+
path += b[1:]
36+
elif path == '' or path.endswith('/'):
37+
path += b
38+
else:
39+
path += '/' + b
40+
return path
41+
42+
def to_native_path_windows(path):
43+
return path.replace('/','\\')
44+
45+
def to_native_path_linux(path):
46+
return path.replace('\\','/')
47+
48+
if sys.platform.startswith('win'):
49+
to_native_path = to_native_path_windows
50+
else:
51+
# no need for any work on linux
52+
def to_native_path_linux(path):
53+
return path
54+
to_native_path = to_native_path_linux
55+
56+
def join_path_native(a, *p):
57+
"""As join path, but makes sure an OS native path is returned. This is only
58+
needed to play it safe on my dear windows and to assure nice paths that only
59+
use '\'"""
60+
return to_native_path(join_path(a, *p))
2861

2962

3063
class SHA1Writer(object):
31-
"""
32-
Wrapper around a file-like object that remembers the SHA1 of
33-
the data written to it. It will write a sha when the stream is closed
34-
or if the asked for explicitly usign write_sha.
35-
36-
Note:
37-
Based on the dulwich project
38-
"""
39-
__slots__ = ("f", "sha1")
40-
41-
def __init__(self, f):
42-
self.f = f
43-
self.sha1 = make_sha("")
64+
"""
65+
Wrapper around a file-like object that remembers the SHA1 of
66+
the data written to it. It will write a sha when the stream is closed
67+
or if the asked for explicitly usign write_sha.
68+
69+
Note:
70+
Based on the dulwich project
71+
"""
72+
__slots__ = ("f", "sha1")
73+
74+
def __init__(self, f):
75+
self.f = f
76+
self.sha1 = make_sha("")
4477

45-
def write(self, data):
46-
self.sha1.update(data)
47-
self.f.write(data)
78+
def write(self, data):
79+
self.sha1.update(data)
80+
self.f.write(data)
4881

49-
def write_sha(self):
50-
sha = self.sha1.digest()
51-
self.f.write(sha)
52-
return sha
82+
def write_sha(self):
83+
sha = self.sha1.digest()
84+
self.f.write(sha)
85+
return sha
5386

54-
def close(self):
55-
sha = self.write_sha()
56-
self.f.close()
57-
return sha
87+
def close(self):
88+
sha = self.write_sha()
89+
self.f.close()
90+
return sha
5891

59-
def tell(self):
60-
return self.f.tell()
92+
def tell(self):
93+
return self.f.tell()
6194

6295

6396
class LockFile(object):

0 commit comments

Comments
 (0)