Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 91 lines (75 sloc) 3.523 kB
7b50e5a @tobami Add Mercurial support
authored
1 import os, datetime
2 from subprocess import Popen, PIPE
8c25971 @tobami Fix revision saving when the given commitid doesn't exist in the repo…
authored
3 import logging
4
4c0d421 @acdha Mercurial: moved repository path to settings.REPOSITORY_BASE_PATH
acdha authored
5 from django.conf import settings
7b50e5a @tobami Add Mercurial support
authored
6
6b213b6 @tobami Fix repo path
authored
7
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
8 def updaterepo(project, update=True):
9 repo_name = os.path.splitext(project.repo_path.split(os.sep)[-1])[0]
10 working_copy = os.path.join(settings.REPOSITORY_BASE_PATH, repo_name)
4c0d421 @acdha Mercurial: moved repository path to settings.REPOSITORY_BASE_PATH
acdha authored
11
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
12 if os.path.exists(working_copy):
13 if not update:
14 return
15
16 p = Popen(['hg', 'pull', '-u'], stdout=PIPE, stderr=PIPE,
17 cwd=working_copy)
7b50e5a @tobami Add Mercurial support
authored
18 stdout, stderr = p.communicate()
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
19
20 if p.returncode != 0 or stderr:
21 raise RuntimeError("hg pull returned %s: %s" % (p.returncode,
22 stderr))
7b50e5a @tobami Add Mercurial support
authored
23 else:
24 return [{'error': False}]
25 else:
311b544 @tobami Refactor repo update code
authored
26 # Clone repo
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
27 cmd = ['hg', 'clone', project.repo_path, repo_name]
28
29 p = Popen(cmd, stdout=PIPE, stderr=PIPE,
4c0d421 @acdha Mercurial: moved repository path to settings.REPOSITORY_BASE_PATH
acdha authored
30 cwd=settings.REPOSITORY_BASE_PATH)
6cefd8e @tobami Fix Version control backends (broken since branches merge)
authored
31 logging.debug('Cloning Mercurial repo {0}for project {1}'.format(
32 project.repo_path, project))
7b50e5a @tobami Add Mercurial support
authored
33 stdout, stderr = p.communicate()
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
34
35 if p.returncode != 0:
36 raise RuntimeError("%s returned %s: %s" % (" ".join(cmd),
37 p.returncode,
38 stderr))
7b50e5a @tobami Add Mercurial support
authored
39 else:
40 return [{'error': False}]
41
42 def getlogs(endrev, startrev):
6cefd8e @tobami Fix Version control backends (broken since branches merge)
authored
43 updaterepo(endrev.branch.project, update=False)
4c0d421 @acdha Mercurial: moved repository path to settings.REPOSITORY_BASE_PATH
acdha authored
44
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
45 # TODO: Move all of this onto the model so we can avoid needing to repeat it:
6cefd8e @tobami Fix Version control backends (broken since branches merge)
authored
46 repo_name = os.path.splitext(endrev.branch.project.repo_path.split(os.sep)[-1])[0]
0089d7d @acdha Git & Mercurial backends: continued refactoring
acdha authored
47 working_copy = os.path.join(settings.REPOSITORY_BASE_PATH, repo_name)
99dbc85 @acdha Whitespace cleanup
acdha authored
48
18cd6e7 @tobami Add author email and short_commit_id to mercurial plugin
authored
49 cmd = ["hg", "log",
50 "-r", "%s:%s" % (endrev.commitid, startrev.commitid),
51 "-b", "default",
52 "--template", "{rev}:{node|short}\n{node}\n{author|user}\n{author|email}\n{date}\n{desc}\n=newlog=\n"]
53
54 p = Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=working_copy)
7b50e5a @tobami Add Mercurial support
authored
55 stdout, stderr = p.communicate()
522685e @tobami Add unittest for json report creation
authored
56
8c25971 @tobami Fix revision saving when the given commitid doesn't exist in the repo…
authored
57 if p.returncode != 0:
58 raise RuntimeError(str(stderr))
7b50e5a @tobami Add Mercurial support
authored
59 else:
18cd6e7 @tobami Add author email and short_commit_id to mercurial plugin
authored
60 stdout = stdout.rstrip('\n')#Remove last newline
7b50e5a @tobami Add Mercurial support
authored
61 logs = []
62 for log in stdout.split("=newlog=\n"):
63 elements = []
64 elements = log.split('\n')[:-1]
18cd6e7 @tobami Add author email and short_commit_id to mercurial plugin
authored
65 if len(elements) < 6:
66 # "Malformed" log
67 logs.append(
68 {'date': '-', 'message': 'error parsing log', 'commitid': '-'})
7b50e5a @tobami Add Mercurial support
authored
69 else:
18cd6e7 @tobami Add author email and short_commit_id to mercurial plugin
authored
70 short_commit_id = elements.pop(0)
71 commit_id = elements.pop(0)
72 author_name = elements.pop(0)
73 author_email = elements.pop(0)
7b50e5a @tobami Add Mercurial support
authored
74 date = elements.pop(0)
0441c47 @tobami Don't show again last log message
authored
75 # All other newlines should belong to the description text. Join.
7b50e5a @tobami Add Mercurial support
authored
76 message = '\n'.join(elements)
99dbc85 @acdha Whitespace cleanup
acdha authored
77
7b50e5a @tobami Add Mercurial support
authored
78 # Parse date
79 date = date.split('-')[0]
80 date = datetime.datetime.fromtimestamp(float(date)).strftime("%Y-%m-%d %H:%M:%S")
99dbc85 @acdha Whitespace cleanup
acdha authored
81
7b50e5a @tobami Add Mercurial support
authored
82 # Add changeset info
11fa22a @tobami Limit horizontal plot height to avoid eating up all memory
authored
83 logs.append({
18cd6e7 @tobami Add author email and short_commit_id to mercurial plugin
authored
84 'date': date, 'author': author_name, 'author_email': author_email,
85 'message': message,'short_commit_id': short_commit_id,
86 'commitid': commit_id})
87 # Remove last log here because mercurial saves the short hast as commitid now
88 if len(logs) > 1 and logs[-1].get('short_commit_id') == startrev.commitid:
89 logs.pop()
7b50e5a @tobami Add Mercurial support
authored
90 return logs
Something went wrong with that request. Please try again.