Merge lp:~robertminsk/bzr-keywords/more-keywords-updated into lp:bzr-keywords

Proposed by rminsk
Status: Work in progress
Proposed branch: lp:~robertminsk/bzr-keywords/more-keywords-updated
Merge into: lp:bzr-keywords
Diff against target: 181 lines (+89/-18)
2 files modified
__init__.py (+39/-18)
keywords.py (+50/-0)
To merge this branch: bzr merge lp:~robertminsk/bzr-keywords/more-keywords-updated
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) Needs Fixing
Review via email: mp+72268@code.launchpad.net

Description of the change

An updated version of more-keywords since the other branch has not been updated in over a year. This version has been updated to work with revision 18 of the trunk. It adds the following keywords:

Revision-Number - revision number that last changed the file.
Branch-Revision-Id - current unique id of the revision on the branch
Branch-Revision-Number - current revision number on the branch

Revid - an alias for Revision-Id
Revno - an alias for Revision-Number
Branch-Revid - an alias for Branch-Revision-Id
Branch-Revno - an alias for Branch-Revision-Number

All the real credit for this goes to user dmitrij.ledkov

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

This looks reasonable, except for the post_change_branch_tip hook:
 * It adds a significant overhead to all operations that change the branch (push, pull, commit, ...)
 * It assumes the tree uses inventories
 * It writes directly to the working tree without locking files

I'm also not sure that it's actually necessary to have this hook.

22. By Robert Minsk <email address hidden>

Removed the post_change_branch_tip hook on advice from Jelmer

Revision history for this message
rminsk (robertminsk) wrote :

> This looks reasonable, except for the post_change_branch_tip hook:
> * It adds a significant overhead to all operations that change the branch
> (push, pull, commit, ...)
> * It assumes the tree uses inventories
> * It writes directly to the working tree without locking files
>
> I'm also not sure that it's actually necessary to have this hook.

Removed post_change_branch_tip

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

> > This looks reasonable, except for the post_change_branch_tip hook:
> > * It adds a significant overhead to all operations that change the branch
> > (push, pull, commit, ...)
> > * It assumes the tree uses inventories
> > * It writes directly to the working tree without locking files
> >
> > I'm also not sure that it's actually necessary to have this hook.
>
> Removed post_change_branch_tip
Thanks.

It would be nice to have tests for this, though at this point I can live with just finally merging the MP and adding tests later.

Please don't use "except:". It eats exceptions, can make it hard to kill bzr, and hides bugs.

The code seems to assume that the branch that it works on lives in ".". This is often not the case - the current branch might be on some remote location, or a completely different local path. The current working directory should not matter. It really should just obtain the branch from the tree object.

Revision history for this message
rminsk (robertminsk) wrote :

Being new to the API I'm not quite sure how to get from, I guess, the working tree to the branch I am interested in. Can you tell be what method in the API I should be looking at?

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

Whoops, sorry, I missed your reply on this.

The tree should generally (if it's a working tree) have a "branch" attribute you can use. If that doesn't exist then your best bet is probably to use the same values for the branch revision id and branch revision number as the tree.

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

Marking this as needs fixing so it doesn't show up in the review queue.

review: Needs Fixing

Unmerged revisions

22. By Robert Minsk <email address hidden>

Removed the post_change_branch_tip hook on advice from Jelmer

21. By Robert Minsk <email address hidden>

Added aliases Revid, Revno, Branch-Revid, Branch-Revno

20. By Robert Minsk <email address hidden>

Added support for --revision command line option.

19. By Robert Minsk <email address hidden>

Porting of more-keywords to the latest branch

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '__init__.py'
2--- __init__.py 2011-03-02 12:31:59 +0000
3+++ __init__.py 2011-08-22 01:14:32 +0000
4@@ -54,20 +54,27 @@
5
6 The currently supported keywords are given below.
7
8- ============= =========================================================
9- Keyword Description
10- ============= =========================================================
11- Date the date and time the file was last modified
12- Committer the committer (name and email) of the last change
13- Authors the authors (names and emails) of the last change
14- Revision-Id the unique id of the revision that last changed the file
15- Path the relative path of the file in the tree
16- Filename just the name part of the relative path
17- Directory just the directory part of the relative path
18- File-Id the unique id assigned to this file
19- Now the current date and time
20- User the current user (name and email)
21- ============= =========================================================
22+ ====================== =====================================================
23+ Keyword Description
24+ ====================== =====================================================
25+ Date date and time the file was last modified
26+ Committer committer (name and email) of the last change
27+ Authors authors (names and emails) of the last change
28+ Revision-Id unique id of the revision that last changed the file
29+ Revid alias for Revision-Id
30+ Revision-Number revision number that last changed the file
31+ Revno alias for Revision-Number
32+ Path relative path of the file in the tree
33+ Filename just the name part of the relative path
34+ Directory just the directory part of the relative path
35+ File-Id unique id assigned to this file
36+ Now current date and time
37+ User current user (name and email)
38+ Branch-Revision-Id current unique id of the revision on the branch
39+ Branch-Revid alias for Branch-Revision-Id
40+ Branch-Revision-Number current revision number on the branch
41+ Branch-Revno alias for Branch-Revision-Number
42+ ====================== =====================================================
43
44 If you want finer control over the formatting of names and email
45 addresses, you can use the following keywords.
46@@ -179,18 +186,25 @@
47 converter=lambda s: s,
48 help='Keyword expansion style.')]
49
50- def run(self, *args, **kwargs):
51+ def run(self, filename, **kwargs):
52 """Process special options and delegate to superclass."""
53 if 'keywords' in kwargs:
54 from bzrlib.plugins.keywords.keywords import (
55 _keyword_style_registry,
56+ _current_branch_id,
57+ _get_branch
58 )
59 # Implicitly set the filters option
60 kwargs['filters'] = True
61 style = kwargs['keywords']
62 _keyword_style_registry.default_key = style
63 del kwargs['keywords']
64- return super(cmd_cat, self).run(*args, **kwargs)
65+ revid = None
66+ if 'revision' in kwargs:
67+ b = _get_branch(filename)
68+ revid = kwargs['revision'][0].as_revision_id(b)
69+ _current_branch_id(filename, revid)
70+ return super(cmd_cat, self).run(filename, **kwargs)
71
72 def help(self):
73 """Return help message including text from superclass."""
74@@ -209,18 +223,25 @@
75 converter=lambda s: s,
76 help='Keyword expansion style.')]
77
78- def run(self, *args, **kwargs):
79+ def run(self, dest, branch_or_subdir=None, **kwargs):
80 """Process special options and delegate to superclass."""
81 if 'keywords' in kwargs:
82 from bzrlib.plugins.keywords.keywords import (
83 _keyword_style_registry,
84+ _current_branch_id,
85+ _get_branch
86 )
87 # Implicitly set the filters option
88 kwargs['filters'] = True
89 style = kwargs['keywords']
90 _keyword_style_registry.default_key = style
91 del kwargs['keywords']
92- return super(cmd_export, self).run(*args, **kwargs)
93+ revid = None
94+ if 'revision' in kwargs:
95+ b = _get_branch(branch_or_subdir)
96+ revid = kwargs['revision'][0].as_revision_id(b)
97+ _current_branch_id(branch_or_subdir, revid)
98+ return super(cmd_export, self).run(dest, branch_or_subdir, **kwargs)
99
100 def help(self):
101 """Return help message including text from superclass."""
102
103=== modified file 'keywords.py'
104--- keywords.py 2011-03-02 12:31:59 +0000
105+++ keywords.py 2011-08-22 01:14:32 +0000
106@@ -16,7 +16,10 @@
107
108 import re, time
109 from bzrlib import (
110+ branch,
111 debug,
112+ errors,
113+ filters,
114 osutils,
115 registry,
116 trace,
117@@ -51,6 +54,12 @@
118 lambda c: ", ".join(c.revision().get_apparent_authors()))
119 keyword_registry.register('Revision-Id',
120 lambda c: c.revision_id())
121+keyword_registry.register('Revid',
122+ lambda c: c.revision_id())
123+keyword_registry.register('Revision-Number',
124+ lambda c: _revision_id_to_revno(c.revision_id()))
125+keyword_registry.register('Revno',
126+ lambda c: _revision_id_to_revno(c.revision_id()))
127 keyword_registry.register('Path',
128 lambda c: c.relpath())
129 keyword_registry.register('Directory',
130@@ -59,6 +68,14 @@
131 lambda c: osutils.split(c.relpath())[1])
132 keyword_registry.register('File-Id',
133 lambda c: c.file_id())
134+keyword_registry.register('Branch-Revision-Id',
135+ lambda c: _current_branch_id())
136+keyword_registry.register('Branch-Revid',
137+ lambda c: _current_branch_id())
138+keyword_registry.register('Branch-Revision-Number',
139+ lambda c: _revision_id_to_revno(_current_branch_id()))
140+keyword_registry.register('Branch-Revno',
141+ lambda c: _revision_id_to_revno(_current_branch_id()))
142
143 # Environment-related keywords
144 keyword_registry.register('Now',
145@@ -262,3 +279,36 @@
146 def _xml_escape_kw_expander(chunks, context=None):
147 """Filter that replaces keywords with a form suitable for use in XML."""
148 return _kw_expander(chunks, context, encoder=_xml_escape)
149+
150+_keyword_branch = registry.Registry()
151+
152+def _get_branch(location=u'.', b=None):
153+ if 'branch' in _keyword_branch.keys():
154+ return _keyword_branch.get('branch')
155+ else:
156+ if b is None:
157+ b = branch.Branch.open_containing(location)[0]
158+ _keyword_branch.register('branch', b)
159+ return b
160+
161+def _revision_id_to_revno(revid):
162+ b = _get_branch()
163+ try:
164+ revno = b.revision_id_to_revno(revid)
165+ return str(revno)
166+ except:
167+ # We need to load and use the full revno map after all
168+ result = b.get_revision_id_to_revno_map().get(revid)
169+ if result is None:
170+ return result
171+ return ".".join(result)
172+
173+def _current_branch_id(location=None, revid=None, b=None):
174+ if 'current' in _keyword_branch.keys():
175+ return _keyword_branch.get('current')
176+ else:
177+ if revid is None:
178+ b = _get_branch(location, b)
179+ revid = b.last_revision()
180+ _keyword_branch.register('current', revid)
181+ return revid

Subscribers

People subscribed via source and target branches

to all changes: