def test_insert_tags(self): """Test inserting of review tags""" msg = '''first line second line.''' tags = [ 'Reviewed-by: Bin Meng <*****@*****.**>', 'Tested-by: Bin Meng <*****@*****.**>' ] signoff = 'Signed-off-by: Simon Glass <*****@*****.**>' tag_str = '\n'.join(tags) new_msg = patchstream.insert_tags(msg, tags) self.assertEqual(msg + '\n\n' + tag_str, new_msg) new_msg = patchstream.insert_tags(msg + '\n', tags) self.assertEqual(msg + '\n\n' + tag_str, new_msg) msg += '\n\n' + signoff new_msg = patchstream.insert_tags(msg, tags) self.assertEqual(msg + '\n' + tag_str, new_msg)
def create_branch(series, new_rtag_list, branch, dest_branch, overwrite, repo=None): """Create a new branch with review tags added Args: series (Series): Series object for the existing branch new_rtag_list (list): List of review tags to add, one for each commit, each a dict: key: Response tag (e.g. 'Reviewed-by') value: Set of people who gave that response, each a name/email string branch (str): Existing branch to update dest_branch (str): Name of new branch to create overwrite (bool): True to force overwriting dest_branch if it exists repo (pygit2.Repository): Repo to use (use None unless testing) Returns: int: Total number of review tags added across all commits Raises: ValueError: if the destination branch name is the same as the original branch, or it already exists and @overwrite is False """ if branch == dest_branch: raise ValueError( 'Destination branch must not be the same as the original branch') if not repo: repo = pygit2.Repository('.') count = len(series.commits) new_br = repo.branches.get(dest_branch) if new_br: if not overwrite: raise ValueError("Branch '%s' already exists (-f to overwrite)" % dest_branch) new_br.delete() if not branch: branch = 'HEAD' target = repo.revparse_single('%s~%d' % (branch, count)) repo.branches.local.create(dest_branch, target) num_added = 0 for seq in range(count): parent = repo.branches.get(dest_branch) cherry = repo.revparse_single('%s~%d' % (branch, count - seq - 1)) repo.merge_base(cherry.oid, parent.target) base_tree = cherry.parents[0].tree index = repo.merge_trees(base_tree, parent, cherry) tree_id = index.write_tree(repo) lines = [] if new_rtag_list[seq]: for tag, people in new_rtag_list[seq].items(): for who in people: lines.append('%s: %s' % (tag, who)) num_added += 1 message = patchstream.insert_tags(cherry.message.rstrip(), sorted(lines)) repo.create_commit(parent.name, cherry.author, cherry.committer, message, tree_id, [parent.target]) return num_added