Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

commit-msg hook support#693

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Byron merged 1 commit intogitpython-developers:masterfromsatahippy:master
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletionsgit/index/base.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -948,13 +948,32 @@ def commit(self, message, parent_commits=None, head=True, author=None,
:return: Commit object representing the new commit"""
if not skip_hooks:
run_commit_hook('pre-commit', self)

self._write_commit_editmsg(message)
run_commit_hook('commit-msg', self, self._commit_editmsg_filepath())
message = self._read_commit_editmsg()
self._remove_commit_editmsg()
tree = self.write_tree()
rval = Commit.create_from_tree(self.repo, tree, message, parent_commits,
head, author=author, committer=committer,
author_date=author_date, commit_date=commit_date)
if not skip_hooks:
run_commit_hook('post-commit', self)
return rval

def _write_commit_editmsg(self, message):
with open(self._commit_editmsg_filepath(), "wb") as commit_editmsg_file:
commit_editmsg_file.write(message.encode(defenc))

def _remove_commit_editmsg(self):
os.remove(self._commit_editmsg_filepath())

def _read_commit_editmsg(self):
with open(self._commit_editmsg_filepath(), "rb") as commit_editmsg_file:
return commit_editmsg_file.read().decode(defenc)

def _commit_editmsg_filepath(self):
return osp.join(self.repo.common_dir, "COMMIT_EDITMSG")

@classmethod
def _flush_stdin_and_wait(cls, proc, ignore_stdout=False):
Expand Down
7 changes: 4 additions & 3 deletionsgit/index/fun.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -62,10 +62,11 @@ def hook_path(name, git_dir):
return osp.join(git_dir, 'hooks', name)


def run_commit_hook(name, index):
def run_commit_hook(name, index, *args):
"""Run the commit hook of the given name. Silently ignores hooks that do not exist.
:param name: name of hook, like 'pre-commit'
:param index: IndexFile instance
:param args: arguments passed to hook file
:raises HookExecutionError: """
hp = hook_path(name, index.repo.git_dir)
if not os.access(hp, os.X_OK):
Expand All@@ -75,7 +76,7 @@ def run_commit_hook(name, index):
env['GIT_INDEX_FILE'] = safe_decode(index.path) if PY3 else safe_encode(index.path)
env['GIT_EDITOR'] = ':'
try:
cmd = subprocess.Popen(hp,
cmd = subprocess.Popen([hp] + list(args),
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand All@@ -93,7 +94,7 @@ def run_commit_hook(name, index):
if cmd.returncode != 0:
stdout = force_text(stdout, defenc)
stderr = force_text(stderr, defenc)
raise HookExecutionError(hp, cmd.returncode,stdout, stderr)
raise HookExecutionError(hp, cmd.returncode,stderr, stdout)
# end handle return code


Expand Down
114 changes: 85 additions & 29 deletionsgit/test/test_index.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -729,35 +729,6 @@ def make_paths():
assert fkey not in index.entries

index.add(files, write=True)
if is_win:
hp = hook_path('pre-commit', index.repo.git_dir)
hpd = osp.dirname(hp)
if not osp.isdir(hpd):
os.mkdir(hpd)
with open(hp, "wt") as fp:
fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1")
# end
os.chmod(hp, 0o744)
try:
index.commit("This should fail")
except HookExecutionError as err:
if is_win:
self.assertIsInstance(err.status, OSError)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, '')
self.assertEqual(err.stderr, '')
assert str(err)
else:
self.assertEqual(err.status, 1)
self.assertEqual(err.command, hp)
self.assertEqual(err.stdout, 'stdout\n')
self.assertEqual(err.stderr, 'stderr\n')
assert str(err)
else:
raise AssertionError("Should have cought a HookExecutionError")
# end exception handling
os.remove(hp)
# end hook testing
nc = index.commit("2 files committed", head=False)

for fkey in keys:
Expand DownExpand Up@@ -859,3 +830,88 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir):
r = Repo.init(rw_dir)
r.index.add([fp])
r.index.commit('Added [.exe')

@with_rw_repo('HEAD', bare=True)
def test_pre_commit_hook_success(self, rw_repo):
index = rw_repo.index
hp = hook_path('pre-commit', index.repo.git_dir)
hpd = osp.dirname(hp)
if not osp.isdir(hpd):
os.mkdir(hpd)
with open(hp, "wt") as fp:
fp.write("#!/usr/bin/env sh\nexit 0")
os.chmod(hp, 0o744)
index.commit("This should not fail")

@with_rw_repo('HEAD', bare=True)
def test_pre_commit_hook_fail(self, rw_repo):
index = rw_repo.index
hp = hook_path('pre-commit', index.repo.git_dir)
hpd = osp.dirname(hp)
if not osp.isdir(hpd):
os.mkdir(hpd)
with open(hp, "wt") as fp:
fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1")
os.chmod(hp, 0o744)
try:
index.commit("This should fail")
except HookExecutionError as err:
if is_win:
self.assertIsInstance(err.status, OSError)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, '')
self.assertEqual(err.stderr, '')
assert str(err)
else:
self.assertEqual(err.status, 1)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, "\n stdout: 'stdout\n'")
self.assertEqual(err.stderr, "\n stderr: 'stderr\n'")
assert str(err)
else:
raise AssertionError("Should have cought a HookExecutionError")

@with_rw_repo('HEAD', bare=True)
def test_commit_msg_hook_success(self, rw_repo):
index = rw_repo.index
commit_message = u"commit default head by Frèderic Çaufl€"
from_hook_message = u"from commit-msg"

hp = hook_path('commit-msg', index.repo.git_dir)
hpd = osp.dirname(hp)
if not osp.isdir(hpd):
os.mkdir(hpd)
with open(hp, "wt") as fp:
fp.write('#!/usr/bin/env sh\necho -n " {}" >> "$1"'.format(from_hook_message))
os.chmod(hp, 0o744)

new_commit = index.commit(commit_message)
self.assertEqual(new_commit.message, u"{} {}".format(commit_message, from_hook_message))

@with_rw_repo('HEAD', bare=True)
def test_commit_msg_hook_fail(self, rw_repo):
index = rw_repo.index
hp = hook_path('commit-msg', index.repo.git_dir)
hpd = osp.dirname(hp)
if not osp.isdir(hpd):
os.mkdir(hpd)
with open(hp, "wt") as fp:
fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1")
os.chmod(hp, 0o744)
try:
index.commit("This should fail")
except HookExecutionError as err:
if is_win:
self.assertIsInstance(err.status, OSError)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, '')
self.assertEqual(err.stderr, '')
assert str(err)
else:
self.assertEqual(err.status, 1)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, "\n stdout: 'stdout\n'")
self.assertEqual(err.stderr, "\n stderr: 'stderr\n'")
assert str(err)
else:
raise AssertionError("Should have cought a HookExecutionError")

[8]ページ先頭

©2009-2025 Movatter.jp