def get_tag_list(localRepo, bookid): repo_home = pathof(localRepo) repo_home = repo_home.replace("/", os.sep) repo_path = os.path.join(repo_home, "epub_" + bookid) cdir = os.getcwd() taglst = [] if os.path.exists(repo_path): os.chdir(repo_path) with open_repo_closing(".") as r: tags = sorted(r.refs.as_dict(b"refs/tags")) for atag in tags: tagkey = b"refs/tags/" + atag obj = r[tagkey] tag_name = unicode_str(atag) tag_message = "" tag_date = "" if isinstance(obj,Tag): time_tuple = time.gmtime(obj.tag_time + obj.tag_timezone) time_str = time.strftime("%a %b %d %Y %H:%M:%S",time_tuple) timezone_str = format_timezone(obj.tag_timezone).decode('ascii') tag_date = time_str + " " + timezone_str tag_message = unicode_str(obj.message) if isinstance(obj, Commit): time_tuple = time.gmtime(obj.author_time + obj.author_timezone) time_str = time.strftime("%a %b %d %Y %H:%M:%S",time_tuple) timezone_str = format_timezone(obj.author_timezone).decode('ascii') tag_date = time_str + " " + timezone_str tag_message = unicode_str(obj.message) taglst.append(tag_name + "|" + tag_date + "|" + tag_message) os.chdir(cdir) return taglst
def _dump_commit(self, commit, marker, ref, file_changes): self.outf.write("commit %s\n" % ref) self.outf.write("mark :%s\n" % marker) self.outf.write("author %s %s %s\n" % (commit.author, commit.author_time, format_timezone(commit.author_timezone))) self.outf.write("committer %s %s %s\n" % (commit.committer, commit.commit_time, format_timezone(commit.commit_timezone))) self.outf.write("data %s\n" % len(commit.message)) self.outf.write(commit.message) self.outf.write("\n") self.outf.write('\n'.join(file_changes)) self.outf.write("\n\n")
def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message): """Generate a single reflog line. :param old_sha: Old Commit SHA :param new_sha: New Commit SHA :param committer: Committer name and e-mail :param timestamp: Timestamp :param timezone: Timezone :param message: Message """ if old_sha is None: old_sha = ZERO_SHA return ( old_sha + b" " + new_sha + b" " + committer + b" " + str(timestamp).encode("ascii") + b" " + format_timezone(timezone) + b"\t" + message )
def keyword_values(repo, ref): expanded_ref = bytes(full_ref(repo, ref), encoding='ascii') version = None commit_timestamp = None author = None try: if isinstance(repo[expanded_ref], Tag): sha = repo[expanded_ref].object[1] else: sha = repo.refs[expanded_ref] short_sha = sha[:7].decode(encoding='ascii') commit = repo.get_object(sha) commit_time = strftime('%Y-%m-%d %H:%M:%S', gmtime(commit.commit_time)) timezone = format_timezone( commit.commit_timezone).decode(encoding='ascii') commit_timestamp = commit_time + ' ' + timezone author = commit.author.decode(encoding='ascii') if ref.startswith('refs/tags'): version = 'Tag %s (%s)' % (ref, short_sha) else: version = short_sha except KeyError: logger.error('%s is not a valid branch or tag' % ref) return version, commit_timestamp, author
def get_committer(self, ctx): extra = ctx.extra() if 'committer' in extra: # fixup timezone (name_timestamp, timezone) = extra['committer'].rsplit(' ', 1) try: timezone = format_timezone(-int(timezone)) return '%s %s' % (name_timestamp, timezone) except ValueError: self.ui.warn(_("Ignoring committer in extra, invalid timezone in r%s: '%s'.\n") % (ctx.rev(), timezone)) return None
def substitute_keywords(text, repo, ref): """Perform keyword substitution on given text Substitutes the keywords 'version:', 'date:' and 'author:' with the relevant attributes extracted from the repository for the given ref. Parameters ---------- text : str repo : dulwich.repo.Repo ref : str Returns ------- str """ new_text = '' expanded_ref = full_ref(repo, ref) try: sha = repo.refs[bytes(expanded_ref, encoding='ascii')] short_sha = sha[:7].decode(encoding='ascii') commit = repo.get_object(sha) commit_time = strftime('%Y-%m-%d %H:%M:%S', gmtime(commit.commit_time)) timezone = format_timezone( commit.commit_timezone).decode(encoding='ascii') commit_timestamp = commit_time + ' ' + timezone author = commit.author.decode(encoding='ascii') if ref.startswith('refs/tags'): version = 'Tag %s (%s)' % (ref, short_sha) else: version = short_sha substitutions = { 'version': version, 'date': commit_timestamp, 'author': author } for line in text.splitlines(keepends=True): for key, value in substitutions.items(): rexp = '%s:.*' % key line = re.sub(rexp, '%s: %s' % (key, value), line) new_text += line except KeyError: logger.error('%s is not a valid branch or tag' % ref) return new_text
def print_tag(tag, decode, outstream=sys.stdout): """Write a human-readable tag. :param tag: A `Tag` object :param decode: Function for decoding bytes to unicode string :param outstream: A stream to write to """ outstream.write("Tagger: " + decode(tag.tagger) + "\n") time_tuple = time.gmtime(tag.tag_time + tag.tag_timezone) time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple) timezone_str = format_timezone(tag.tag_timezone).decode('ascii') outstream.write("Date: " + time_str + " " + timezone_str + "\n") outstream.write("\n") outstream.write(decode(tag.message) + "\n") outstream.write("\n")
def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message): """Generate a single reflog line. :param old_sha: Old Commit SHA :param new_sha: New Commit SHA :param committer: Committer name and e-mail :param timestamp: Timestamp :param timezone: Timezone :param message: Message """ if old_sha is None: old_sha = ZERO_SHA return (old_sha + b' ' + new_sha + b' ' + committer + b' ' + str(int(timestamp)).encode('ascii') + b' ' + format_timezone(timezone) + b'\t' + message)
def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message): """Generate a single reflog line. Args: old_sha: Old Commit SHA new_sha: New Commit SHA committer: Committer name and e-mail timestamp: Timestamp timezone: Timezone message: Message """ if old_sha is None: old_sha = ZERO_SHA return (old_sha + b" " + new_sha + b" " + committer + b" " + str(int(timestamp)).encode("ascii") + b" " + format_timezone(timezone) + b"\t" + message)
def get_author(self, ctx): # hg authors might not have emails author = ctx.user() # check for git author pattern compliance regex = re.compile('^(.*?) \<(.*?)\>(.*)$') a = regex.match(author) if a: name = a.group(1) email = a.group(2) if len(a.group(3)) > 0: name += ' ext:(' + urllib.quote(a.group(3)) + ')' author = name + ' <' + email + '>' else: author = author + ' <none@none>' (time, timezone) = ctx.date() return author + ' ' + str(int(time)) + ' ' + format_timezone(-timezone)
def print_commit(commit, decode, outstream=sys.stdout): """Write a human-readable commit log entry. :param commit: A `Commit` object :param outstream: A stream file to write to """ outstream.write("-" * 50 + "\n") outstream.write("commit: " + commit.id.decode('ascii') + "\n") if len(commit.parents) > 1: outstream.write("merge: " + "...".join([c.decode('ascii') for c in commit.parents[1:]]) + "\n") outstream.write("Author: " + decode(commit.author) + "\n") if commit.author != commit.committer: outstream.write("Committer: " + decode(commit.committer) + "\n") time_tuple = time.gmtime(commit.author_time + commit.author_timezone) time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple) timezone_str = format_timezone(commit.author_timezone).decode('ascii') outstream.write("Date: " + time_str + " " + timezone_str + "\n") outstream.write("\n") outstream.write(decode(commit.message) + "\n") outstream.write("\n")
def print_commit(commit, decode, outstream=sys.stdout): """Write a human-readable commit log entry. :param commit: A `Commit` object :param outstream: A stream file to write to """ outstream.write("-" * 50 + "\n") outstream.write("commit: " + commit.id.decode('ascii') + "\n") if len(commit.parents) > 1: outstream.write( "merge: " + "...".join([c.decode('ascii') for c in commit.parents[1:]]) + "\n") outstream.write("Author: " + decode(commit.author) + "\n") if commit.author != commit.committer: outstream.write("Committer: " + decode(commit.committer) + "\n") time_tuple = time.gmtime(commit.author_time + commit.author_timezone) time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple) timezone_str = format_timezone(commit.author_timezone).decode('ascii') outstream.write("Date: " + time_str + " " + timezone_str + "\n") outstream.write("\n") outstream.write(decode(commit.message) + "\n") outstream.write("\n")
def test_substitute_keywords(project_repo): test_text = """\ First line version: date: author: Last line""" commit_ref = project_repo.head() short_ref = commit_ref[:7].decode(encoding='ascii') commit = project_repo.get_object(commit_ref) commit_time = strftime('%Y-%m-%d %H:%M:%S', gmtime(commit.commit_time)) timezone = format_timezone(commit.commit_timezone).decode(encoding='ascii') commit_timestamp = commit_time + ' ' + timezone author = commit.author.decode(encoding='ascii') result = git.substitute_keywords(test_text, project_repo, 'master') expected_result = """\ First line version: %s date: %s author: %s Last line""" % (short_ref, commit_timestamp, author) assert result == expected_result result = git.substitute_keywords(test_text, project_repo, 'test-tag') expected_result = """\ First line version: %s date: %s author: %s Last line""" % (short_ref, commit_timestamp, author) assert result == expected_result result = git.substitute_keywords(test_text, project_repo, 'test-garbage') expected_result = '' assert result == expected_result
def test_generate_timezone_utc_negative(self): self.assertEquals("-0000", format_timezone(0, True))
def export_hg_commit(self, rev): # return if we've already processed this node = self.repo.changelog.lookup(rev) phgsha = hex(node) pgit_sha = self.map_git_get(phgsha) if pgit_sha: return pgit_sha, True self.ui.status(_("converting revision %s\n") % str(rev)) # make sure parents are converted first parents = self.repo.parents(rev) for parent in parents: p_rev = parent.rev() hgsha = hex(parent.node()) git_sha = self.map_git_get(hgsha) if not p_rev == -1: if not git_sha: self.export_hg_commit(p_rev) ctx = self.repo.changectx(rev) tree_sha, renames = self.write_git_tree(ctx) commit = {} commit['tree'] = tree_sha (time, timezone) = ctx.date() # hg authors might not have emails author = ctx.user() if not '>' in author: # TODO : this kills losslessness - die (submodules)? author = author + ' <none@none>' commit['author'] = author + ' ' + str(int(time)) + ' ' + format_timezone(-timezone) message = ctx.description() commit['message'] = ctx.description() + "\n" extra = ctx.extra() if 'committer' in extra: # fixup timezone (name_timestamp, timezone) = extra['committer'].rsplit(' ', 1) timezone = format_timezone(-int(timezone)) commit['committer'] = '%s %s' % (name_timestamp, timezone) if 'encoding' in extra: commit['encoding'] = extra['encoding'] # HG EXTRA INFORMATION add_extras = False extra_message = '' if not ctx.branch() == 'default': add_extras = True extra_message += "branch : " + ctx.branch() + "\n" if renames: add_extras = True for oldfile, newfile in renames: extra_message += "rename : " + oldfile + " => " + newfile + "\n" if add_extras: commit['message'] += "\n--HG--\n" + extra_message commit['parents'] = [] for parent in parents: hgsha = hex(parent.node()) git_sha = self.map_git_get(hgsha) if git_sha: commit['parents'].append(git_sha) commit_sha = self.git.write_commit_hash(commit) # writing new blobs to git self.map_set(commit_sha, phgsha) return commit_sha, False
def test_format_timezone_double_negative(self): self.assertEqual("--700", format_timezone(int(((7 * 60)) * 60), True))
def test_format_timezone_pdt(self): self.assertEqual(b'-0400', format_timezone(-4 * 60 * 60))
def test_format_timezone_pdt_half(self): self.assertEquals("-0440", format_timezone(int(((-4 * 60) - 40) * 60)))
def test_format_timezone_cet(self): self.assertEquals("+0100", format_timezone(60 * 60))
def test_generate_timezone_utc_negative(self): self.assertEqual("-0000", format_timezone(0, True))
def test_generate_timezone_utc(self): self.assertEqual("+0000", format_timezone(0))
def export_hg_commit(self, rev): def is_octopus_part(ctx): return ctx.extra().get('hg-git', None) in set(['octopus', 'octopus-done']) # return if we've already processed this node = self.repo.changelog.lookup(rev) phgsha = hex(node) pgit_sha = self.map_git_get(phgsha) if pgit_sha: return pgit_sha, True self.ui.status(_("converting revision %s\n") % str(rev)) # make sure parents are converted first ctx = self.repo.changectx(rev) extra = ctx.extra() parents = [] if extra.get('hg-git', None) == 'octopus-done': # implode octopus parents part = ctx while is_octopus_part(part): (p1, p2) = part.parents() assert not is_octopus_part(p1) parents.append(p1) part = p2 parents.append(p2) else: parents = ctx.parents() for parent in parents: p_rev = parent.rev() hgsha = hex(parent.node()) git_sha = self.map_git_get(hgsha) if not p_rev == -1: if not git_sha: self.export_hg_commit(p_rev) tree_sha, renames = self.write_git_tree(ctx) commit = {} commit['tree'] = tree_sha (time, timezone) = ctx.date() # hg authors might not have emails author = ctx.user() if not '>' in author: author = author + ' <none@none>' commit['author'] = author + ' ' + str(int(time)) + ' ' + format_timezone(-timezone) message = ctx.description() commit['message'] = ctx.description() + "\n" if 'committer' in extra: # fixup timezone (name_timestamp, timezone) = extra['committer'].rsplit(' ', 1) try: timezone = format_timezone(-int(timezone)) commit['committer'] = '%s %s' % (name_timestamp, timezone) except ValueError: self.ui.warn(_("Ignoring committer in extra, invalid timezone in r%s: '%s'.\n") % (rev, timezone)) if 'encoding' in extra: commit['encoding'] = extra['encoding'] # HG EXTRA INFORMATION add_extras = False extra_message = '' if not ctx.branch() == 'default': add_extras = True extra_message += "branch : " + ctx.branch() + "\n" if renames: add_extras = True for oldfile, newfile in renames: extra_message += "rename : " + oldfile + " => " + newfile + "\n" for key, value in extra.iteritems(): if key in ['committer', 'encoding', 'branch', 'hg-git', 'git']: continue else: add_extras = True extra_message += "extra : " + key + " : " + urllib.quote(value) + "\n" if add_extras: commit['message'] += "\n--HG--\n" + extra_message commit['parents'] = [] for parent in parents: hgsha = hex(parent.node()) git_sha = self.map_git_get(hgsha) if git_sha: commit['parents'].append(git_sha) commit_sha = self.git.write_commit_hash(commit) # writing new blobs to git self.map_set(commit_sha, phgsha) return commit_sha, False
def test_generate_timezone_utc(self): self.assertEqual(b'+0000', format_timezone(0))
def test_format_timezone_double_negative(self): self.assertEqual(b'--700', format_timezone(int(((7 * 60)) * 60), True))
def test_generate_timezone_utc(self): self.assertEquals("+0000", format_timezone(0))
def test_format_timezone_cet(self): self.assertEqual("+0100", format_timezone(60 * 60))
def test_format_timezone_pdt(self): self.assertEquals("-0400", format_timezone(-4 * 60 * 60))
def test_format_timezone_pdt(self): self.assertEqual("-0400", format_timezone(-4 * 60 * 60))
def test_format_timezone_pdt_half(self): self.assertEqual("-0440", format_timezone(int(((-4 * 60) - 40) * 60)))
def test_format_timezone_cet(self): self.assertEqual(b'+0100', format_timezone(60 * 60))