mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
using python dom implementation for hglog
This commit is contained in:
@@ -29,7 +29,8 @@
|
|||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
# import basic packages
|
|
||||||
|
# import basic modules
|
||||||
import sys, os
|
import sys, os
|
||||||
|
|
||||||
# create python path
|
# create python path
|
||||||
@@ -40,139 +41,148 @@ if len(pythonPath) > 0:
|
|||||||
for i in range(len(pathParts)):
|
for i in range(len(pathParts)):
|
||||||
sys.path.insert(i, pathParts[i])
|
sys.path.insert(i, pathParts[i])
|
||||||
|
|
||||||
# import mercurial packages
|
# import mercurial modules
|
||||||
from mercurial import hg, ui, commands
|
from mercurial import hg, ui, commands
|
||||||
from mercurial.node import hex
|
from mercurial.node import hex
|
||||||
from xml.sax.saxutils import escape
|
from xml.dom.minidom import Document
|
||||||
import datetime, time
|
|
||||||
|
|
||||||
# header
|
# util methods
|
||||||
def printHeader(total):
|
def openRepository():
|
||||||
print '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
repositoryPath = os.environ['SCM_REPOSITORY_PATH']
|
||||||
print '<changeset-paging>'
|
return hg.repository(ui.ui(), path = repositoryPath)
|
||||||
print ' <total>' + str(total) + '</total>'
|
|
||||||
print ' <changesets>'
|
|
||||||
|
|
||||||
# changeset
|
def writeXml(doc):
|
||||||
def printChangeset(repo, ctx):
|
# print doc.toprettyxml(indent=" ")
|
||||||
|
print doc.toxml()
|
||||||
|
|
||||||
|
def createChildNode(doc, parentNode, name):
|
||||||
|
node = doc.createElement(name)
|
||||||
|
parentNode.appendChild(node)
|
||||||
|
return node
|
||||||
|
|
||||||
|
def appendValue(doc, node, value):
|
||||||
|
textNode = doc.createTextNode(value)
|
||||||
|
node.appendChild(textNode)
|
||||||
|
|
||||||
|
def appendTextNode(doc, parentNode, name, value):
|
||||||
|
node = createChildNode(doc, parentNode, name)
|
||||||
|
appendValue(doc, node, value)
|
||||||
|
|
||||||
|
def appendListNodes(doc, parentNode, name, values):
|
||||||
|
if values:
|
||||||
|
for value in values:
|
||||||
|
appendTextNode(doc, parentNode, name, value)
|
||||||
|
|
||||||
|
def appendWrappedListNodes(doc, parentNode, wrapperName, name, values):
|
||||||
|
if values:
|
||||||
|
wrapperNode = createChildNode(doc, parentNode, wrapperName)
|
||||||
|
appendListNodes(doc, wrapperNode, name, values)
|
||||||
|
|
||||||
|
# changeset methods
|
||||||
|
|
||||||
|
def getId(ctx):
|
||||||
|
return str(ctx.rev()) + ':' + hex(ctx.node()[:6])
|
||||||
|
|
||||||
|
def appendIdNode(doc, parentNode, ctx):
|
||||||
|
id = getId(ctx)
|
||||||
|
appendTextNode(doc, parentNode, 'id', id)
|
||||||
|
|
||||||
|
def appendParentNodes(doc, parentNode, ctx):
|
||||||
|
parents = ctx.parents()
|
||||||
|
if parents:
|
||||||
|
for parent in parents:
|
||||||
|
parentId = getId(parent)
|
||||||
|
appendTextNode(doc, parentNode, 'parents', parentId)
|
||||||
|
|
||||||
|
def appendDateNode(doc, parentNode, ctx):
|
||||||
time = int(ctx.date()[0]) * 1000
|
time = int(ctx.date()[0]) * 1000
|
||||||
branch = ctx.branch()
|
date = str(time).split('.')[0]
|
||||||
tags = ctx.tags()
|
appendTextNode(doc, parentNode, 'date', date)
|
||||||
status = repo.status(ctx.p1().node(), ctx.node())
|
|
||||||
mods = status[0]
|
def appendAuthorNodes(doc, parentNode, ctx):
|
||||||
added = status[1]
|
|
||||||
deleted = status[2]
|
|
||||||
authorName = ctx.user()
|
authorName = ctx.user()
|
||||||
authorMail = None
|
authorMail = None
|
||||||
parents = ctx.parents()
|
|
||||||
|
|
||||||
if authorName:
|
if authorName:
|
||||||
|
authorNode = createChildNode(doc, parentNode, 'author')
|
||||||
s = authorName.find('<')
|
s = authorName.find('<')
|
||||||
e = authorName.find('>')
|
e = authorName.find('>')
|
||||||
if s > 0 and e > 0:
|
if s > 0 and e > 0:
|
||||||
authorMail = authorName[s + 1:e].strip()
|
authorMail = authorName[s + 1:e].strip()
|
||||||
authorName = authorName[0:s].strip()
|
authorName = authorName[0:s].strip()
|
||||||
|
appendTextNode(doc, authorNode, 'mail', authorMail)
|
||||||
print ' <changeset>'
|
|
||||||
print ' <id>' + str(ctx.rev()) + ':' + hex(ctx.node()[:6]) + '</id>'
|
appendTextNode(doc, authorNode, 'name', authorName)
|
||||||
if parents:
|
|
||||||
for parent in parents:
|
def appendBranchesNode(doc, parentNode, ctx):
|
||||||
print ' <parents>' + str(parent.rev()) + ':' + hex(parent.node()[:6]) + '</parents>'
|
branch = ctx.branch()
|
||||||
print ' <author>' + escape(ctx.user()) + '</author>'
|
|
||||||
print ' <description>' + escape(ctx.description().encode('UTF-8')) + '</description>'
|
|
||||||
print ' <date>' + str(time).split('.')[0] + '</date>'
|
|
||||||
|
|
||||||
# author
|
|
||||||
if authorName:
|
|
||||||
print ' <author>'
|
|
||||||
print ' <name>' + authorName + '</name>'
|
|
||||||
if authorMail:
|
|
||||||
print ' <mail>' + authorMail + '</mail>'
|
|
||||||
print ' </author>'
|
|
||||||
|
|
||||||
# branches
|
|
||||||
if branch != 'default':
|
if branch != 'default':
|
||||||
print ' <branches>' + branch + '</branches>'
|
appendTextNode(doc, parentNode, 'branches', branch)
|
||||||
|
|
||||||
# tags
|
|
||||||
if tags:
|
|
||||||
for t in tags:
|
|
||||||
print ' <tags>' + t + '</tags>'
|
|
||||||
|
|
||||||
# modifications
|
def appendModifications(doc, parentNode, ctx):
|
||||||
print ' <modifications>'
|
status = repo.status(ctx.p1().node(), ctx.node())
|
||||||
|
if status:
|
||||||
|
modificationsNode = createChildNode(doc, parentNode, 'modifications')
|
||||||
|
appendWrappedListNodes(doc, modificationsNode, 'added', 'file', status[1])
|
||||||
|
appendWrappedListNodes(doc, modificationsNode, 'modified', 'file', status[0])
|
||||||
|
appendWrappedListNodes(doc, modificationsNode, 'removed', 'file', status[2])
|
||||||
|
|
||||||
# files added
|
def appendChangesetNode(doc, parentNode, ctx):
|
||||||
if added:
|
changesetNode = createChildNode(doc, parentNode, 'changeset')
|
||||||
print ' <added>'
|
appendIdNode(doc, changesetNode, ctx)
|
||||||
for add in added:
|
appendParentNodes(doc, changesetNode, ctx)
|
||||||
print ' <file>' + add + '</file>'
|
appendTextNode(doc, changesetNode, 'description', ctx.description())
|
||||||
print ' </added>'
|
appendDateNode(doc, changesetNode, ctx)
|
||||||
|
appendAuthorNodes(doc, changesetNode, ctx)
|
||||||
|
appendBranchesNode(doc, changesetNode, ctx)
|
||||||
|
appendListNodes(doc, changesetNode, 'tags', ctx.tags())
|
||||||
|
appendModifications(doc, changesetNode, ctx)
|
||||||
|
|
||||||
|
# changeset methods end
|
||||||
|
|
||||||
# files modified
|
# change log methods
|
||||||
if mods:
|
|
||||||
print ' <modified>'
|
|
||||||
for mod in mods:
|
|
||||||
print ' <file>' + mod + '</file>'
|
|
||||||
print ' </modified>'
|
|
||||||
|
|
||||||
# files deleted
|
def createBasicNodes(doc, ctxs):
|
||||||
if deleted:
|
rootNode = doc.createElement('changeset-paging')
|
||||||
print ' <removed>'
|
doc.appendChild(rootNode)
|
||||||
for dele in deleted:
|
total = str(len(repo))
|
||||||
print ' <file>' + dele + '</file>'
|
appendTextNode(doc, rootNode, 'total', total)
|
||||||
print ' </removed>'
|
return createChildNode(doc, rootNode, 'changesets')
|
||||||
|
|
||||||
print ' </modifications>'
|
def appendChangesetsForPath(doc, repo, rev, path):
|
||||||
print ' </changeset>'
|
|
||||||
|
|
||||||
# footer
|
|
||||||
def printFooter():
|
|
||||||
print ' </changesets>'
|
|
||||||
print '</changeset-paging>'
|
|
||||||
|
|
||||||
def printChangesetsForPath(repo, rev, path):
|
|
||||||
if len(rev) <= 0:
|
if len(rev) <= 0:
|
||||||
rev = "tip"
|
rev = "tip"
|
||||||
|
|
||||||
fctxs = repo[rev].filectx(path)
|
fctxs = repo[rev].filectx(path)
|
||||||
maxRev = fctxs.rev()
|
maxRev = fctxs.rev()
|
||||||
|
|
||||||
revs = []
|
revs = []
|
||||||
for i in fctxs.filelog():
|
for i in fctxs.filelog():
|
||||||
fctx = fctxs.filectx(i)
|
fctx = fctxs.filectx(i)
|
||||||
if fctx.rev() <= maxRev:
|
if fctx.rev() <= maxRev:
|
||||||
revs.append(fctx.changectx())
|
revs.append(fctx.changectx())
|
||||||
|
|
||||||
# reverse changesets
|
# reverse changesets
|
||||||
revs.reverse()
|
revs.reverse()
|
||||||
|
|
||||||
total = len(revs)
|
|
||||||
|
|
||||||
# handle paging
|
# handle paging
|
||||||
start = os.environ['SCM_PAGE_START']
|
start = os.environ['SCM_PAGE_START']
|
||||||
limit = os.environ['SCM_PAGE_LIMIT']
|
limit = os.environ['SCM_PAGE_LIMIT']
|
||||||
|
|
||||||
if len(start) > 0:
|
if len(start) > 0:
|
||||||
revs = revs[int(start):]
|
revs = revs[int(start):]
|
||||||
|
|
||||||
if len(limit) > 0:
|
if len(limit) > 0:
|
||||||
revs = revs[:int(limit)]
|
revs = revs[:int(limit)]
|
||||||
|
|
||||||
# output
|
# output
|
||||||
printHeader(total)
|
changesets = createBasicNodes(doc, revs)
|
||||||
for ctx in revs:
|
for ctx in revs:
|
||||||
printChangeset(repo, ctx)
|
appendChangesetNode(doc, changesets, ctx)
|
||||||
printFooter()
|
|
||||||
|
|
||||||
def printChangesetsForStartAndEnd(repo, startRev, endRev):
|
def appendChangesetsForStartAndEnd(doc, repo, startRev, endRev):
|
||||||
printHeader(len(repo))
|
changesets = createBasicNodes(doc, repo)
|
||||||
for i in range(endRev, startRev, -1):
|
for i in range(endRev, startRev, -1):
|
||||||
printChangeset(repo, repo[i])
|
appendChangesetNode(doc, changesets, repo[i])
|
||||||
printFooter()
|
|
||||||
|
# change log methods
|
||||||
|
|
||||||
|
# main method
|
||||||
|
|
||||||
repositoryPath = os.environ['SCM_REPOSITORY_PATH']
|
repo = openRepository()
|
||||||
repo = hg.repository(ui.ui(), path = repositoryPath)
|
doc = Document()
|
||||||
|
|
||||||
path = os.environ['SCM_PATH']
|
path = os.environ['SCM_PATH']
|
||||||
startNode = os.environ['SCM_REVISION_START']
|
startNode = os.environ['SCM_REVISION_START']
|
||||||
@@ -180,11 +190,11 @@ endNode = os.environ['SCM_REVISION_END']
|
|||||||
rev = os.environ['SCM_REVISION']
|
rev = os.environ['SCM_REVISION']
|
||||||
|
|
||||||
if len(path) > 0:
|
if len(path) > 0:
|
||||||
printChangesetsForPath(repo, rev, path)
|
appendChangesetsForPath(doc, repo, rev, path)
|
||||||
elif len(rev) > 0:
|
elif len(rev) > 0:
|
||||||
ctx = repo[rev]
|
ctx = repo[rev]
|
||||||
print '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
changesets = createBasicNodes(doc, repo)
|
||||||
printChangeset(repo, ctx)
|
appendChangesetNode(doc, changesets, repo, ctx)
|
||||||
else:
|
else:
|
||||||
if len(startNode) > 0 and len(endNode) > 0:
|
if len(startNode) > 0 and len(endNode) > 0:
|
||||||
# start and end revision
|
# start and end revision
|
||||||
@@ -194,18 +204,15 @@ else:
|
|||||||
# paging
|
# paging
|
||||||
start = os.environ['SCM_PAGE_START']
|
start = os.environ['SCM_PAGE_START']
|
||||||
limit = os.environ['SCM_PAGE_LIMIT']
|
limit = os.environ['SCM_PAGE_LIMIT']
|
||||||
|
|
||||||
limit = int(limit)
|
limit = int(limit)
|
||||||
|
|
||||||
end = int(start)
|
end = int(start)
|
||||||
endRev = len(repo) - end - 1
|
endRev = len(repo) - end - 1
|
||||||
|
|
||||||
startRev = endRev - limit
|
startRev = endRev - limit
|
||||||
|
|
||||||
# fix negative start revisions
|
# fix negative start revisions
|
||||||
if startRev < -1:
|
if startRev < -1:
|
||||||
startRev = -1
|
startRev = -1
|
||||||
|
|
||||||
# print
|
# print
|
||||||
printChangesetsForStartAndEnd(repo, startRev, endRev)
|
appendChangesetsForStartAndEnd(doc, repo, startRev, endRev)
|
||||||
|
|
||||||
|
# write document
|
||||||
|
writeXml(doc)
|
||||||
|
|||||||
Reference in New Issue
Block a user