Fix viewing tags, rename 'commit_id' to 'rev'(ision)
Jonas Haag
9 years ago
46 | 46 | def setup_routes(self): |
47 | 47 | for endpoint, rule in [ |
48 | 48 | ('repo_list', '/'), |
49 | ('blob', '/<repo>/blob/<commit_id>/'), | |
50 | ('blob', '/<repo>/blob/<commit_id>/<path:path>'), | |
51 | ('raw', '/<repo>/raw/<commit_id>/'), | |
52 | ('raw', '/<repo>/raw/<commit_id>/<path:path>'), | |
53 | ('commit', '/<repo>/commit/<commit_id>/'), | |
49 | ('blob', '/<repo>/blob/<rev>/'), | |
50 | ('blob', '/<repo>/blob/<rev>/<path:path>'), | |
51 | ('raw', '/<repo>/raw/<rev>/'), | |
52 | ('raw', '/<repo>/raw/<rev>/<path:path>'), | |
53 | ('commit', '/<repo>/commit/<rev>/'), | |
54 | 54 | ('history', '/<repo>/'), |
55 | ('history', '/<repo>/tree/<commit_id>/'), | |
56 | ('history', '/<repo>/tree/<commit_id>/<path:path>'), | |
55 | ('history', '/<repo>/tree/<rev>/'), | |
56 | ('history', '/<repo>/tree/<rev>/<path:path>'), | |
57 | 57 | ]: |
58 | 58 | self.add_url_rule(rule, view_func=getattr(views, endpoint)) |
59 | 59 |
20 | 20 | return refs[0].commit_time |
21 | 21 | return None |
22 | 22 | |
23 | def get_ref_or_commit(self, name_or_sha1): | |
24 | for prefix in ['', 'refs/heads/', 'refs/tags/']: | |
23 | def get_commit(self, rev): | |
24 | for prefix in ['refs/heads/', 'refs/tags/', '']: | |
25 | key = prefix + rev | |
25 | 26 | try: |
26 | return self[prefix+name_or_sha1] | |
27 | # XXX: Workaround https://github.com/jelmer/dulwich/issues/82 | |
28 | if not key.isalnum(): | |
29 | key = self.refs[key] | |
30 | obj = self.object_store[key] | |
31 | if isinstance(obj, dulwich.objects.Tag): | |
32 | obj = self[obj.object[1]] | |
33 | return obj | |
27 | 34 | except KeyError: |
28 | 35 | pass |
29 | ||
30 | raise KeyError(name_or_sha1) | |
36 | raise KeyError(rev) | |
31 | 37 | |
32 | 38 | def get_default_branch(self): |
33 | 39 | """ |
35 | 41 | """ |
36 | 42 | for candidate in ['master', 'trunk', 'default', 'gh-pages']: |
37 | 43 | try: |
38 | self.get_ref_or_commit(candidate) | |
44 | self.get_commit(candidate) | |
39 | 45 | return candidate |
40 | 46 | except KeyError: |
41 | 47 | pass |
0 | 0 | {% extends 'skeleton.html' %} |
1 | 1 | |
2 | 2 | {% block title %} |
3 | {{ repo.name }} ({{ commit_id|shorten_sha1 }}) | |
3 | {{ repo.name }} ({{ rev|shorten_sha1 }}) | |
4 | 4 | {% endblock %} |
5 | 5 | |
6 | 6 | {% block breadcrumbs %} |
7 | 7 | <span> |
8 | 8 | <a href="{{ url_for('history', repo=repo.name) }}">{{ repo.name }}</a> |
9 | 9 | <span class=slash>/</span> |
10 | <a href="{{ url_for('history', repo=repo.name, commit_id=commit_id) }}">{{ commit_id|shorten_sha1 }}</a> | |
10 | <a href="{{ url_for('history', repo=repo.name, rev=rev) }}">{{ rev|shorten_sha1 }}</a> | |
11 | 11 | </span> |
12 | 12 | |
13 | 13 | {% if subpaths %} |
16 | 16 | {% if loop.last %} |
17 | 17 | <a href="">{{ name|force_unicode }}</a> |
18 | 18 | {% else %} |
19 | <a href="{{ url_for('history', repo=repo.name, commit_id=commit_id, path=subpath) }}">{{ name|force_unicode }}</a> | |
19 | <a href="{{ url_for('history', repo=repo.name, rev=rev, path=subpath) }}">{{ name|force_unicode }}</a> | |
20 | 20 | <span class=slash>/</span> |
21 | 21 | {% endif %} |
22 | 22 | {% endfor %} |
26 | 26 | |
27 | 27 | {% block extra_header %} |
28 | 28 | <div class=branch-selector> |
29 | <span>{{ commit_id|shorten_sha1 }}</span> | |
29 | <span>{{ rev|shorten_sha1 }}</span> | |
30 | 30 | <div> |
31 | 31 | <ul class=branches> |
32 | 32 | {% for branch in branches %} |
33 | <li><a href="{{ url_for(view, repo=repo.name, commit_id=branch, path=path) }}">{{ branch }}</a></li> | |
33 | <li><a href="{{ url_for(view, repo=repo.name, rev=branch, path=path) }}">{{ branch }}</a></li> | |
34 | 34 | {% endfor %} |
35 | 35 | </ul> |
36 | 36 | {% if tags %} |
37 | 37 | <ul class=tags> |
38 | 38 | {% for tag in tags %} |
39 | <li><a href="{{ url_for(view, repo=repo.name, commit_id=tag, path=path) }}">{{ tag }}</a></li> | |
39 | <li><a href="{{ url_for(view, repo=repo.name, rev=tag, path=path) }}">{{ tag }}</a></li> | |
40 | 40 | {% endfor %} |
41 | 41 | </ul> |
42 | 42 | {% endif %} |
44 | 44 | Commit History |
45 | 45 | {% endif %} |
46 | 46 | <span> |
47 | @<a href="{{ url_for(view, repo=repo.name, commit_id=commit_id) }}">{{ commit_id }}</a> | |
47 | @<a href="{{ url_for(view, repo=repo.name, rev=rev) }}">{{ rev }}</a> | |
48 | 48 | </span> |
49 | 49 | {% if USE_SMARTHTTP %} |
50 | 50 | <code>git clone {{ url_for('history', repo=repo.name, _external=True) }}</code> |
56 | 56 | <ul> |
57 | 57 | {% for commit in history %} |
58 | 58 | <li> |
59 | <a class=commit href="{{ url_for('commit', repo=repo.name, commit_id=commit.id) }}"> | |
59 | <a class=commit href="{{ url_for('commit', repo=repo.name, rev=commit.id) }}"> | |
60 | 60 | <span class=line1> |
61 | 61 | <span>{{ commit.message|force_unicode|shorten_message }}</span> |
62 | 62 | </span> |
0 | 0 | <div class=tree> |
1 | <h2>Tree @<a href="{{ url_for('commit', repo=repo.name, commit_id=commit_id) }}">{{ commit_id|shorten_sha1 }}</a></h2> | |
1 | <h2>Tree @<a href="{{ url_for('commit', repo=repo.name, rev=rev) }}">{{ rev|shorten_sha1 }}</a></h2> | |
2 | 2 | <ul> |
3 | 3 | {% for _, name, fullpath in root_tree.dirs %} |
4 | <li><a href="{{ url_for('history', repo=repo.name, commit_id=commit_id, path=fullpath) }}" class=dir>{{ name|force_unicode }}</a></li> | |
4 | <li><a href="{{ url_for('history', repo=repo.name, rev=rev, path=fullpath) }}" class=dir>{{ name|force_unicode }}</a></li> | |
5 | 5 | {% endfor %} |
6 | 6 | {% for _, name, fullpath in root_tree.files %} |
7 | <li><a href="{{ url_for('blob', repo=repo.name, commit_id=commit_id, path=fullpath) }}">{{ name|force_unicode }}</a></li> | |
7 | <li><a href="{{ url_for('blob', repo=repo.name, rev=rev, path=fullpath) }}">{{ name|force_unicode }}</a></li> | |
8 | 8 | {% endfor %} |
9 | 9 | </ul> |
10 | 10 | </div> |
7 | 7 | |
8 | 8 | {% include 'tree.inc.html' %} |
9 | 9 | |
10 | {% set raw_url = url_for('raw', repo=repo.name, commit_id=commit_id, path=path) %} | |
10 | {% set raw_url = url_for('raw', repo=repo.name, rev=rev, path=path) %} | |
11 | 11 | |
12 | 12 | <div class=blobview> |
13 | 13 | <h2> |
14 | 14 | {{ filename|force_unicode }} |
15 | 15 | <span> |
16 | @<a href="{{ url_for('commit', repo=repo.name, commit_id=commit_id) }}">{{ commit_id|shorten_sha1 }}</a> | |
16 | @<a href="{{ url_for('commit', repo=repo.name, rev=rev) }}">{{ rev|shorten_sha1 }}</a> | |
17 | 17 | — |
18 | 18 | {% if is_markup %} |
19 | 19 | {% if render_markup %} |
24 | 24 | · |
25 | 25 | {% endif %} |
26 | 26 | <a href="{{ raw_url }}">raw</a> |
27 | · <a href="{{ url_for('history', repo=repo.name, commit_id=commit_id, path=path) }}">history</a> | |
27 | · <a href="{{ url_for('history', repo=repo.name, rev=rev, path=path) }}">history</a> | |
28 | 28 | </span> |
29 | 29 | </h2> |
30 | 30 | {% if is_binary %} |
2 | 2 | {% block extra_header %}{% endblock %} {# no branch selector on commits #} |
3 | 3 | |
4 | 4 | {% block title %} |
5 | Commit {{ commit_id }} - {{ repo.name }} | |
5 | Commit {{ rev }} - {{ repo.name }} | |
6 | 6 | {% endblock %} |
7 | 7 | |
8 | 8 | {% block content %} |
37 | 37 | {% if file.new_filename == '/dev/null' %} |
38 | 38 | <del>{{ file.old_filename|force_unicode }}</del> |
39 | 39 | {% else %} |
40 | <a href="{{ url_for('blob', repo=repo.name, commit_id=commit_id, path=file.new_filename) }}"> | |
40 | <a href="{{ url_for('blob', repo=repo.name, rev=rev, path=file.new_filename) }}"> | |
41 | 41 | {{ file.new_filename|force_unicode }} |
42 | 42 | </a> |
43 | 43 | {% endif %} |
7 | 7 | from werkzeug.exceptions import NotFound |
8 | 8 | |
9 | 9 | from dulwich.objects import Blob |
10 | from dulwich.diff_tree import RenameDetector | |
11 | 10 | |
12 | 11 | from klaus import markup |
13 | 12 | from klaus.utils import parent_directory, subpaths, get_mimetype_and_encoding, \ |
30 | 29 | """ |
31 | 30 | Base for all views with a repo context. |
32 | 31 | |
33 | The arguments `repo`, `commit_id`, `path` (see `dispatch_request`) define | |
34 | the repository, branch/commit and directory/file context, respectively -- | |
32 | The arguments `repo`, `rev`, `path` (see `dispatch_request`) define the | |
33 | repository, branch/commit and directory/file context, respectively -- | |
35 | 34 | that is, they specify what (and in what state) is being displayed in all the |
36 | 35 | derived views. |
37 | 36 | |
38 | 37 | For example: The 'history' view is the `git log` equivalent, i.e. if `path` |
39 | 38 | is "/foo/bar", only commits related to "/foo/bar" are displayed, and if |
40 | `commit_id` is "master", the history of the "master" branch is displayed. | |
39 | `rev` is "master", the history of the "master" branch is displayed. | |
41 | 40 | """ |
42 | 41 | def __init__(self, view_name, template_name=None): |
43 | 42 | self.view_name = view_name |
44 | 43 | self.template_name = template_name |
45 | 44 | self.context = {} |
46 | 45 | |
47 | def dispatch_request(self, repo, commit_id=None, path=''): | |
48 | self.make_context(repo, commit_id, path.strip('/')) | |
46 | def dispatch_request(self, repo, rev=None, path=''): | |
47 | self.make_context(repo, rev, path.strip('/')) | |
49 | 48 | return self.get_response() |
50 | 49 | |
51 | 50 | def get_response(self): |
52 | 51 | return render_template(self.template_name, **self.context) |
53 | 52 | |
54 | def make_context(self, repo, commit_id, path): | |
53 | def make_context(self, repo, rev, path): | |
55 | 54 | try: |
56 | 55 | repo = current_app.repo_map[repo] |
57 | if commit_id is None: | |
58 | commit_id = repo.get_default_branch() | |
59 | commit = repo.get_ref_or_commit(commit_id) | |
56 | if rev is None: | |
57 | rev = repo.get_default_branch() | |
58 | commit = repo.get_commit(rev) | |
60 | 59 | except KeyError: |
61 | 60 | raise NotFound("Commit not found") |
62 | 61 | |
68 | 67 | self.context = { |
69 | 68 | 'view': self.view_name, |
70 | 69 | 'repo': repo, |
71 | 'commit_id': commit_id, | |
70 | 'rev': rev, | |
72 | 71 | 'commit': commit, |
73 | 'branches': repo.get_branch_names(exclude=commit_id), | |
72 | 'branches': repo.get_branch_names(exclude=rev), | |
74 | 73 | 'tags': repo.get_tag_names(), |
75 | 74 | 'path': path, |
76 | 75 | 'blob_or_tree': blob_or_tree, |
144 | 143 | skip = 0 |
145 | 144 | |
146 | 145 | history = self.context['repo'].history( |
147 | self.context['commit_id'], | |
146 | self.context['rev'], | |
148 | 147 | self.context['path'], |
149 | 148 | history_length + 1, |
150 | 149 | skip |