Skip to content

Commit 61f3db7

Browse files
committed
Removed ORIG_HEAD handling which was downright wrong. ORIG_HEAD gets only set during merge and rebase, and probably everything that changes the ref more drastically. Probably I have to reread that. What needs to be adjusted though is the reflog
1 parent a21a9f6 commit 61f3db7

File tree

5 files changed

+47
-71
lines changed

5 files changed

+47
-71
lines changed

refs/head.py

-17
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,6 @@ def orig_head(self):
2929
to contain the previous value of HEAD"""
3030
return SymbolicReference(self.repo, self._ORIG_HEAD_NAME)
3131

32-
def _set_reference(self, ref):
33-
"""If someone changes the reference through us, we must manually update
34-
the ORIG_HEAD if we are detached. The underlying implementation can only
35-
handle un-detached heads as it has to check whether the current head
36-
is the checked-out one"""
37-
if self.is_detached:
38-
prev_commit = self.commit
39-
super(HEAD, self)._set_reference(ref)
40-
SymbolicReference.create(self.repo, self._ORIG_HEAD_NAME, prev_commit, force=True)
41-
else:
42-
super(HEAD, self)._set_reference(ref)
43-
# END handle detached mode
44-
45-
# aliased reference
46-
reference = property(SymbolicReference._get_reference, _set_reference, doc="Returns the Reference we point to")
47-
ref = reference
48-
4932
def reset(self, commit='HEAD', index=True, working_tree = False,
5033
paths=None, **kwargs):
5134
"""Reset our HEAD to the given commit optionally synchronizing

refs/log.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from git.util import (
22
join_path,
33
Actor,
4+
LockedFD,
45
)
56

67
from gitdb.util import (
@@ -173,13 +174,16 @@ def iter_entries(cls, stream):
173174
def to_file(self, filepath):
174175
"""Write the contents of the reflog instance to a file at the given filepath.
175176
:param filepath: path to file, parent directories are assumed to exist"""
176-
# TODO: Use locked fd
177-
fp = open(filepath, 'wb')
177+
lfd = LockedFD(filepath)
178+
fp = lfd.open(write=True, stream=True)
178179
try:
179180
self._serialize(fp)
180-
finally:
181-
fp.close()
182-
#END handle file streams
181+
lfd.commit()
182+
except:
183+
# on failure it rolls back automatically, but we make it clear
184+
lfd.rollback()
185+
raise
186+
#END handle change
183187

184188
def append_entry(self, oldbinsha, newbinsha, message, write=True):
185189
"""Append a new log entry to the revlog, changing it in place.

refs/symbolic.py

+6-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
exists,
1414
isfile,
1515
rename,
16-
hex_to_bin
16+
hex_to_bin,
17+
LockedFD
1718
)
1819

1920
from log import RefLog
@@ -181,11 +182,13 @@ def _get_reference(self):
181182
raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha))
182183
return self.from_path(self.repo, target_ref_path)
183184

184-
def _set_reference(self, ref):
185+
def _set_reference(self, ref, msg = None):
185186
"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
186187
Otherwise we try to get a commit from it using our interface.
187188
188-
Strings are allowed but will be checked to be sure we have a commit"""
189+
Strings are allowed but will be checked to be sure we have a commit
190+
:param msg: If set to a string, the message will be used in the reflog.
191+
Otherwise, a reflog entry is not written for the changed reference"""
189192
write_value = None
190193
if isinstance(ref, SymbolicReference):
191194
write_value = "ref: %s" % ref.path
@@ -205,22 +208,6 @@ def _set_reference(self, ref):
205208
# END end try string
206209
# END try commit attribute
207210

208-
# maintain the orig-head if we are currently checked-out
209-
head = HEAD(self.repo)
210-
try:
211-
if head.ref == self:
212-
try:
213-
# TODO: implement this atomically, if we fail below, orig_head is at an incorrect spot
214-
# Enforce the creation of ORIG_HEAD
215-
SymbolicReference.create(self.repo, head.orig_head().name, self.commit, force=True)
216-
except ValueError:
217-
pass
218-
#END exception handling
219-
# END if we are checked-out
220-
except TypeError:
221-
pass
222-
# END handle detached heads
223-
224211
# if we are writing a ref, use symbolic ref to get the reflog and more
225212
# checking
226213
# Otherwise we detach it and have to do it manually. Besides, this works

test/test_index.py

-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ def mixed_iterator():
422422
# same index, no parents
423423
commit_message = "index without parents"
424424
commit_no_parents = index.commit(commit_message, parent_commits=list(), head=True)
425-
assert SymbolicReference(rw_repo, 'ORIG_HEAD').commit == cur_commit
426425
assert commit_no_parents.message == commit_message
427426
assert len(commit_no_parents.parents) == 0
428427
assert cur_head.commit == commit_no_parents

test/test_refs.py

+32-29
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,38 @@ def test_heads(self, rwrepo):
9595
assert head.tracking_branch() is None
9696
# END for each head
9797

98-
# verify ORIG_HEAD gets set for detached heads
99-
head = rwrepo.head
100-
orig_head = head.orig_head()
101-
cur_head = head.ref
102-
cur_commit = cur_head.commit
103-
pcommit = cur_head.commit.parents[0].parents[0]
104-
head.ref = pcommit # detach head
105-
assert orig_head.commit == cur_commit
106-
107-
# even if we set it through its reference - chaning the ref
108-
# will adjust the orig_head, which still points to cur_commit
109-
head.ref = cur_head
110-
assert orig_head.commit == pcommit
111-
assert head.commit == cur_commit == cur_head.commit
112-
113-
cur_head.commit = pcommit
114-
assert head.commit == pcommit
115-
assert orig_head.commit == cur_commit
116-
117-
# with automatic dereferencing
118-
head.commit = cur_commit
119-
assert orig_head.commit == pcommit
120-
121-
# changing branches which are not checked out doesn't affect the ORIG_HEAD
122-
other_head = Head.create(rwrepo, 'mynewhead', pcommit)
123-
assert other_head.commit == pcommit
124-
assert orig_head.commit == pcommit
125-
other_head.commit = pcommit.parents[0]
126-
assert orig_head.commit == pcommit
98+
# verify REFLOG gets altered
99+
if False:
100+
head = rwrepo.head
101+
orig_head = head.orig_head()
102+
cur_head = head.ref
103+
cur_commit = cur_head.commit
104+
pcommit = cur_head.commit.parents[0].parents[0]
105+
head.ref = pcommit # detach head
106+
assert orig_head.commit == cur_commit
107+
108+
# even if we set it through its reference - chaning the ref
109+
# will adjust the orig_head, which still points to cur_commit
110+
head.ref = cur_head
111+
assert orig_head.commit == pcommit
112+
assert head.commit == cur_commit == cur_head.commit
113+
114+
cur_head.commit = pcommit
115+
assert head.commit == pcommit
116+
assert orig_head.commit == cur_commit
117+
118+
# with automatic dereferencing
119+
head.commit = cur_commit
120+
assert orig_head.commit == pcommit
121+
122+
# changing branches which are not checked out doesn't affect the ORIG_HEAD
123+
other_head = Head.create(rwrepo, 'mynewhead', pcommit)
124+
assert other_head.commit == pcommit
125+
assert orig_head.commit == pcommit
126+
other_head.commit = pcommit.parents[0]
127+
assert orig_head.commit == pcommit
128+
129+
# TODO: Need changing a ref changes HEAD reflog as well if it pointed to it
127130

128131

129132
def test_refs(self):

0 commit comments

Comments
 (0)