|
|
|
|
|
|
|
|
|
|
|
import contextlib |
|
from dataclasses import dataclass |
|
from io import BytesIO |
|
import logging |
|
import os |
|
import os.path as osp |
|
from pathlib import Path |
|
import re |
|
import shutil |
|
from stat import S_ISLNK, ST_MODE |
|
import subprocess |
|
import sys |
|
import tempfile |
|
|
|
from gitdb.base import IStream |
|
|
|
import ddt |
|
import pytest |
|
|
|
from git import BlobFilter, Diff, Git, IndexFile, Object, Repo, Tree |
|
from git.exc import ( |
|
CheckoutError, |
|
GitCommandError, |
|
HookExecutionError, |
|
InvalidGitRepositoryError, |
|
UnmergedEntriesError, |
|
) |
|
from git.index.fun import hook_path, run_commit_hook |
|
from git.index.typ import BaseIndexEntry, IndexEntry |
|
from git.index.util import TemporaryFileSwap |
|
from git.objects import Blob |
|
from git.util import Actor, cwd, hex_to_bin, rmtree |
|
|
|
from test.lib import ( |
|
TestBase, |
|
VirtualEnvironment, |
|
fixture, |
|
fixture_path, |
|
with_rw_directory, |
|
with_rw_repo, |
|
) |
|
|
|
HOOKS_SHEBANG = "#!/usr/bin/env sh\n" |
|
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
def _get_windows_ansi_encoding(): |
|
"""Get the encoding specified by the Windows system-wide ANSI active code page.""" |
|
|
|
import winreg |
|
|
|
hklm_path = R"SYSTEM\CurrentControlSet\Control\Nls\CodePage" |
|
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, hklm_path) as key: |
|
value, _ = winreg.QueryValueEx(key, "ACP") |
|
return f"cp{value}" |
|
|
|
|
|
class WinBashStatus: |
|
"""Namespace of native-Windows bash.exe statuses. Affects what hook tests can pass. |
|
|
|
Call check() to check the status. (CheckError and WinError should not typically be |
|
used to trigger skip or xfail, because they represent unexpected situations.) |
|
""" |
|
|
|
@dataclass |
|
class Inapplicable: |
|
"""This system is not native Windows: either not Windows at all, or Cygwin.""" |
|
|
|
@dataclass |
|
class Absent: |
|
"""No command for bash.exe is found on the system.""" |
|
|
|
@dataclass |
|
class Native: |
|
"""Running bash.exe operates outside any WSL distribution (as with Git Bash).""" |
|
|
|
@dataclass |
|
class Wsl: |
|
"""Running bash.exe calls bash in a WSL distribution.""" |
|
|
|
@dataclass |
|
class WslNoDistro: |
|
"""Running bash.exe tries to run bash on a WSL distribution, but none exists.""" |
|
|
|
process: "subprocess.CompletedProcess[bytes]" |
|
message: str |
|
|
|
@dataclass |
|
class CheckError: |
|
"""Running bash.exe fails in an unexpected error or gives unexpected output.""" |
|
|
|
process: "subprocess.CompletedProcess[bytes]" |
|
message: str |
|
|
|
@dataclass |
|
class WinError: |
|
"""bash.exe may exist but can't run. CreateProcessW fails unexpectedly.""" |
|
|
|
exception: OSError |
|
|
|
@classmethod |
|
def check(cls): |
|
"""Check the status of the bash.exe that run_commit_hook will try to use. |
|
|
|
This runs a command with bash.exe and checks the result. On Windows, shell and |
|
non-shell executable search differ; shutil.which often finds the wrong bash.exe. |
|
|
|
run_commit_hook uses Popen, including to run bash.exe on Windows. It doesn't |
|
pass shell=True (and shouldn't). On Windows, Popen calls CreateProcessW, which |
|
checks some locations before using the PATH environment variable. It is expected |
|
to try System32, even if another directory with the executable precedes it in |
|
PATH. When WSL is present, even with no distributions, bash.exe usually exists |
|
in System32; Popen finds it even if a shell would run another one, as on CI. |
|
(Without WSL, System32 may still have bash.exe; users sometimes put it there.) |
|
""" |
|
if sys.platform != "win32": |
|
return cls.Inapplicable() |
|
|
|
try: |
|
|
|
|
|
|
|
script = 'test -e /proc/sys/fs/binfmt_misc/WSLInterop; echo "$?"' |
|
command = ["bash.exe", "-c", script] |
|
process = subprocess.run(command, capture_output=True) |
|
except FileNotFoundError: |
|
return cls.Absent() |
|
except OSError as error: |
|
return cls.WinError(error) |
|
|
|
text = cls._decode(process.stdout).rstrip() |
|
|
|
if process.returncode == 1 and re.search(r"\bhttps://aka.ms/wslstore\b", text): |
|
return cls.WslNoDistro(process, text) |
|
if process.returncode != 0: |
|
_logger.error("Error running bash.exe to check WSL status: %s", text) |
|
return cls.CheckError(process, text) |
|
if text == "0": |
|
return cls.Wsl() |
|
if text == "1": |
|
return cls.Native() |
|
_logger.error("Strange output checking WSL status: %s", text) |
|
return cls.CheckError(process, text) |
|
|
|
@staticmethod |
|
def _decode(stdout): |
|
"""Decode bash.exe output as best we can.""" |
|
|
|
|
|
|
|
|
|
if b"\r\0\n\0" in stdout: |
|
return stdout.decode("utf-16le") |
|
|
|
|
|
|
|
|
|
|
|
|
|
acp = _get_windows_ansi_encoding() |
|
try: |
|
return stdout.decode(acp) |
|
except UnicodeDecodeError: |
|
pass |
|
except LookupError as error: |
|
_logger.warning(str(error)) |
|
|
|
|
|
return stdout.decode("utf-8", errors="replace") |
|
|
|
|
|
_win_bash_status = WinBashStatus.check() |
|
|
|
|
|
def _make_hook(git_dir, name, content, make_exec=True): |
|
"""A helper to create a hook""" |
|
hp = hook_path(name, git_dir) |
|
hpd = osp.dirname(hp) |
|
if not osp.isdir(hpd): |
|
os.mkdir(hpd) |
|
with open(hp, "wt") as fp: |
|
fp.write(HOOKS_SHEBANG + content) |
|
if make_exec: |
|
os.chmod(hp, 0o744) |
|
return hp |
|
|
|
|
|
@ddt.ddt |
|
class TestIndex(TestBase): |
|
def __init__(self, *args): |
|
super().__init__(*args) |
|
self._reset_progress() |
|
|
|
def _assert_fprogress(self, entries): |
|
self.assertEqual(len(entries), len(self._fprogress_map)) |
|
for _path, call_count in self._fprogress_map.items(): |
|
self.assertEqual(call_count, 2) |
|
|
|
self._reset_progress() |
|
|
|
def _fprogress(self, path, done, item): |
|
self._fprogress_map.setdefault(path, 0) |
|
curval = self._fprogress_map[path] |
|
if curval == 0: |
|
assert not done |
|
if curval == 1: |
|
assert done |
|
self._fprogress_map[path] = curval + 1 |
|
|
|
def _fprogress_add(self, path, done, item): |
|
"""Called as progress func - we keep track of the proper call order.""" |
|
assert item is not None |
|
self._fprogress(path, done, item) |
|
|
|
def _reset_progress(self): |
|
|
|
self._fprogress_map = {} |
|
|
|
def _assert_entries(self, entries): |
|
for entry in entries: |
|
assert isinstance(entry, BaseIndexEntry) |
|
assert not osp.isabs(entry.path) |
|
assert "\\" not in entry.path |
|
|
|
|
|
def test_index_file_base(self): |
|
|
|
index = IndexFile(self.rorepo, fixture_path("index")) |
|
assert index.entries |
|
assert index.version > 0 |
|
|
|
|
|
entry = next(iter(index.entries.values())) |
|
for attr in ( |
|
"path", |
|
"ctime", |
|
"mtime", |
|
"dev", |
|
"inode", |
|
"mode", |
|
"uid", |
|
"gid", |
|
"size", |
|
"binsha", |
|
"hexsha", |
|
"stage", |
|
): |
|
getattr(entry, attr) |
|
|
|
|
|
|
|
entries = index.entries |
|
assert isinstance(index.update(), IndexFile) |
|
assert entries is not index.entries |
|
|
|
|
|
index_merge = IndexFile(self.rorepo, fixture_path("index_merge")) |
|
self.assertEqual(len(index_merge.entries), 106) |
|
assert len([e for e in index_merge.entries.values() if e.stage != 0]) |
|
|
|
|
|
tmpfile = tempfile.mktemp() |
|
index_merge.write(tmpfile) |
|
with open(tmpfile, "rb") as fp: |
|
self.assertEqual(fp.read(), fixture("index_merge")) |
|
os.remove(tmpfile) |
|
|
|
def _cmp_tree_index(self, tree, index): |
|
|
|
if isinstance(tree, str): |
|
tree = self.rorepo.commit(tree).tree |
|
|
|
blist = [] |
|
for blob in tree.traverse(predicate=lambda e, d: e.type == "blob", branch_first=False): |
|
assert (blob.path, 0) in index.entries |
|
blist.append(blob) |
|
|
|
if len(blist) != len(index.entries): |
|
iset = {k[0] for k in index.entries.keys()} |
|
bset = {b.path for b in blist} |
|
raise AssertionError( |
|
"CMP Failed: Missing entries in index: %s, missing in tree: %s" % (bset - iset, iset - bset) |
|
) |
|
|
|
|
|
@with_rw_repo("0.1.6") |
|
def test_index_lock_handling(self, rw_repo): |
|
def add_bad_blob(): |
|
rw_repo.index.add([Blob(rw_repo, b"f" * 20, "bad-permissions", "foo")]) |
|
|
|
try: |
|
|
|
add_bad_blob() |
|
except Exception as ex: |
|
msg_py3 = "required argument is not an integer" |
|
msg_py2 = "cannot convert argument to integer" |
|
assert msg_py2 in str(ex) or msg_py3 in str(ex) |
|
|
|
|
|
try: |
|
add_bad_blob() |
|
except Exception as ex: |
|
assert "index.lock' could not be obtained" not in str(ex) |
|
|
|
@with_rw_repo("0.1.6") |
|
def test_index_file_from_tree(self, rw_repo): |
|
common_ancestor_sha = "5117c9c8a4d3af19a9958677e45cda9269de1541" |
|
cur_sha = "4b43ca7ff72d5f535134241e7c797ddc9c7a3573" |
|
other_sha = "39f85c4358b7346fee22169da9cad93901ea9eb9" |
|
|
|
|
|
base_index = IndexFile.from_tree(rw_repo, common_ancestor_sha) |
|
assert base_index.entries |
|
self._cmp_tree_index(common_ancestor_sha, base_index) |
|
|
|
|
|
two_way_index = IndexFile.from_tree(rw_repo, common_ancestor_sha, cur_sha) |
|
assert two_way_index.entries |
|
self._cmp_tree_index(cur_sha, two_way_index) |
|
|
|
|
|
three_way_index = IndexFile.from_tree(rw_repo, common_ancestor_sha, cur_sha, other_sha) |
|
assert len([e for e in three_way_index.entries.values() if e.stage != 0]) |
|
|
|
|
|
merge_required = lambda t: t[0] != 0 |
|
merge_blobs = list(three_way_index.iter_blobs(merge_required)) |
|
assert merge_blobs |
|
assert merge_blobs[0][0] in (1, 2, 3) |
|
assert isinstance(merge_blobs[0][1], Blob) |
|
|
|
|
|
prefix = "lib/git" |
|
for _stage, blob in base_index.iter_blobs(BlobFilter([prefix])): |
|
assert blob.path.startswith(prefix) |
|
|
|
|
|
self.assertRaises(UnmergedEntriesError, three_way_index.write_tree) |
|
|
|
|
|
unmerged_blob_map = three_way_index.unmerged_blobs() |
|
assert unmerged_blob_map |
|
|
|
|
|
three_way_index.resolve_blobs(line[0][1] for line in unmerged_blob_map.values()) |
|
tree = three_way_index.write_tree() |
|
assert isinstance(tree, Tree) |
|
num_blobs = 0 |
|
for blob in tree.traverse(predicate=lambda item, d: item.type == "blob"): |
|
assert (blob.path, 0) in three_way_index.entries |
|
num_blobs += 1 |
|
|
|
self.assertEqual(num_blobs, len(three_way_index.entries)) |
|
|
|
@with_rw_repo("0.1.6") |
|
def test_index_merge_tree(self, rw_repo): |
|
|
|
self.assertNotEqual(self.rorepo, rw_repo) |
|
self.assertEqual(len({self.rorepo, self.rorepo, rw_repo, rw_repo}), 2) |
|
|
|
|
|
|
|
next_commit = "4c39f9da792792d4e73fc3a5effde66576ae128c" |
|
parent_commit = rw_repo.head.commit.parents[0] |
|
manifest_key = IndexFile.entry_key("MANIFEST.in", 0) |
|
manifest_entry = rw_repo.index.entries[manifest_key] |
|
rw_repo.index.merge_tree(next_commit) |
|
|
|
assert manifest_entry.binsha != rw_repo.index.entries[manifest_key].binsha |
|
|
|
rw_repo.index.reset(rw_repo.head) |
|
self.assertEqual(rw_repo.index.entries[manifest_key].binsha, manifest_entry.binsha) |
|
|
|
|
|
|
|
|
|
|
|
|
|
manifest_fake_entry = BaseIndexEntry((manifest_entry[0], b"\0" * 20, 0, manifest_entry[3])) |
|
|
|
self._assert_entries(rw_repo.index.add([manifest_fake_entry], write=False)) |
|
|
|
|
|
assert rw_repo.index.entries[manifest_key].binsha != Object.NULL_BIN_SHA |
|
|
|
|
|
index = rw_repo.index |
|
index.entries[manifest_key] = IndexEntry.from_base(manifest_fake_entry) |
|
index.write() |
|
self.assertEqual(rw_repo.index.entries[manifest_key].hexsha, Diff.NULL_HEX_SHA) |
|
|
|
|
|
rw_repo.index.write() |
|
|
|
|
|
|
|
|
|
self.assertRaises(GitCommandError, index.merge_tree, next_commit, base=parent_commit) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
index.entries[manifest_key] = IndexEntry(manifest_entry[:1] + (hex_to_bin("f" * 40),) + manifest_entry[2:]) |
|
tree = index.write_tree() |
|
|
|
|
|
unmerged_tree = IndexFile.from_tree(rw_repo, parent_commit, tree, next_commit) |
|
unmerged_blobs = unmerged_tree.unmerged_blobs() |
|
self.assertEqual(len(unmerged_blobs), 1) |
|
self.assertEqual(list(unmerged_blobs.keys())[0], manifest_key[0]) |
|
|
|
@with_rw_repo("0.1.6") |
|
def test_index_file_diffing(self, rw_repo): |
|
|
|
index = IndexFile(rw_repo) |
|
assert index.path is not None |
|
assert len(index.entries) |
|
|
|
|
|
index.write() |
|
|
|
|
|
|
|
|
|
|
|
|
|
cur_head_commit = rw_repo.head.reference.commit |
|
rw_repo.head.reset("HEAD~6", index=True, working_tree=False) |
|
|
|
|
|
diff = index.diff() |
|
self.assertEqual(len(diff), 0) |
|
|
|
|
|
diff = index.diff("HEAD") |
|
self.assertEqual(len(diff), 0) |
|
|
|
|
|
diff = index.diff(cur_head_commit) |
|
assert len(diff) |
|
|
|
|
|
adiff = index.diff(str(cur_head_commit), R=True) |
|
odiff = index.diff(cur_head_commit, R=False) |
|
assert adiff != odiff |
|
self.assertEqual(odiff, diff) |
|
|
|
|
|
wdiff = index.diff(None) |
|
assert wdiff != adiff |
|
assert wdiff != odiff |
|
|
|
|
|
self.assertRaises(ValueError, index.diff, int) |
|
|
|
|
|
cur_branch = rw_repo.active_branch |
|
cur_commit = cur_branch.commit |
|
rev_head_parent = "HEAD~1" |
|
assert index.reset(rev_head_parent) is index |
|
|
|
self.assertEqual(cur_branch, rw_repo.active_branch) |
|
self.assertEqual(cur_commit, rw_repo.head.commit) |
|
|
|
|
|
assert index.diff(None) |
|
|
|
|
|
new_data = b"will be reverted" |
|
file_path = osp.join(rw_repo.working_tree_dir, "CHANGES") |
|
with open(file_path, "wb") as fp: |
|
fp.write(new_data) |
|
index.reset(rev_head_parent, working_tree=True) |
|
assert not index.diff(None) |
|
self.assertEqual(cur_branch, rw_repo.active_branch) |
|
self.assertEqual(cur_commit, rw_repo.head.commit) |
|
with open(file_path, "rb") as fp: |
|
assert fp.read() != new_data |
|
|
|
|
|
test_file = osp.join(rw_repo.working_tree_dir, "CHANGES") |
|
with open(test_file, "ab") as fd: |
|
fd.write(b"some data") |
|
rval = index.checkout(None, force=True, fprogress=self._fprogress) |
|
assert "CHANGES" in list(rval) |
|
self._assert_fprogress([None]) |
|
assert osp.isfile(test_file) |
|
|
|
os.remove(test_file) |
|
rval = index.checkout(None, force=False, fprogress=self._fprogress) |
|
assert "CHANGES" in list(rval) |
|
self._assert_fprogress([None]) |
|
assert osp.isfile(test_file) |
|
|
|
|
|
os.remove(test_file) |
|
rval = index.checkout(test_file, fprogress=self._fprogress) |
|
self.assertEqual(list(rval)[0], "CHANGES") |
|
self._assert_fprogress([test_file]) |
|
assert osp.exists(test_file) |
|
|
|
|
|
self.assertRaises(CheckoutError, index.checkout, "doesnt_exist_ever.txt.that") |
|
self.assertRaises(CheckoutError, index.checkout, paths=["doesnt/exist"]) |
|
|
|
|
|
append_data = b"hello" |
|
with open(test_file, "ab") as fp: |
|
fp.write(append_data) |
|
try: |
|
index.checkout(test_file) |
|
except CheckoutError as e: |
|
|
|
if rw_repo.git.version_info < (2, 29): |
|
self.assertEqual(len(e.failed_files), 1) |
|
self.assertEqual(e.failed_files[0], osp.basename(test_file)) |
|
self.assertEqual(len(e.failed_files), len(e.failed_reasons)) |
|
self.assertIsInstance(e.failed_reasons[0], str) |
|
self.assertEqual(len(e.valid_files), 0) |
|
with open(test_file, "rb") as fd: |
|
s = fd.read() |
|
self.assertTrue(s.endswith(append_data), s) |
|
else: |
|
raise AssertionError("Exception CheckoutError not thrown") |
|
|
|
|
|
index.checkout(test_file, force=True) |
|
assert not open(test_file, "rb").read().endswith(append_data) |
|
|
|
|
|
rmtree(osp.join(rw_repo.working_tree_dir, "lib")) |
|
rval = index.checkout("lib") |
|
assert len(list(rval)) > 1 |
|
|
|
def _count_existing(self, repo, files): |
|
"""Return count of files that actually exist in the repository directory.""" |
|
existing = 0 |
|
basedir = repo.working_tree_dir |
|
for f in files: |
|
existing += osp.isfile(osp.join(basedir, f)) |
|
|
|
return existing |
|
|
|
|
|
|
|
@pytest.mark.xfail( |
|
sys.platform == "win32" and Git().config("core.symlinks") == "true", |
|
reason="Assumes symlinks are not created on Windows and opens a symlink to a nonexistent target.", |
|
raises=FileNotFoundError, |
|
) |
|
@with_rw_repo("0.1.6") |
|
def test_index_mutation(self, rw_repo): |
|
index = rw_repo.index |
|
num_entries = len(index.entries) |
|
cur_head = rw_repo.head |
|
|
|
uname = "Thomas Müller" |
|
umail = "sd@company.com" |
|
with rw_repo.config_writer() as writer: |
|
writer.set_value("user", "name", uname) |
|
writer.set_value("user", "email", umail) |
|
self.assertEqual(writer.get_value("user", "name"), uname) |
|
|
|
|
|
|
|
def mixed_iterator(): |
|
count = 0 |
|
for entry in index.entries.values(): |
|
type_id = count % 5 |
|
if type_id == 0: |
|
yield entry.path |
|
elif type_id == 1: |
|
yield Path(entry.path) |
|
elif type_id == 2: |
|
yield Blob(rw_repo, entry.binsha, entry.mode, entry.path) |
|
elif type_id == 3: |
|
yield BaseIndexEntry(entry[:4]) |
|
elif type_id == 4: |
|
yield entry |
|
else: |
|
raise AssertionError("Invalid Type") |
|
count += 1 |
|
|
|
|
|
|
|
deleted_files = index.remove(mixed_iterator(), working_tree=False) |
|
assert deleted_files |
|
self.assertEqual(self._count_existing(rw_repo, deleted_files), len(deleted_files)) |
|
self.assertEqual(len(index.entries), 0) |
|
|
|
|
|
index.reset() |
|
self.assertEqual(len(index.entries), num_entries) |
|
|
|
|
|
deleted_files = index.remove(mixed_iterator(), working_tree=True) |
|
assert deleted_files |
|
self.assertEqual(self._count_existing(rw_repo, deleted_files), 0) |
|
|
|
|
|
index.reset(working_tree=True) |
|
self.assertEqual(self._count_existing(rw_repo, deleted_files), len(deleted_files)) |
|
|
|
|
|
self.assertRaises(TypeError, index.remove, [1]) |
|
|
|
|
|
deleted_files = index.remove([osp.join(rw_repo.working_tree_dir, "lib")], r=True) |
|
assert len(deleted_files) > 1 |
|
self.assertRaises(ValueError, index.remove, ["/doesnt/exists"]) |
|
|
|
|
|
|
|
cur_commit = cur_head.commit |
|
commit_message = "commit default head by Frèderic Çaufl€" |
|
|
|
new_commit = index.commit(commit_message, head=False) |
|
assert cur_commit != new_commit |
|
self.assertEqual(new_commit.author.name, uname) |
|
self.assertEqual(new_commit.author.email, umail) |
|
self.assertEqual(new_commit.committer.name, uname) |
|
self.assertEqual(new_commit.committer.email, umail) |
|
self.assertEqual(new_commit.message, commit_message) |
|
self.assertEqual(new_commit.parents[0], cur_commit) |
|
self.assertEqual(len(new_commit.parents), 1) |
|
self.assertEqual(cur_head.commit, cur_commit) |
|
|
|
|
|
cur_commit = cur_head.commit |
|
|
|
my_author = Actor("Frèderic Çaufl€", "author@example.com") |
|
my_committer = Actor("Committing Frèderic Çaufl€", "committer@example.com") |
|
commit_actor = index.commit(commit_message, author=my_author, committer=my_committer) |
|
assert cur_commit != commit_actor |
|
self.assertEqual(commit_actor.author.name, "Frèderic Çaufl€") |
|
self.assertEqual(commit_actor.author.email, "author@example.com") |
|
self.assertEqual(commit_actor.committer.name, "Committing Frèderic Çaufl€") |
|
self.assertEqual(commit_actor.committer.email, "committer@example.com") |
|
self.assertEqual(commit_actor.message, commit_message) |
|
self.assertEqual(commit_actor.parents[0], cur_commit) |
|
self.assertEqual(len(new_commit.parents), 1) |
|
self.assertEqual(cur_head.commit, commit_actor) |
|
self.assertEqual(cur_head.log()[-1].actor, my_committer) |
|
|
|
|
|
cur_commit = cur_head.commit |
|
commit_message = "commit with dates by Avinash Sajjanshetty" |
|
|
|
new_commit = index.commit( |
|
commit_message, |
|
author_date="2006-04-07T22:13:13", |
|
commit_date="2005-04-07T22:13:13", |
|
) |
|
assert cur_commit != new_commit |
|
print(new_commit.authored_date, new_commit.committed_date) |
|
self.assertEqual(new_commit.message, commit_message) |
|
self.assertEqual(new_commit.authored_date, 1144447993) |
|
self.assertEqual(new_commit.committed_date, 1112911993) |
|
|
|
|
|
commit_message = "index without parents" |
|
commit_no_parents = index.commit(commit_message, parent_commits=[], head=True) |
|
self.assertEqual(commit_no_parents.message, commit_message) |
|
self.assertEqual(len(commit_no_parents.parents), 0) |
|
self.assertEqual(cur_head.commit, commit_no_parents) |
|
|
|
|
|
commit_message = "Index with multiple parents\n commit with another line" |
|
commit_multi_parent = index.commit(commit_message, parent_commits=(commit_no_parents, new_commit)) |
|
self.assertEqual(commit_multi_parent.message, commit_message) |
|
self.assertEqual(len(commit_multi_parent.parents), 2) |
|
self.assertEqual(commit_multi_parent.parents[0], commit_no_parents) |
|
self.assertEqual(commit_multi_parent.parents[1], new_commit) |
|
self.assertEqual(cur_head.commit, commit_multi_parent) |
|
|
|
|
|
|
|
index.reset(new_commit.parents[0], working_tree=True).reset(new_commit, working_tree=False) |
|
lib_file_path = osp.join("lib", "git", "__init__.py") |
|
assert (lib_file_path, 0) not in index.entries |
|
assert osp.isfile(osp.join(rw_repo.working_tree_dir, lib_file_path)) |
|
|
|
|
|
entries = index.add(["lib"], fprogress=self._fprogress_add) |
|
self._assert_entries(entries) |
|
self._assert_fprogress(entries) |
|
assert len(entries) > 1 |
|
|
|
|
|
entries = index.reset(new_commit).add([osp.join("lib", "git", "*.py")], fprogress=self._fprogress_add) |
|
self._assert_entries(entries) |
|
self._assert_fprogress(entries) |
|
self.assertEqual(len(entries), 14) |
|
|
|
|
|
entries = index.reset(new_commit).add( |
|
[osp.join(rw_repo.working_tree_dir, "lib", "git", "head.py")] * 2, |
|
fprogress=self._fprogress_add, |
|
) |
|
self._assert_entries(entries) |
|
self.assertEqual(entries[0].mode & 0o644, 0o644) |
|
|
|
|
|
self._reset_progress() |
|
self.assertEqual(len(entries), 2) |
|
|
|
|
|
self.assertRaises(OSError, index.reset(new_commit).add, ["doesnt/exist/must/raise"]) |
|
|
|
|
|
old_blob = new_commit.parents[0].tree.blobs[0] |
|
entries = index.reset(new_commit).add([old_blob], fprogress=self._fprogress_add) |
|
self._assert_entries(entries) |
|
self._assert_fprogress(entries) |
|
self.assertEqual(index.entries[(old_blob.path, 0)].hexsha, old_blob.hexsha) |
|
self.assertEqual(len(entries), 1) |
|
|
|
|
|
null_hex_sha = Diff.NULL_HEX_SHA |
|
null_bin_sha = b"\0" * 20 |
|
self.assertRaises( |
|
ValueError, |
|
index.reset(new_commit).add, |
|
[BaseIndexEntry((0, null_bin_sha, 0, "doesntmatter"))], |
|
) |
|
|
|
|
|
new_file_relapath = "my_new_file" |
|
self._make_file(new_file_relapath, "hello world", rw_repo) |
|
entries = index.reset(new_commit).add( |
|
[BaseIndexEntry((0o10644, null_bin_sha, 0, new_file_relapath))], |
|
fprogress=self._fprogress_add, |
|
) |
|
self._assert_entries(entries) |
|
self._assert_fprogress(entries) |
|
self.assertEqual(len(entries), 1) |
|
self.assertNotEqual(entries[0].hexsha, null_hex_sha) |
|
|
|
|
|
if sys.platform != "win32": |
|
for target in ("/etc/nonexisting", "/etc/passwd", "/etc"): |
|
basename = "my_real_symlink" |
|
|
|
link_file = osp.join(rw_repo.working_tree_dir, basename) |
|
os.symlink(target, link_file) |
|
entries = index.reset(new_commit).add([link_file], fprogress=self._fprogress_add) |
|
self._assert_entries(entries) |
|
self._assert_fprogress(entries) |
|
self.assertEqual(len(entries), 1) |
|
self.assertTrue(S_ISLNK(entries[0].mode)) |
|
self.assertTrue(S_ISLNK(index.entries[index.entry_key("my_real_symlink", 0)].mode)) |
|
|
|
|
|
self.assertEqual( |
|
index.repo.odb.stream(entries[0].binsha).read().decode("ascii"), |
|
target, |
|
) |
|
|
|
os.remove(link_file) |
|
|
|
|
|
|
|
|
|
fake_symlink_relapath = "my_fake_symlink" |
|
link_target = "/etc/that" |
|
fake_symlink_path = self._make_file(fake_symlink_relapath, link_target, rw_repo) |
|
fake_entry = BaseIndexEntry((0o120000, null_bin_sha, 0, fake_symlink_relapath)) |
|
entries = index.reset(new_commit).add([fake_entry], fprogress=self._fprogress_add) |
|
self._assert_entries(entries) |
|
self._assert_fprogress(entries) |
|
assert entries[0].hexsha != null_hex_sha |
|
self.assertEqual(len(entries), 1) |
|
self.assertTrue(S_ISLNK(entries[0].mode)) |
|
|
|
|
|
full_index_entry = IndexEntry.from_base(BaseIndexEntry((0o120000, entries[0].binsha, 0, entries[0].path))) |
|
entry_key = index.entry_key(full_index_entry) |
|
index.reset(new_commit) |
|
|
|
assert entry_key not in index.entries |
|
index.entries[entry_key] = full_index_entry |
|
index.write() |
|
index.update() |
|
new_entry = index.entries[entry_key] |
|
assert S_ISLNK(new_entry.mode) |
|
|
|
|
|
tree = index.write_tree() |
|
assert fake_symlink_relapath in tree |
|
index.write() |
|
|
|
|
|
assert not S_ISLNK(os.stat(fake_symlink_path)[ST_MODE]) |
|
os.remove(fake_symlink_path) |
|
index.checkout(fake_symlink_path) |
|
|
|
|
|
if sys.platform == "win32": |
|
|
|
|
|
with open(fake_symlink_path, "rt") as fd: |
|
self.assertEqual(fd.read(), link_target) |
|
else: |
|
self.assertTrue(S_ISLNK(os.lstat(fake_symlink_path)[ST_MODE])) |
|
|
|
|
|
def assert_mv_rval(rval): |
|
for source, dest in rval: |
|
assert not osp.exists(source) and osp.exists(dest) |
|
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, index.move, ["just_one_path"]) |
|
|
|
files = ["AUTHORS", "LICENSE"] |
|
self.assertRaises(GitCommandError, index.move, files) |
|
|
|
|
|
assert_mv_rval(index.move(files, f=True)) |
|
|
|
|
|
paths = ["LICENSE", "VERSION", "doc"] |
|
rval = index.move(paths, dry_run=True) |
|
self.assertEqual(len(rval), 2) |
|
assert osp.exists(paths[0]) |
|
|
|
|
|
rval = index.move(paths) |
|
assert_mv_rval(rval) |
|
|
|
|
|
rval = index.move(["doc", "test"]) |
|
assert_mv_rval(rval) |
|
|
|
|
|
|
|
count = [0] |
|
|
|
def rewriter(entry): |
|
rval = str(count[0]) |
|
count[0] += 1 |
|
return rval |
|
|
|
|
|
|
|
def make_paths(): |
|
"""Help out the test by yielding two existing paths and one new path.""" |
|
yield "CHANGES" |
|
yield "ez_setup.py" |
|
yield index.entries[index.entry_key("README", 0)] |
|
yield index.entries[index.entry_key(".gitignore", 0)] |
|
|
|
for fid in range(3): |
|
fname = "newfile%i" % fid |
|
with open(fname, "wb") as fd: |
|
fd.write(b"abcd") |
|
yield Blob(rw_repo, Blob.NULL_BIN_SHA, 0o100644, fname) |
|
|
|
|
|
|
|
paths = list(make_paths()) |
|
self._assert_entries(index.add(paths, path_rewriter=rewriter)) |
|
|
|
for filenum in range(len(paths)): |
|
assert index.entry_key(str(filenum), 0) in index.entries |
|
|
|
|
|
|
|
arela = "aa" |
|
brela = "bb" |
|
afile = self._make_file(arela, "adata", rw_repo) |
|
bfile = self._make_file(brela, "bdata", rw_repo) |
|
akey = index.entry_key(arela, 0) |
|
bkey = index.entry_key(brela, 0) |
|
keys = (akey, bkey) |
|
absfiles = (afile, bfile) |
|
files = (arela, brela) |
|
|
|
for fkey in keys: |
|
assert fkey not in index.entries |
|
|
|
index.add(files, write=True) |
|
nc = index.commit("2 files committed", head=False) |
|
|
|
for fkey in keys: |
|
assert fkey in index.entries |
|
|
|
|
|
index.reset(paths=(arela, afile)) |
|
assert akey not in index.entries |
|
assert bkey in index.entries |
|
|
|
|
|
rw_repo.head.commit = nc |
|
for absfile in absfiles: |
|
os.remove(absfile) |
|
|
|
index.reset(working_tree=True, paths=files) |
|
|
|
for fkey in keys: |
|
assert fkey in index.entries |
|
for absfile in absfiles: |
|
assert osp.isfile(absfile) |
|
|
|
@with_rw_repo("HEAD") |
|
def test_compare_write_tree(self, rw_repo): |
|
"""Test writing all trees, comparing them for equality.""" |
|
|
|
max_count = 25 |
|
count = 0 |
|
for commit in rw_repo.head.commit.traverse(): |
|
if count >= max_count: |
|
break |
|
count += 1 |
|
index = rw_repo.index.reset(commit) |
|
orig_tree = commit.tree |
|
self.assertEqual(index.write_tree(), orig_tree) |
|
|
|
|
|
@with_rw_repo("HEAD", bare=False) |
|
def test_index_single_addremove(self, rw_repo): |
|
fp = osp.join(rw_repo.working_dir, "testfile.txt") |
|
with open(fp, "w") as fs: |
|
fs.write("content of testfile") |
|
self._assert_entries(rw_repo.index.add(fp)) |
|
deleted_files = rw_repo.index.remove(fp) |
|
assert deleted_files |
|
|
|
def test_index_new(self): |
|
B = self.rorepo.tree("6d9b1f4f9fa8c9f030e3207e7deacc5d5f8bba4e") |
|
H = self.rorepo.tree("25dca42bac17d511b7e2ebdd9d1d679e7626db5f") |
|
M = self.rorepo.tree("e746f96bcc29238b79118123028ca170adc4ff0f") |
|
|
|
for args in ((B,), (B, H), (B, H, M)): |
|
index = IndexFile.new(self.rorepo, *args) |
|
assert isinstance(index, IndexFile) |
|
|
|
|
|
@with_rw_repo("HEAD", bare=True) |
|
def test_index_bare_add(self, rw_bare_repo): |
|
|
|
|
|
|
|
|
|
assert rw_bare_repo.working_tree_dir is None |
|
assert rw_bare_repo.bare |
|
contents = b"This is a BytesIO file" |
|
filesize = len(contents) |
|
fileobj = BytesIO(contents) |
|
filename = "my-imaginary-file" |
|
istream = rw_bare_repo.odb.store(IStream(Blob.type, filesize, fileobj)) |
|
entry = BaseIndexEntry((0o100644, istream.binsha, 0, filename)) |
|
try: |
|
rw_bare_repo.index.add([entry]) |
|
except AssertionError: |
|
self.fail("Adding to the index of a bare repo is not allowed.") |
|
|
|
|
|
asserted = False |
|
path = osp.join("git", "test", "test_index.py") |
|
try: |
|
rw_bare_repo.index.add([path]) |
|
except InvalidGitRepositoryError: |
|
asserted = True |
|
assert asserted, "Adding using a filename is not correctly asserted." |
|
|
|
@with_rw_directory |
|
def test_add_utf8P_path(self, rw_dir): |
|
|
|
|
|
fp = osp.join(rw_dir, "ø.txt") |
|
with open(fp, "wb") as fs: |
|
fs.write("content of ø".encode("utf-8")) |
|
|
|
r = Repo.init(rw_dir) |
|
r.index.add([fp]) |
|
r.index.commit("Added orig and prestable") |
|
|
|
@with_rw_directory |
|
def test_add_a_file_with_wildcard_chars(self, rw_dir): |
|
|
|
fp = osp.join(rw_dir, "[.exe") |
|
with open(fp, "wb") as f: |
|
f.write(b"something") |
|
|
|
r = Repo.init(rw_dir) |
|
r.index.add([fp]) |
|
r.index.commit("Added [.exe") |
|
|
|
def test__to_relative_path_at_root(self): |
|
root = osp.abspath(os.sep) |
|
|
|
class Mocked: |
|
bare = False |
|
git_dir = root |
|
working_tree_dir = root |
|
|
|
repo = Mocked() |
|
path = os.path.join(root, "file") |
|
index = IndexFile(repo) |
|
|
|
rel = index._to_relative_path(path) |
|
self.assertEqual(rel, os.path.relpath(path, root)) |
|
|
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.Absent, |
|
reason="Can't run a hook on Windows without bash.exe.", |
|
rasies=HookExecutionError, |
|
) |
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.WslNoDistro, |
|
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", |
|
raises=HookExecutionError, |
|
) |
|
@with_rw_repo("HEAD", bare=True) |
|
def test_run_commit_hook(self, rw_repo): |
|
index = rw_repo.index |
|
_make_hook(index.repo.git_dir, "fake-hook", "echo 'ran fake hook' >output.txt") |
|
run_commit_hook("fake-hook", index) |
|
output = Path(rw_repo.git_dir, "output.txt").read_text(encoding="utf-8") |
|
self.assertEqual(output, "ran fake hook\n") |
|
|
|
@ddt.data((False,), (True,)) |
|
@with_rw_directory |
|
def test_hook_uses_shell_not_from_cwd(self, rw_dir, case): |
|
(chdir_to_repo,) = case |
|
|
|
shell_name = "bash.exe" if sys.platform == "win32" else "sh" |
|
maybe_chdir = cwd(rw_dir) if chdir_to_repo else contextlib.nullcontext() |
|
repo = Repo.init(rw_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
venv = VirtualEnvironment(rw_dir, with_pip=False) |
|
shutil.copy(venv.python, Path(rw_dir, shell_name)) |
|
shutil.copy(fixture_path("polyglot"), hook_path("polyglot", repo.git_dir)) |
|
payload = Path(rw_dir, "payload.txt") |
|
|
|
if type(_win_bash_status) in {WinBashStatus.Absent, WinBashStatus.WslNoDistro}: |
|
|
|
with self.assertRaises(HookExecutionError): |
|
with maybe_chdir: |
|
run_commit_hook("polyglot", repo.index) |
|
self.assertFalse(payload.exists()) |
|
else: |
|
|
|
with maybe_chdir: |
|
run_commit_hook("polyglot", repo.index) |
|
self.assertFalse(payload.exists()) |
|
output = Path(rw_dir, "output.txt").read_text(encoding="utf-8") |
|
self.assertEqual(output, "Ran intended hook.\n") |
|
|
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.Absent, |
|
reason="Can't run a hook on Windows without bash.exe.", |
|
rasies=HookExecutionError, |
|
) |
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.WslNoDistro, |
|
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", |
|
raises=HookExecutionError, |
|
) |
|
@with_rw_repo("HEAD", bare=True) |
|
def test_pre_commit_hook_success(self, rw_repo): |
|
index = rw_repo.index |
|
_make_hook(index.repo.git_dir, "pre-commit", "exit 0") |
|
index.commit("This should not fail") |
|
|
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.WslNoDistro, |
|
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", |
|
raises=AssertionError, |
|
) |
|
@with_rw_repo("HEAD", bare=True) |
|
def test_pre_commit_hook_fail(self, rw_repo): |
|
index = rw_repo.index |
|
hp = _make_hook(index.repo.git_dir, "pre-commit", "echo stdout; echo stderr 1>&2; exit 1") |
|
try: |
|
index.commit("This should fail") |
|
except HookExecutionError as err: |
|
if type(_win_bash_status) is WinBashStatus.Absent: |
|
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 caught a HookExecutionError") |
|
|
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.Absent, |
|
reason="Can't run a hook on Windows without bash.exe.", |
|
rasies=HookExecutionError, |
|
) |
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.Wsl, |
|
reason="Specifically seems to fail on WSL bash (in spite of #1399)", |
|
raises=AssertionError, |
|
) |
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.WslNoDistro, |
|
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", |
|
raises=HookExecutionError, |
|
) |
|
@with_rw_repo("HEAD", bare=True) |
|
def test_commit_msg_hook_success(self, rw_repo): |
|
commit_message = "commit default head by Frèderic Çaufl€" |
|
from_hook_message = "from commit-msg" |
|
index = rw_repo.index |
|
_make_hook( |
|
index.repo.git_dir, |
|
"commit-msg", |
|
'printf " {}" >> "$1"'.format(from_hook_message), |
|
) |
|
new_commit = index.commit(commit_message) |
|
self.assertEqual(new_commit.message, "{} {}".format(commit_message, from_hook_message)) |
|
|
|
@pytest.mark.xfail( |
|
type(_win_bash_status) is WinBashStatus.WslNoDistro, |
|
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", |
|
raises=AssertionError, |
|
) |
|
@with_rw_repo("HEAD", bare=True) |
|
def test_commit_msg_hook_fail(self, rw_repo): |
|
index = rw_repo.index |
|
hp = _make_hook(index.repo.git_dir, "commit-msg", "echo stdout; echo stderr 1>&2; exit 1") |
|
try: |
|
index.commit("This should fail") |
|
except HookExecutionError as err: |
|
if type(_win_bash_status) is WinBashStatus.Absent: |
|
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 caught a HookExecutionError") |
|
|
|
@with_rw_repo("HEAD") |
|
def test_index_add_pathlike(self, rw_repo): |
|
git_dir = Path(rw_repo.git_dir) |
|
|
|
file = git_dir / "file.txt" |
|
file.touch() |
|
|
|
rw_repo.index.add(file) |
|
|
|
|
|
class TestIndexUtils: |
|
@pytest.mark.parametrize("file_path_type", [str, Path]) |
|
def test_temporary_file_swap(self, tmp_path, file_path_type): |
|
file_path = tmp_path / "foo" |
|
file_path.write_bytes(b"some data") |
|
|
|
with TemporaryFileSwap(file_path_type(file_path)) as ctx: |
|
assert Path(ctx.file_path) == file_path |
|
assert not file_path.exists() |
|
|
|
|
|
file_path.write_bytes(b"other data") |
|
|
|
temp_file_path = Path(ctx.tmp_file_path) |
|
assert temp_file_path.parent == file_path.parent |
|
assert temp_file_path.name.startswith(file_path.name) |
|
assert temp_file_path.read_bytes() == b"some data" |
|
|
|
assert not temp_file_path.exists() |
|
assert file_path.read_bytes() == b"some data" |
|
|