From 54b27c0a13f1aa5a58040544804b3006aee22677 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Mon, 29 Apr 2024 21:38:36 +0000 Subject: [PATCH] Don't shell out to git to resolve upstream branches unnecessarily Even though we already know the parent is origin/main, every branch that is parented to origin/main ends up shelling out to git in the upstream() function, just to double-check that origin/main exists. If the parent has already been registered, don't bother repeating the work. This cuts out about half of the times that git map-branches shells out to git and reduces it from 1.7s to 1.2s on my machine. Change-Id: Ic5fdaaa5bc62ed8a3574f5a28f9f783093dacc2f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5498460 Reviewed-by: Gavin Mak Commit-Queue: Gavin Mak Auto-Submit: David Benjamin --- git_map_branches.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/git_map_branches.py b/git_map_branches.py index ec9c86baf..fbf6092bc 100755 --- a/git_map_branches.py +++ b/git_map_branches.py @@ -153,18 +153,17 @@ class BranchMapper(object): if not branch_info: continue - parent = branch_info.upstream if self.__check_cycle(branch): continue + parent = branch_info.upstream if not self.__branches_info[parent]: - branch_upstream = upstream(branch) - # If git can't find the upstream, mark the upstream as gone. - if branch_upstream: - parent = branch_upstream - else: - self.__gone_branches.add(parent) - # A parent that isn't in the branches info is a root. - roots.add(parent) + # If the parent is not a known branch, it may be an upstream + # branch like origin/main or it may be gone. Determine which it + # is, but don't re-query the same parent multiple times. + if parent not in roots: + if not upstream(branch): + self.__gone_branches.add(parent) + roots.add(parent) self.__parent_map[parent].append(branch)