def test_new_commit(self): repo = self.repo message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding='utf-8') tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] too_short_prefix = tree[:3] parents = [COMMIT_SHA[:5]] with pytest.raises(ValueError): repo.create_commit(None, author, committer, message, too_short_prefix, parents) sha = repo.create_commit(None, author, committer, message, tree_prefix, parents) commit = repo[sha] assert GIT_OBJ_COMMIT == commit.type assert '98286caaab3f1fde5bf52c8369b2b0423bad743b' == commit.hex assert commit.message_encoding is None assert message == commit.message assert 12346 == commit.commit_time assert committer == commit.committer assert author == commit.author assert tree == commit.tree.hex assert Oid(hex=tree) == commit.tree_id assert 1 == len(commit.parents) assert COMMIT_SHA == commit.parents[0].hex assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
def commit(self): if self.tree_modifier.tree.oid != self.get_last_tree().oid: raise Exception("The repository was modified outside of this process. For safety reasons, we cannot commit!") self.working_tree = self.tree_modifier.apply() self.tree_modifier = TreeModifier(self.repo, self.working_tree) if self.repo.head_is_unborn: parents = [] else: commit = self.repo[self.getCurrentCommit()] if commit.tree.id == self.working_tree.id: return parents = [commit.id] config = self.repo.config author = Signature(config['user.name'], config['user.email']) committer = Signature(config['user.name'], config['user.email']) tree_id = self.working_tree.id message = '\n'.join(self.messages) self.repo.create_commit('refs/heads/master', author, committer, message, tree_id, parents) self.saveCurrentCommit() self.messages = [] if not self.repo.is_bare and self.update_working_copy: self.repo.index.read_tree(self.working_tree) self.repo.index.write()
def setUp(self): self.dir = TemporaryDirectory() self.file = NamedTemporaryFile(dir=self.dir.name, delete=False) self.filename = path.basename(self.file.name) self.author = Signature('QuitStoreTest', '*****@*****.**') self.comitter = Signature('QuitStoreTest', '*****@*****.**') self.repo = quit.git.Repository(self.dir.name, create=True)
def test_new_commit(self): repo = self.repo message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding='utf-8') tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] too_short_prefix = tree[:3] parents = [COMMIT_SHA[:5]] self.assertRaises(ValueError, repo.create_commit, None, author, committer, message, too_short_prefix, parents) sha = repo.create_commit(None, author, committer, message, tree_prefix, parents) commit = repo[sha] self.assertEqual(GIT_OBJ_COMMIT, commit.type) self.assertEqual('98286caaab3f1fde5bf52c8369b2b0423bad743b', commit.hex) self.assertEqual(None, commit.message_encoding) self.assertEqual(message, commit.message) self.assertEqual(12346, commit.commit_time) self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(author, commit.author) self.assertEqual(tree, commit.tree.hex) self.assertEqual(Oid(hex=tree), commit.tree_id) self.assertEqual(1, len(commit.parents)) self.assertEqual(COMMIT_SHA, commit.parents[0].hex) self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])
def test_new_commit_encoding(self): repo = self.repo encoding = 'iso-8859-1' message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0, encoding) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding) tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] parents = [COMMIT_SHA[:5]] sha = repo.create_commit(None, author, committer, message, tree_prefix, parents, encoding) commit = repo[sha] self.assertEqual(GIT_OBJ_COMMIT, commit.type) self.assertEqual('iso-8859-1', commit.message_encoding) self.assertEqual(message.encode(encoding), commit.raw_message) self.assertEqual(12346, commit.commit_time) self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(author, commit.author) self.assertEqual(tree, commit.tree.hex) self.assertEqual(Oid(hex=tree), commit.tree_id) self.assertEqual(1, len(commit.parents)) self.assertEqual(COMMIT_SHA, commit.parents[0].hex) self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])
def test_new_commit_encoding(self): repo = self.repo encoding = 'iso-8859-1' message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0, encoding) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding) tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] parents = [COMMIT_SHA[:5]] sha = repo.create_commit(None, author, committer, message, tree_prefix, parents, encoding) commit = repo[sha] assert GIT_OBJ_COMMIT == commit.type assert 'iso-8859-1' == commit.message_encoding assert message.encode(encoding) == commit.raw_message assert 12346 == commit.commit_time assert committer == commit.committer assert author == commit.author assert tree == commit.tree.hex assert Oid(hex=tree) == commit.tree_id assert 1 == len(commit.parents) assert COMMIT_SHA == commit.parents[0].hex assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
def setUp(self): self.dir = TemporaryDirectory() self.remotedir = TemporaryDirectory() self.file = NamedTemporaryFile(dir=self.dir.name, delete=False) self.filename = path.basename(self.file.name) self.author = Signature('QuitStoreTest', '*****@*****.**') self.comitter = Signature('QuitStoreTest', '*****@*****.**') # Initialize repository init_repository(self.dir.name, False)
def __init__(self, path=None, bare=False, author_name='hexastore', author_email='hexastore@git'): path = path or self.__class__.__name__.lower() self.path = path self.bare = bare self.repository = init_repository(path, bare=bare) self.author = Signature(author_name, author_email) self.commiter = Signature(author_name, author_email) self.queries = [] self.default_branch = 'refs/heads/master'
def test_read_commit(self): commit = self.repo[COMMIT_SHA] assert COMMIT_SHA == str(commit.id) parents = commit.parents assert 1 == len(parents) assert 'c2792cfa289ae6321ecf2cd5806c2194b0fd070c' == str(parents[0].id) assert commit.message_encoding is None assert commit.message == ('Second test data commit.\n\n' 'This commit has some additional text.\n') commit_time = 1288481576 assert commit_time == commit.commit_time assert commit.committer == Signature('Dave Borowitz', '*****@*****.**', commit_time, -420) assert commit.author == Signature('Dave Borowitz', '*****@*****.**', 1288477363, -420) assert '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' == str(commit.tree.id)
def git_commit(self, message, author=None, date=None): """Equivalent to 'git commit', we must give the message and we can also give the author and date. """ from calendar import timegm # TODO Check the 'nothing to commit' case # Write index self.index.write() self.index_mtime = getmtime(self.index_path) # Tree tree = self.index.write_tree() # Parent parent = self._resolve_reference('HEAD') parents = [parent] if parent else [] # Committer when_time = time.time() when_offset = -(time.altzone if time.daylight else time.timezone) when_offset = when_offset / 60 name = self.username email = self.useremail committer = Signature(name, email, when_time, when_offset) # Author if author is None: author = (name, email) if date: if date.tzinfo: from pytz import utc when_time = date.astimezone(utc) # To UTC when_time = when_time.timetuple() # As struct_time when_time = timegm(when_time) # To unix time when_offset = date.utcoffset().seconds / 60 else: err = "Worktree.git_commit doesn't support naive datatime yet" raise NotImplementedError, err author = Signature(author[0], author[1], when_time, when_offset) # Create the commit return self.repo.create_commit('HEAD', author, committer, message, tree, parents)
def edit(self, new_markdown: str, user: User, edit_message: str) -> None: """Set the page's markdown, render its HTML, and commit the repo.""" if new_markdown == self.markdown: return self.markdown = new_markdown self.rendered_html = convert_markdown_to_safe_html(new_markdown) self.rendered_html = add_anchors_to_headings(self.rendered_html) self.last_edited_time = utc_now() repo = Repository(self.BASE_PATH) author = Signature(user.username, user.username) repo.index.read() repo.index.add(str(self.file_path.relative_to(self.BASE_PATH))) repo.index.write() # Prepend the group name and page path to the edit message - if you change the # format of this, make sure to also change the page-editing template to match edit_message = f"~{self.group.path}/{self.path}: {edit_message}" repo.create_commit( repo.head.name, author, author, edit_message, repo.index.write_tree(), [repo.head.target], )
def _commit(self, original_commits, blobs): for filename, blob_id in blobs.items(): file_mode = self._file_modes[filename] index_entry = IndexEntry(filename, blob_id, file_mode) self.repo.index.add(index_entry) commits = [self.repo.get(h) for h in original_commits] main_commit = commits[0] if len(commits) > 1: # most recent commit main_commit = sorted(commits, key=commit_datetime)[-1] commit_message = main_commit.message commit_message += "\n\nautomatic commit by git-black, original commits:\n" commit_message += "\n".join( [" {}".format(c) for c in original_commits]) committer = Signature( name=self.repo.config["user.name"], email=self.repo.config["user.email"], ) self.repo.index.write() tree = self.repo.index.write_tree() head = self.repo.head.peel() self.repo.create_commit("HEAD", main_commit.author, committer, commit_message, tree, [head.id]) self.progress += 1 now = time.monotonic() if now - self.last_log > 0.04: sys.stdout.write("Making commit {}/{} \r".format( self.progress, self.total)) sys.stdout.flush() self.last_log = now
def test_default(self): signature = Signature( 'Foo', '*****@*****.**', 1322174594, 60) encoding = signature._encoding self.assertEqual(encoding, 'ascii') self.assertEqual(signature.name, signature._name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature._name)
def append_reflog(repository, reference, name, email, message): try: ref = repository.lookup_reference(reference) except KeyError: return True committer = Signature(name, email) ref.append_log(committer, message)
def test_latin1(self): encoding = 'iso-8859-1' signature = Signature('Foo Ibáñez', '*****@*****.**', 1322174594, 60, encoding) self.assertEqual(encoding, signature._encoding) self.assertEqual(signature.name, signature._name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature._name)
def __init__(self): self.committer = Signature('GitStatusBot', '*****@*****.**') self.push_user = '******' self.flagerrors = ['GIT_STATUS_CONFLICTED', 'GIT_STATUS_WT_UNREADABLE',\ 'GIT_STATUS_WT_TYPECHANGE', 'GIT_STATUS_INDEX_TYPECHANGE'] self.flagupdate = ['GIT_STATUS_WT_NEW', 'GIT_STATUS_WT_RENAMED',\ 'GIT_STATUS_WT_MODIFIED', 'GIT_STATUS_WT_DELETED'] self.flagcommit = ['GIT_STATUS_INDEX_NEW', 'GIT_STATUS_INDEX_RENAMED',\ 'GIT_STATUS_INDEX_MODIFIED', 'GIT_STATUS_INDEX_DELETED'] self.status_flags = {'GIT_STATUS_CURRENT' : pygit2.GIT_STATUS_CURRENT,\ 'GIT_STATUS_INDEX_NEW' : pygit2.GIT_STATUS_INDEX_NEW,\ 'GIT_STATUS_INDEX_MODIFIED' : pygit2.GIT_STATUS_INDEX_MODIFIED,\ 'GIT_STATUS_INDEX_DELETED' : pygit2.GIT_STATUS_INDEX_DELETED,\ 'GIT_STATUS_INDEX_RENAMED' : pygit2.GIT_STATUS_INDEX_RENAMED,\ 'GIT_STATUS_INDEX_TYPECHANGE' : pygit2.GIT_STATUS_INDEX_TYPECHANGE,\ 'GIT_STATUS_WT_NEW' : pygit2.GIT_STATUS_WT_NEW,\ 'GIT_STATUS_WT_MODIFIED' : pygit2.GIT_STATUS_WT_MODIFIED,\ 'GIT_STATUS_WT_DELETED' : pygit2.GIT_STATUS_WT_DELETED,\ 'GIT_STATUS_WT_TYPECHANGE' : pygit2.GIT_STATUS_WT_TYPECHANGE,\ 'GIT_STATUS_WT_RENAMED' : pygit2.GIT_STATUS_WT_RENAMED,\ 'GIT_STATUS_WT_UNREADABLE' : pygit2.GIT_STATUS_WT_UNREADABLE,\ 'GIT_STATUS_IGNORED' : pygit2.GIT_STATUS_IGNORED,\ 'GIT_STATUS_CONFLICTED' : pygit2.GIT_STATUS_CONFLICTED} self.status_flags_value = dict( (v, k) for k, v in self.status_flags.items())
def commit(repo, message='init package', branch=APP_GIT_BRANCH, init=False): """ Create a commit for all changes in a given repo :param repo: The repo to be committed :param message: Message for the commit :param branch: The branch to commit, if not specified, master branch will be used :param init: indicates whether this commit is a initial commit. This is use to initialize git repo since git requires a commit to use branch :return: None """ if repo.status() == {} and not init: raise CleanDirError('No changes detected') index = repo.index # Add all changes to git index.add_all() index.write() tree = index.write_tree() author = Signature(USER['name'], USER['email']) # Get branch to push branch = repo.lookup_branch(branch) # Create commit repo.create_commit( APP_GIT_REF, author, author, message, tree, [] if len(repo.listall_branches()) == 0 else [branch.target])
def test_remove_note(barerepo): head = barerepo.head note = barerepo.lookup_note(head.target.hex) author = committer = Signature('Foo bar', '*****@*****.**', 12346, 0) note.remove(author, committer) with pytest.raises(KeyError): barerepo.lookup_note(head.target.hex)
def commit_file( self, filename, content, author, email, branch_name="master", message="", timezone=0, first_commit=False, parent_commit_id=0, ): if not first_commit: if parent_commit_id: parent_commit = self.get(parent_commit_id) else: parent_commit = self._get_commit(branch_name) parents = [parent_commit.id] tree_parent = parent_commit.tree_id tree_builder = self.TreeBuilder(tree_parent) else: parents = [] tree_builder = self.TreeBuilder() blob_id = self.create_blob(content.encode(encoding="UTF-8")) tree_builder.insert(filename, blob_id, GIT_FILEMODE_BLOB) tree = tree_builder.write() author = committer = Signature(author, email, int(time()), timezone) commit_id = self.create_commit("refs/heads/" + branch_name, author, committer, message, tree, parents) if commit_id: return {"commit_id": str(commit_id), "blob_id": str(blob_id)} return {"commit_id": "", "blob_id": ""}
def test_default(): signature = Signature('Foo Ibáñez', '*****@*****.**', 1322174594, 60) encoding = signature._encoding assert encoding == 'utf-8' assert signature.name == signature.raw_name.decode(encoding) assert signature.name.encode(encoding) == signature.raw_name assert signature.email == signature.raw_email.decode(encoding) assert signature.email.encode(encoding) == signature.raw_email
def test_now(self): encoding = 'utf-8' signature = Signature( 'Foo Ibáñez', '*****@*****.**', encoding=encoding) self.assertEqual(encoding, signature._encoding) self.assertEqual(signature.name, signature._name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature._name) self.assertTrue(abs(signature.time - time.time()) < 5)
def add_version_commit(): repo = Repository(script_dir) create_commit(repo, 'Update to {}'.format(ver_str)) config = Config.get_global_config() author = Signature(config['user.name'], config['user.email']) repo.create_tag('v{}'.format(ver_str), repo.revparse_single('HEAD').id, GIT_OBJ_COMMIT, author, 'v{}'.format(ver_str))
def test_default(self): signature = Signature('Foo Ibáñez', '*****@*****.**', 1322174594, 60) encoding = signature._encoding self.assertEqual(encoding, 'utf-8') self.assertEqual(signature.name, signature.raw_name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature.raw_name) self.assertEqual(signature.email, signature.raw_email.decode(encoding)) self.assertEqual(signature.email.encode(encoding), signature.raw_email)
def test_create_note(barerepo): annotated_id = barerepo.revparse_single('HEAD~3').hex author = committer = Signature('Foo bar', '*****@*****.**', 12346, 0) note_id = barerepo.create_note(NOTE[1], author, committer, annotated_id) assert NOTE[0] == note_id.hex # check the note blob assert NOTE[1].encode() == barerepo[note_id].data
def test_latin1(): encoding = 'iso-8859-1' signature = Signature('Foo Ibáñez', '*****@*****.**', encoding=encoding) assert encoding == signature._encoding assert signature.name == signature.raw_name.decode(encoding) assert signature.name.encode(encoding) == signature.raw_name assert signature.email == signature.raw_email.decode(encoding) assert signature.email.encode(encoding) == signature.raw_email
def test_create_note(self): annotated_id = self.repo.revparse_single('HEAD~3').hex author = committer = Signature('Foo bar', '*****@*****.**', 12346, 0) note_id = self.repo.create_note(NOTE[1], author, committer, annotated_id) self.assertEqual(NOTE[0], note_id.hex) # check the note blob self.assertEqual(NOTE[1].encode(), self.repo[note_id].data)
def create_tag(repository, name, ref, author_name, author_email, message): obj = repository.revparse_single(ref) if obj.type == GIT_OBJ_COMMIT: signature = Signature(author_name, author_email) oid = repository.create_tag(name, str(obj.id), GIT_OBJ_COMMIT, signature, message) return oid and str(oid) else: return None
def test_set_target_with_message(self): reference = self.repo.lookup_reference('HEAD') self.assertEqual(reference.target, 'refs/heads/master') sig = Signature('foo', 'bar') msg = 'Hello log' reference.set_target('refs/heads/i18n', signature=sig, message=msg) self.assertEqual(reference.target, 'refs/heads/i18n') self.assertEqual(list(reference.log())[0].message, msg) self.assertEqualSignature(list(reference.log())[0].committer, sig)
def test_now(): encoding = 'utf-8' signature = Signature('Foo Ibáñez', '*****@*****.**', encoding=encoding) assert encoding == signature._encoding assert signature.name == signature.raw_name.decode(encoding) assert signature.name.encode(encoding) == signature.raw_name assert signature.email == signature.raw_email.decode(encoding) assert signature.email.encode(encoding) == signature.raw_email assert abs(signature.time - time.time()) < 5
def git_commit(repo, tree, files, parents=[]): for f in files: repo.index.add(f) entry = repo.index[f] tree.insert(f, entry.oid, 0o100644) repo.index.write() sign = Signature('foo', '*****@*****.**') return repo.create_commit('refs/heads/master', sign, sign, 'foo', tree.write(), parents)