[PATCH] refactoring "--listfile" option for win32mbcs and fixutf8 extensions

5 views
Skip to first unread message

Toshi MARUYAMA

unread,
Aug 13, 2010, 5:25:32 AM8/13/10
to thg...@googlegroups.com
# HG changeset patch
# User Toshi MARUYAMA <marut...@gmail.com>
# Date 1281691377 -32400
# Node ID 133fadc0f82b64d36c04ca1122c554b99298a68b
# Parent 8f9e7970f04ba5ac1ffa11af00dd87576ec811d1
refactoring "--listfile" option for win32mbcs and fixutf8 extensions.
add new option "--listfileutf8".

see comments in util/cmdoptlistfile.py.

diff -r 8f9e7970f04b -r 133fadc0f82b tortoisehg/hgqt/annotate.py
--- a/tortoisehg/hgqt/annotate.py Thu Aug 12 22:31:45 2010 +0200
+++ b/tortoisehg/hgqt/annotate.py Fri Aug 13 18:22:57 2010 +0900
@@ -12,6 +12,7 @@

from tortoisehg.hgqt import visdiff, qtlib, wctxactions
from tortoisehg.util import paths, hglib, colormap
+from tortoisehg.util import cmdoptlistfile
from tortoisehg.hgqt.i18n import _
from tortoisehg.hgqt.grep import SearchWidget

@@ -387,6 +388,7 @@
line = int(line)
try:
repo = hg.repository(ui.ui(), path=paths.find_root())
+ pats += cmdoptlistfile.get_files_from_listfile()
ctx = repo[opts.get('rev') or '.']
fctx = ctx[pats[0]] # just for validation
except Exception, e:
diff -r 8f9e7970f04b -r 133fadc0f82b tortoisehg/hgqt/quickop.py
--- a/tortoisehg/hgqt/quickop.py Thu Aug 12 22:31:45 2010 +0200
+++ b/tortoisehg/hgqt/quickop.py Fri Aug 13 18:22:57 2010 +0900
@@ -14,6 +14,7 @@

from tortoisehg.hgqt.i18n import _
from tortoisehg.util import hglib, shlib, paths
+from tortoisehg.util import cmdoptlistfile

from tortoisehg.hgqt import qtlib, status, cmdui

@@ -38,6 +39,8 @@
repo = hg.repository(ui.ui(), path=paths.find_root())
assert repo
os.chdir(repo.root)
+ pats += cmdoptlistfile.get_files_from_listfile()
+
self.setWindowTitle('%s - hg %s' % (hglib.get_reponame(repo), command))

layout = QVBoxLayout()
diff -r 8f9e7970f04b -r 133fadc0f82b tortoisehg/hgqt/rename.py
--- a/tortoisehg/hgqt/rename.py Thu Aug 12 22:31:45 2010 +0200
+++ b/tortoisehg/hgqt/rename.py Fri Aug 13 18:22:57 2010 +0900
@@ -17,7 +17,7 @@
from tortoisehg.hgqt.i18n import _
from tortoisehg.hgqt import cmdui, qtlib
from tortoisehg.util import hglib, paths
-
+from tortoisehg.util import cmdoptlistfile

class RenameDialog(QDialog):
"""TortoiseHg rename dialog"""
@@ -39,6 +39,7 @@
try:
self.root = paths.find_root()
self.repo = hg.repository(ui, self.root)
+ pats += cmdoptlistfile.get_files_from_listfile()
except (error.RepoError):
qtlib.ErrorMsgBox(_('Rename Error'),
_('Could not find or initialize the repository'
diff -r 8f9e7970f04b -r 133fadc0f82b tortoisehg/hgqt/run.py
--- a/tortoisehg/hgqt/run.py Thu Aug 12 22:31:45 2010 +0200
+++ b/tortoisehg/hgqt/run.py Fri Aug 13 18:22:57 2010 +0900
@@ -27,6 +27,7 @@
from tortoisehg.hgqt.i18n import agettext as _
from tortoisehg.util import hglib, paths, shlib
from tortoisehg.util import version as thgversion
+from tortoisehg.util import cmdoptlistfile
from tortoisehg.hgqt import qtlib
try:
from tortoisehg.util.config import nofork as config_nofork
@@ -89,42 +90,6 @@
shell=True)
sys.exit(0)

-def get_list_from_file(filename):
- try:
- if filename == '-':
- lines = [ x.replace("\n", "") for x in sys.stdin.readlines() ]
- else:
- fd = open(filename, "r")
- lines = [ x.replace("\n", "") for x in fd.readlines() ]
- fd.close()
- os.unlink(filename)
- except IOError, e:
- sys.stderr.write(_('can not read file "%s". Ignored.\n') % filename)
- return []
-
- # Convert absolute file paths to repo/cwd canonical
- cwd = os.getcwd()
- root = paths.find_root(cwd)
- if not root:
- return lines
- if cwd == root:
- cwd_rel = ''
- else:
- cwd_rel = cwd[len(root+os.sep):] + os.sep
- files = []
- for f in lines:
- try:
- cpath = util.canonpath(root, cwd, f)
- # canonpath will abort on .hg/ paths
- except util.Abort:
- continue
- if cpath.startswith(cwd_rel):
- cpath = cpath[len(cwd_rel):]
- files.append(cpath)
- else:
- files.append(f)
- return files
-
def _parse(ui, args):
options = {}
cmdoptions = {}
@@ -166,7 +131,11 @@
listfile = options.get('listfile')
if listfile:
del options['listfile']
- args += get_list_from_file(listfile)
+ cmdoptlistfile.get_lines_from_listfile(listfile, False)
+ listfileutf8 = options.get('listfileutf8')
+ if listfileutf8:
+ del options['listfileutf8']
+ cmdoptlistfile.get_lines_from_listfile(listfileutf8, True)

return (cmd, cmd and i[0] or None, args, options, cmdoptions, alias)

@@ -699,6 +668,7 @@
('', 'nofork', None, _('do not fork GUI process')),
('', 'fork', None, _('always fork GUI process')),
('', 'listfile', '', _('read file list from file')),
+ ('', 'listfileutf8', '', _('read file list from file encoding utf-8')),
]

table = {
diff -r 8f9e7970f04b -r 133fadc0f82b tortoisehg/util/cmdoptlistfile.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tortoisehg/util/cmdoptlistfile.py Fri Aug 13 18:22:57 2010 +0900
@@ -0,0 +1,94 @@
+# cmdoptlistfile.py - parse and store files with --listfile and --listfileutf8 options.
+#
+# Copyright 2010 Toshi MARUYAMA <marut...@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2, incorporated herein by reference.
+
+# Windows and Nautilus shellext execute
+# "thg subcmd --listfile TMPFILE" or "thg subcmd --listfileutf8 TMPFILE"(planning) .
+# Extensions written in .hg/hgrc is enabled after calling "hg.repository(ui, root)".
+#
+# 1. win32mbcs extension
+# Japanese shift_jis and Chinese big5 contain '0x5c'(backslash) in filename.
+# Mercurial resolves this problem with win32mbcs extension.
+# So, thg must parse path after loading win32mbcs extension.
+#
+# 2. fixutf8 extension
+# fixutf8 extension requires paths encoding utf-8.
+# So, thg need to convert to utf-8.
+#
+
+import os
+import sys
+
+from mercurial import util, extensions
+from tortoisehg.util import paths
+from tortoisehg.util import hglib
+
+_lines = []
+_linesutf8 = []
+
+def get_lines_from_listfile(filename, isutf8):
+ global _lines
+ global _linesutf8
+ try:
+ if filename == '-':
+ lines = [ x.replace("\n", "") for x in sys.stdin.readlines() ]
+ else:
+ fd = open(filename, "r")
+ lines = [ x.replace("\n", "") for x in fd.readlines() ]
+ fd.close()
+ os.unlink(filename)
+ if isutf8:
+ _linesutf8 = lines
+ else:
+ _lines = lines
+ except IOError, e:
+ sys.stderr.write(_('can not read file "%s". Ignored.\n') % filename)
+
+def get_files_from_listfile():
+ global _lines
+ global _linesutf8
+ lines = []
+ need_to_utf8 = False
+ if sys.platform == 'win32':
+ try:
+ fixutf8 = extensions.find("fixutf8")
+ if fixutf8:
+ need_to_utf8 = True
+ except KeyError:
+ pass
+
+ if need_to_utf8:
+ lines += _linesutf8
+ for l in _lines:
+ lines.append(hglib.toutf(l))
+ else:
+ lines += _lines
+ for l in _linesutf8:
+ lines.append(hglib.fromutf(l))
+
+ # Convert absolute file paths to repo/cwd canonical
+ cwd = os.getcwd()
+ root = paths.find_root(cwd)
+ if not root:
+ return lines
+ if cwd == root:
+ cwd_rel = ''
+ else:
+ cwd_rel = cwd[len(root+os.sep):] + os.sep
+ files = []
+ for f in lines:
+ try:
+ cpath = util.canonpath(root, cwd, f)
+ # canonpath will abort on .hg/ paths
+ except util.Abort:
+ continue
+ if cpath.startswith(cwd_rel):
+ cpath = cpath[len(cwd_rel):]
+ files.append(cpath)
+ else:
+ files.append(f)
+ return files
+

Steve Borho

unread,
Aug 13, 2010, 10:42:22 AM8/13/10
to thg...@googlegroups.com
On Fri, Aug 13, 2010 at 4:25 AM, Toshi MARUYAMA <marut...@gmail.com> wrote:
> # HG changeset patch
> # User Toshi MARUYAMA <marut...@gmail.com>
> # Date 1281691377 -32400
> # Node ID 133fadc0f82b64d36c04ca1122c554b99298a68b
> # Parent  8f9e7970f04ba5ac1ffa11af00dd87576ec811d1
> refactoring "--listfile" option for win32mbcs and fixutf8 extensions.
> add new option "--listfileutf8".
>
> see comments in util/cmdoptlistfile.py.

Can you explain why the various apps need to call
cmdoptlistfile.get_files_from_listfile()?

Why aren't all of the filenames added to the pats list sent to every dialog?

--
Steve Borho

Toshi MARUYAMA

unread,
Aug 13, 2010, 4:12:50 PM8/13/10
to thg...@googlegroups.com
Steve Borho wrote (2010-08-13 23:42):
> On Fri, Aug 13, 2010 at 4:25 AM, Toshi MARUYAMA <marut...@gmail.com> wrote:
>> # HG changeset patch
>> # User Toshi MARUYAMA<marut...@gmail.com>
>> # Date 1281691377 -32400
>> # Node ID 133fadc0f82b64d36c04ca1122c554b99298a68b
>> # Parent 8f9e7970f04ba5ac1ffa11af00dd87576ec811d1
>> refactoring "--listfile" option for win32mbcs and fixutf8 extensions.
>> add new option "--listfileutf8".
>>
>> see comments in util/cmdoptlistfile.py.
>
> Can you explain why the various apps need to call
> cmdoptlistfile.get_files_from_listfile()?

Filelist in "--listfile" is fullpath and written by shellext.
For example "C:\somewhere\file1.txt".
If file1.txt contain '0x5c', thg destroy it before win32mbcs enabled.

As described at comments in util/cmdoptlistfile.py, before calling
"hg.repository(ui, root)" thg cannot know win32mbcs and fixutf8 extension enabled.

> Why aren't all of the filenames added to the pats list sent to every dialog?
>

As far as I know, --listfile option needs for "add", "remove", "rename" and "status".
hgtk support "hgtk log FILE", but thg is not implemented this feature now.

Steve Borho

unread,
Aug 14, 2010, 2:19:04 PM8/14/10
to thg...@googlegroups.com

I would prefer (re-)parsing the file after loading extensions inside
run.py if that is required. Each app shouldn't need to query the
files provided by the shell extension. That will just lead to bugs.

--
Steve Borho

Reply all
Reply to author
Forward
0 new messages