cmds: update the selected filename when running rebase commands
[git-cola.git] / cola / display.py
blob3c730a7b09a4891f138199dfa43b80fbe7e198ce
1 """Display models and utility functions"""
2 import collections
5 def shorten_paths(source_paths):
6 """Shorten a sequence of paths into unique strings for display"""
7 result = {}
8 # Start by assuming that all paths are in conflict.
9 # On each iteration we will collect all the path suffixes, move the newly
10 # unique entries to the result, and repeat until no conflicts remain.
11 count = 0
12 conflicts = list(source_paths)
13 in_conflict = True
14 while in_conflict:
15 count += 1
16 # Gather the suffixes for the current paths in conflict
17 suffixes = collections.defaultdict(list)
18 for path in conflicts:
19 suffix = path_suffix(path, count)
20 suffixes[suffix].append(path)
22 # Loop over the suffixes to gather new conflicts and unique entries.
23 conflicts = []
24 in_conflict = False
26 for suffix, paths in suffixes.items():
27 # If only a single path exists for the suffix then no conflict
28 # exists, and the suffix is valid.
29 if len(paths) == 1:
30 result[paths[0]] = suffix
31 # If this loop runs too long then bail out by using the full path.
32 elif count >= 128:
33 for path in paths:
34 result[path] = path
35 # If multiple paths map to the same suffix then the paths are
36 # considered in conflict, and will be reprocessed.
37 else:
38 conflicts.extend(paths)
39 in_conflict = True
41 return result
44 def path_suffix(path, count):
45 """Return `count` number of trailing path components"""
46 path = normalize_path(path)
47 components = path.split('/')[-count:]
48 return '/'.join(components)
51 def normalize_path(path):
52 """Normalize a path so that only "/" is used as a separator"""
53 return path.replace('\\', '/')