Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIx issue #464 corrupt log file #469

Merged
merged 9 commits into from
Aug 2, 2016
16 changes: 15 additions & 1 deletion git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
safe_decode,
)

# value of Windows process creation flag taken from MSDN
CREATE_NO_WINDOW = 0x08000000

execute_kwargs = ('istream', 'with_keep_cwd', 'with_extended_output',
'with_exceptions', 'as_process', 'stdout_as_string',
'output_stream', 'with_stdout', 'kill_after_timeout',
Expand Down Expand Up @@ -609,6 +612,11 @@ def execute(self, command,
# end handle

try:
if sys.platform == 'win32':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally for these kinds of assignment, I believe it's easier to read by starting out like this:

creationflags = None
if sys.platform == 'win32':
    creationflags = CREATE_NO_WINDOW

creationflags = CREATE_NO_WINDOW
else:
creationflags = 0

proc = Popen(command,
env=env,
cwd=cwd,
Expand All @@ -619,6 +627,7 @@ def execute(self, command,
shell=self.USE_SHELL,
close_fds=(os.name == 'posix'), # unsupported on windows
universal_newlines=universal_newlines,
creationflags=creationflags,
**subprocess_kwargs
)
except cmd_not_found_exception as err:
Expand All @@ -629,7 +638,12 @@ def execute(self, command,

def _kill_process(pid):
""" Callback method to kill a process. """
p = Popen(['ps', '--ppid', str(pid)], stdout=PIPE)
if sys.platform == 'win32':
creationflags = CREATE_NO_WINDOW
else:
creationflags = 0

p = Popen(['ps', '--ppid', str(pid)], stdout=PIPE, creationflags=creationflags)
child_pids = []
for line in p.stdout:
if len(line.split()) > 0:
Expand Down
5 changes: 3 additions & 2 deletions git/refs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def from_line(cls, line):
newhexsha = info[41:81]
for hexsha in (oldhexsha, newhexsha):
if not cls._re_hexsha_only.match(hexsha):
raise ValueError("Invalid hexsha: %s" % hexsha)
raise ValueError("Invalid hexsha: %r" % (hexsha,))
# END if hexsha re doesn't match
# END for each hexsha

Expand Down Expand Up @@ -274,11 +274,12 @@ def append_entry(cls, config_reader, filepath, oldbinsha, newbinsha, message):
raise ValueError("Shas need to be given in binary format")
# END handle sha type
assure_directory_exists(filepath, is_file=True)
first_line = message.split('\n')[0]
committer = isinstance(config_reader, Actor) and config_reader or Actor.committer(config_reader)
entry = RefLogEntry((
bin_to_hex(oldbinsha).decode('ascii'),
bin_to_hex(newbinsha).decode('ascii'),
committer, (int(time.time()), time.altzone), message
committer, (int(time.time()), time.altzone), first_line
))

lf = LockFile(filepath)
Expand Down
13 changes: 12 additions & 1 deletion git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,23 @@ def test_empty_repo(self, rw_dir):
new_file_path = os.path.join(rw_dir, "new_file.ext")
touch(new_file_path)
r.index.add([new_file_path])
r.index.commit("initial commit")
r.index.commit("initial commit\nBAD MESSAGE 1\n")

# Now a branch should be creatable
nb = r.create_head('foo')
assert nb.is_valid()

with open(new_file_path, 'w') as f:
f.write('Line 1\n')

r.index.add([new_file_path])
r.index.commit("add line 1\nBAD MESSAGE 2\n")

with open('%s/.git/logs/refs/heads/master' % (rw_dir,), 'r') as f:
contents = f.read()

assert 'BAD MESSAGE' not in contents, 'log is corrupt'

def test_merge_base(self):
repo = self.rorepo
c1 = 'f6aa8d1'
Expand Down