Skip to content

Commit 8ef208c

Browse files
committed
misc: Fix new name-shortening for relative and non-path cases
This situation was being hit when starting a new blank comparison (so no actual file names) and then loading a file into one pane. The pane with a file ended up with valid parent components, but nothing in common with the other pane.
1 parent 32e39ec commit 8ef208c

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

meld/misc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import shutil
2727
import subprocess
2828
from pathlib import PurePath
29+
from typing import List
2930

3031
from gi.repository import Gdk
3132
from gi.repository import GLib
@@ -293,7 +294,7 @@ def all_same(iterable):
293294
return True
294295

295296

296-
def shorten_names(*names):
297+
def shorten_names(*names) -> List[str]:
297298
"""Remove common parts of a list of paths
298299
299300
For example, `('/tmp/foo1', '/tmp/foo2')` would be summarised as
@@ -307,6 +308,8 @@ def shorten_names(*names):
307308
# Identify the longest common path among the list of path
308309
common = set(paths[0].parents)
309310
common = common.intersection(*(p.parents for p in paths))
311+
if not common:
312+
return list(names)
310313
common_parent = sorted(common, key=lambda p: -len(p.parts))[0]
311314

312315
paths = [p.relative_to(common_parent) for p in paths]

test/test_misc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def test_all_same(lst, expected):
9090
('posix', ['/tmp/bar/subdir/subsub', '/tmp/bar/'], ['subsub', 'bar']),
9191
('nt', ['C:\\Users\\hmm\\bar', 'C:\\Users\\hmm\\foo'], ['bar', 'foo']),
9292
('nt', ['C:\\Users\\bar\\hmm', 'C:\\Users\\foo\\hmm'], ['[bar] hmm', '[foo] hmm']),
93+
# Check that paths with no commonality are handled
94+
('posix', ['nothing in', 'common'], ['nothing in', 'common']),
95+
('posix', ['<unnamed>', '/tmp/real/path'], ['<unnamed>', '/tmp/real/path']),
9396
])
9497
def test_shorten_names(os_name, paths, expected):
9598
from meld.misc import shorten_names

0 commit comments

Comments
 (0)