Home Home > GIT Browse > linux-next
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Poirier <bpoirier@suse.com>2019-08-08 14:42:28 +0900
committerBenjamin Poirier <bpoirier@suse.com>2019-08-08 14:42:28 +0900
commitaa6b5279bf9c7b56d23fc934e4dff233b9dc9d2e (patch)
tree4ea8d9cd6606d962bd58bb14543986b809bf5a33
parente1353be651631a960957d82b948f613095816c46 (diff)
git-sort: merge_tool: Preserve the order of patches when calculating "added"
When merging, the relative order of the patches added to the out-of-tree section between the merge base and remote must be preserved. Previously it was not, on the erroneous expectation that all added patches are commit-sorted. Therefore, added patches (remote - base) in the oot subsection were appended in shuffled order to the oot subsection of the result.
-rw-r--r--scripts/git_sort/lib.py80
-rwxr-xr-xscripts/git_sort/merge_tool.py8
2 files changed, 84 insertions, 4 deletions
diff --git a/scripts/git_sort/lib.py b/scripts/git_sort/lib.py
index 9f7da0efb6..4a4f9d3241 100644
--- a/scripts/git_sort/lib.py
+++ b/scripts/git_sort/lib.py
@@ -26,6 +26,7 @@ import re
import signal
import subprocess
import sys
+import weakref
import pygit2_wrapper as pygit2
@@ -558,3 +559,82 @@ def sequence_insert(series, rev, top):
"Please run scripts/series_sort.py.")
return (name, commit_pos - top_index,)
+
+
+# https://github.com/ActiveState/code/tree/master/recipes/Python/576696_OrderedSet_with_Weakrefs
+class Link(object):
+ __slots__ = 'prev', 'next', 'key', '__weakref__'
+
+
+class OrderedSet(collections.MutableSet):
+ 'Set the remembers the order elements were added'
+ # Big-O running times for all methods are the same as for regular sets.
+ # The internal self.__map dictionary maps keys to links in a doubly linked list.
+ # The circular doubly linked list starts and ends with a sentinel element.
+ # The sentinel element never gets deleted (this simplifies the algorithm).
+ # The prev/next links are weakref proxies (to prevent circular references).
+ # Individual links are kept alive by the hard reference in self.__map.
+ # Those hard references disappear when a key is deleted from an OrderedSet.
+
+ def __init__(self, iterable=None):
+ self.__root = root = Link() # sentinel node for doubly linked list
+ root.prev = root.next = root
+ self.__map = {} # key --> link
+ if iterable is not None:
+ self |= iterable
+
+ def __len__(self):
+ return len(self.__map)
+
+ def __contains__(self, key):
+ return key in self.__map
+
+ def add(self, key):
+ # Store new key in a new link at the end of the linked list
+ if key not in self.__map:
+ self.__map[key] = link = Link()
+ root = self.__root
+ last = root.prev
+ link.prev, link.next, link.key = last, root, key
+ last.next = root.prev = weakref.proxy(link)
+
+ def discard(self, key):
+ # Remove an existing item using self.__map to find the link which is
+ # then removed by updating the links in the predecessor and successors.
+ if key in self.__map:
+ link = self.__map.pop(key)
+ link.prev.next = link.next
+ link.next.prev = link.prev
+
+ def __iter__(self):
+ # Traverse the linked list in order.
+ root = self.__root
+ curr = root.next
+ while curr is not root:
+ yield curr.key
+ curr = curr.next
+
+ def __reversed__(self):
+ # Traverse the linked list in reverse order.
+ root = self.__root
+ curr = root.prev
+ while curr is not root:
+ yield curr.key
+ curr = curr.prev
+
+ def pop(self, last=True):
+ if not self:
+ raise KeyError('set is empty')
+ key = next(reversed(self)) if last else next(iter(self))
+ self.discard(key)
+ return key
+
+ def __repr__(self):
+ if not self:
+ return '%s()' % (self.__class__.__name__,)
+ return '%s(%r)' % (self.__class__.__name__, list(self))
+
+ def __eq__(self, other):
+ if isinstance(other, OrderedSet):
+ return len(self) == len(other) and list(self) == list(other)
+ return not self.isdisjoint(other)
diff --git a/scripts/git_sort/merge_tool.py b/scripts/git_sort/merge_tool.py
index fda4c3478f..60433fcbba 100755
--- a/scripts/git_sort/merge_tool.py
+++ b/scripts/git_sort/merge_tool.py
@@ -63,9 +63,9 @@ if __name__ == "__main__":
# (before, inside, after, set(inside),)
local, base, remote = (
- (s[0], s[1], s[2], set([series_conf.firstword(l)
- for l in s[1]
- if series_conf.filter_patches(l)]),)
+ (s[0], s[1], s[2], lib.OrderedSet([series_conf.firstword(l)
+ for l in s[1]
+ if series_conf.filter_patches(l)]),)
for s in [
series_conf.split(open(s_path))
for s_path in (local_path, base_path, remote_path,)
@@ -74,7 +74,7 @@ if __name__ == "__main__":
added = remote[3] - base[3]
removed = base[3] - remote[3]
- moved = set(lib.list_moved_patches(base[1], remote[1]))
+ moved = lib.OrderedSet(lib.list_moved_patches(base[1], remote[1]))
if added or removed:
print("%d commits added, %d commits removed from base to remote." %