From 6f0c635c93815a7a297efc8740c50a7c5a193035 Mon Sep 17 00:00:00 2001 From: Hugo Wang Date: Sun, 20 Nov 2016 23:05:21 +0800 Subject: [PATCH 1/3] make history show all sorted --- xonsh/history.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/xonsh/history.py b/xonsh/history.py index bb0838fd6..1f806a46b 100644 --- a/xonsh/history.py +++ b/xonsh/history.py @@ -70,6 +70,15 @@ def _gc_bytes_to_rmfiles(hsize, files): return rmfiles +def _get_history_files(reverse=False): + data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') + data_dir = expanduser_abs_path(data_dir) + files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) + if f.startswith('xonsh-') and f.endswith('.json')] + files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) + return files + + class HistoryGC(threading.Thread): """Shell history garbage collection.""" @@ -266,18 +275,13 @@ def _all_xonsh_parser(**kwargs): return format: (cmd, start_time, index) """ - data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') - data_dir = expanduser_abs_path(data_dir) - - files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) - if f.startswith('xonsh-') and f.endswith('.json')] ind = 0 - for f in files: + for f in _get_history_files(): try: json_file = LazyJSON(f, reopen=False) except ValueError: # Invalid json file - pass + continue commands = json_file.load()['cmds'] for c in commands: yield (c['inp'].rstrip(), c['ts'][0], ind) From d392a6f45412e5e282d7cb384bb262885bfec5d3 Mon Sep 17 00:00:00 2001 From: Hugo Wang Date: Tue, 22 Nov 2016 08:33:52 +0800 Subject: [PATCH 2/3] updated HistoryGC.files() --- xonsh/history.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/xonsh/history.py b/xonsh/history.py index 1f806a46b..cbc1d95a6 100644 --- a/xonsh/history.py +++ b/xonsh/history.py @@ -2,7 +2,6 @@ """Implements the xonsh history object.""" import os import sys -import glob import json import time import uuid @@ -70,12 +69,16 @@ def _gc_bytes_to_rmfiles(hsize, files): return rmfiles -def _get_history_files(reverse=False): +def _get_history_files(sort=True, reverse=False): + """Find and return the history files. Optionally sort files by + modify time. + """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = expanduser_abs_path(data_dir) files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] - files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) + if sort: + files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) return files @@ -120,20 +123,22 @@ class HistoryGC(threading.Thread): """Find and return the history files. Optionally locked files may be excluded. - This is sorted by the last closed time. Returns a list of (timestamp, - file) tuples. + This is sorted by the last closed time. Returns a list of + (timestamp, number of cmds, file name) tuples. """ # pylint: disable=no-member env = getattr(builtins, '__xonsh_env__', None) if env is None: return [] - xdd = env.get('XONSH_DATA_DIR') - xdd = expanduser_abs_path(xdd) - fs = [f for f in glob.iglob(os.path.join(xdd, 'xonsh-*.json'))] + fs = _get_history_files(sort=False) files = [] for f in fs: try: + if os.path.getsize(f) == 0: + # collect empty files (for gc) + files.append((time.time(), 0, f)) + continue lj = LazyJSON(f, reopen=False) if only_unlocked and lj['locked']: continue From f66f0740872def3fbb4c27f2c85567d56f261059 Mon Sep 17 00:00:00 2001 From: Hugo Wang Date: Tue, 22 Nov 2016 08:53:42 +0800 Subject: [PATCH 3/3] added news file --- news/history-sort.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 news/history-sort.rst diff --git a/news/history-sort.rst b/news/history-sort.rst new file mode 100644 index 000000000..1a60f899a --- /dev/null +++ b/news/history-sort.rst @@ -0,0 +1,14 @@ +**Added:** None + +**Changed:** None + +**Deprecated:** None + +**Removed:** None + +**Fixed:** + +* Made ``history show`` result sorted. +* Fixed issue that ``history gc`` does not delete empty history files. + +**Security:** None