Attachment 'qtimes.py'
Download 1 # qtimes.py - save or restore modification times of patched files
2 #
3 # Copyright 2008 Andrei Vermel <andrei.vermel@gmail.com>
4 #
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
7
8 """save or restore modification times of patched files"""
9
10 from mercurial.i18n import _
11 from mercurial.node import *
12 from mercurial.error import RepoError
13 from mercurial import commands, scmutil, hg, node, util
14 import os
15
16 _sha1 = util.sha1
17
18 def times(ui, repo, *pats, **opts):
19 """save or restore modification times of patched files"""
20 try:
21 node1, node2 = scmutil.revpair(repo, ['qparent', 'qtip'])
22 except RepoError:
23 node1, node2 = 0, 0
24 if node1 == node2:
25 ui.warn(_('no patches applied - nothing to do'))
26 return True
27 matchfn = scmutil.match(repo[None], pats, opts)
28 cwd = (pats and repo.getcwd()) or ''
29 modified, added, removed, deleted, unknown, ignored, clean = [
30 n for n in repo.status(node1=node1, node2=node2, match=matchfn)]
31 if opts['save']:
32 pm = file(repo.path+'/patched_mtimes', 'w')
33 patched = modified + added
34 for f in patched:
35 fname = repo.wjoin(f)
36 mtime = os.stat(fname).st_mtime
37 checksum = _sha1("")
38 checksum.update(file(fname, 'r').read())
39 pm.write("%s,%s,%s\n" % (fname, checksum.hexdigest(), mtime))
40 elif opts['restore']:
41 pm = file(repo.path+'/patched_mtimes', 'r')
42 for line in pm.readlines():
43 fname, checksum, mtime = line.strip().split(',')
44 if not os.path.exists(fname):
45 continue
46 cs = _sha1("")
47 cs.update(file(fname, 'r').read())
48 if cs.hexdigest() != checksum:
49 ui.warn(_('Not restoring timestamp of file %s\n') % fname)
50 else:
51 st = os.stat(fname)
52 os.utime(fname, (st.st_atime, type(st.st_mtime)(mtime)))
53 else:
54 ui.warn(_("hg qtimes: either -r or -s option is needed\n"))
55 commands.help_(ui, 'qtimes')
56
57 return True
58
59 cmdtable = {
60 "qtimes":
61 (times,
62 [('s', 'save', None, _('save modification times')),
63 ('r', 'restore', None, _('restore modification times'))],
64 _('hg qtimes (-s | -r)')),
65 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.
