Skip to content

Commit

Permalink
Introduce support to provide TOPIC without "hg topic"
Browse files Browse the repository at this point in the history
hg --pushvars TOPIC=MyTopicName
Special use case is TOPIC=on or TOPIC=off to enable/disable TOPIC
usage at all.

See #3 and fixes #10.
  • Loading branch information
misery committed May 29, 2024
1 parent 1c2eee1 commit 17242bb
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions contrib/mercurial_git_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@

HG = 'hg'
USE_TOPICS = True
TOPIC = None


def get_ticket_refs(text, prefixes=None):
Expand Down Expand Up @@ -336,7 +337,8 @@ def __init__(self, root, cmd):
class BaseReviewRequest(object):
"""A class to represent a review request from a Mercurial hook."""

def __init__(self, root, repo, changesets, base, submitter, differ, web):
def __init__(self, root, repo, changesets, base,
submitter, differ, web, topic):
"""Initialize object with the given information.
Args:
Expand All @@ -360,10 +362,14 @@ def __init__(self, root, repo, changesets, base, submitter, differ, web):
web (unicode, optional):
URL to web repository.
topic (unicode, optional):
The name of the topic.
"""
self.root = root
self.repo = repo
self.submitter = submitter
self._topic = topic
self._changesets = changesets
self._check_changesets()
self.base = base
Expand Down Expand Up @@ -417,7 +423,7 @@ def branch(self):

def summary(self):
if len(self._changesets) > 1:
return "Topic: " + self._changesets[0].topic()
return "Topic: " + self._topic
else:
return self._changesets[0].summary()

Expand Down Expand Up @@ -707,7 +713,7 @@ def _commit_id_data(self):
content = []

if len(self._changesets) > 1:
content.append(self._changesets[0].topic().encode('utf-8'))
content.append(self._topic.encode('utf-8'))
content.append(self.submitter.encode('utf-8'))
content.append(six.text_type(self.repo).encode('utf-8'))
else:
Expand Down Expand Up @@ -786,14 +792,16 @@ def _get_request(self):


class MercurialReviewRequest(BaseReviewRequest):
def __init__(self, root, repo, changeset, base, submitter, differ, web):
def __init__(self, root, repo, changeset, base,
submitter, differ, web, topic):
super(MercurialReviewRequest, self).__init__(root,
repo,
changeset,
base,
submitter,
differ,
web)
web,
topic)

def _commit_id_data(self):
content = super(MercurialReviewRequest, self)._commit_id_data()
Expand Down Expand Up @@ -881,14 +889,16 @@ def _generate_diff_info(self):


class GitReviewRequest(BaseReviewRequest):
def __init__(self, root, repo, changeset, base, submitter, differ, web):
def __init__(self, root, repo, changeset, base,
submitter, differ, web, topic):
super(GitReviewRequest, self).__init__(root,
repo,
changeset,
base,
submitter,
differ,
web)
web,
topic)

def _generate_diff_info(self):
"""Generate the diff if it has been changed."""
Expand Down Expand Up @@ -1341,14 +1351,15 @@ def _extract_changeset_topics(self, changesets):
nontopicchanges = []
currentTopic = None
for changeset in changesets:
if changeset.topic():
topic = changeset.topic() if TOPIC is None else TOPIC
if topic:
if currentTopic is None:
currentTopic = changeset.topic()
elif currentTopic != changeset.topic():
if changeset.topic() in topicchanges:
currentTopic = topic
elif currentTopic != topic:
if topic in topicchanges:
raise HookError('Topic is out of order: %s'
% currentTopic)
currentTopic = changeset.topic()
currentTopic = topic

if currentTopic in topicchanges:
topicchanges[currentTopic].append(changeset)
Expand Down Expand Up @@ -1377,18 +1388,23 @@ def _handle_changeset_list_process(self, node, changesets):
topic,
len(topicchanges[topic]))
self._handle_changeset_list_process_request(topicchanges[topic],
revreqs)
revreqs,
topic)

return self._handle_approved_review_requests(revreqs)

def _handle_changeset_list_process_request(self, changesets, revreqs):
def _handle_changeset_list_process_request(self,
changesets,
revreqs,
topic=None):
request = self.review_request_class(self.root,
self.repo_id,
changesets,
self.base,
self.submitter,
self._differ,
self.web)
self.web,
topic)

if self._check_duplicate(request, revreqs):
self.log('Ignoring changeset(s) (%s) as it has a '
Expand Down Expand Up @@ -1638,9 +1654,14 @@ def get_logging_level(logging):

def set_topic_usage():
global USE_TOPICS
TOPIC = 'HG_USERVAR_TOPIC'
if TOPIC in os.environ:
USE_TOPICS = os.environ[TOPIC].lower() in ('true', 'on')
global TOPIC

TOPICENV = 'HG_USERVAR_TOPIC'
if TOPICENV in os.environ:
TOPIC = os.environ[TOPICENV]
USE_TOPICS = TOPIC.lower() != 'off'
if TOPIC.lower() in ('on', 'off'):
TOPIC = None

USE_TOPICS = USE_TOPICS and rbversion >= '2'

Expand Down

0 comments on commit 17242bb

Please sign in to comment.