From 97e4c6ab6393cd5019abecb36ae2a003beb7982c Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Tue, 24 Sep 2024 18:45:02 +0000 Subject: [PATCH] Set blame.ignoreRevsFile to .git-blame-ignore-revs if present blame.ignoreRevsFile was set unconditionally in https://crrev.com/c/5838110, and that broke blame for repositories without .git-blame-ignore-revs file. This change explicitly checks for that file before it sets git config. It also removes this config if file is not present, and effectively fixing broken git blame operations. The check should remain there forever since .git-blame-ignore-revs can be removed from a repository. Bug: 368562244 Change-Id: Id8365045635eb9623b0712b14bb3e7e3206b8795 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5888125 Reviewed-by: Gavin Mak Commit-Queue: Josip Sokcevic --- gclient_scm.py | 34 +++++++++++++++++++++++++++++----- git_common.py | 2 ++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/gclient_scm.py b/gclient_scm.py index a4bde4fb2..acdfcea27 100644 --- a/gclient_scm.py +++ b/gclient_scm.py @@ -662,7 +662,8 @@ class GitWrapper(SCMWrapper): def set_config(f): def wrapper(*args): return_val = f(*args) - if os.path.exists(os.path.join(args[0].checkout_path, '.git')): + checkout_path = args[0].checkout_path + if os.path.exists(os.path.join(checkout_path, '.git')): # The config updates to the project are stored in this list # and updated consecutively after the reads. The updates # are done this way because `scm.GIT.GetConfig` caches @@ -671,7 +672,30 @@ class GitWrapper(SCMWrapper): # the cache to set and unset consecutively. config_updates = [] - ignore_submodules = scm.GIT.GetConfig(args[0].checkout_path, + blame_ignore_revs_cfg = scm.GIT.GetConfig( + checkout_path, 'blame.ignorerevsfile') + + blame_ignore_revs_cfg_set = \ + blame_ignore_revs_cfg == \ + git_common.GIT_BLAME_IGNORE_REV_FILE + + blame_ignore_revs_exists = os.path.isfile( + os.path.join(checkout_path, + git_common.GIT_BLAME_IGNORE_REV_FILE)) + + if not blame_ignore_revs_cfg_set and blame_ignore_revs_exists: + config_updates.append( + ('blame.ignoreRevsFile', + git_common.GIT_BLAME_IGNORE_REV_FILE)) + elif blame_ignore_revs_cfg_set and not blame_ignore_revs_exists: + # Some repos may have incorrect config set, unset this + # value. Moreover, some repositories may decide to remove + # git_common.GIT_BLAME_IGNORE_REV_FILE, which would break + # blame without this check. + # See https://crbug.com/368562244 for more details. + config_updates.append(('blame.ignoreRevsFile', None)) + + ignore_submodules = scm.GIT.GetConfig(checkout_path, 'diff.ignoresubmodules', None, 'local') @@ -693,11 +717,11 @@ class GitWrapper(SCMWrapper): gclient_utils.AddWarning(warning_message) - if scm.GIT.GetConfig(args[0].checkout_path, + if scm.GIT.GetConfig(checkout_path, 'fetch.recursesubmodules') != 'off': config_updates.append(('fetch.recurseSubmodules', 'off')) - if scm.GIT.GetConfig(args[0].checkout_path, + if scm.GIT.GetConfig(checkout_path, 'push.recursesubmodules') != 'off': # The default is off, but if user sets submodules.recurse to # on, this becomes on too. We never want to push submodules @@ -706,7 +730,7 @@ class GitWrapper(SCMWrapper): config_updates.append(('push.recurseSubmodules', 'off')) for update in config_updates: - scm.GIT.SetConfig(args[0].checkout_path, + scm.GIT.SetConfig(checkout_path, update[0], update[1], modify_all=True) diff --git a/git_common.py b/git_common.py index 2fde5ec84..47e9583c5 100644 --- a/git_common.py +++ b/git_common.py @@ -84,6 +84,8 @@ GIT_EXE = 'git' if not IS_WIN else win_find_git() # The recommended minimum version of Git, as (, , ). GIT_MIN_VERSION = (2, 26, 0) +GIT_BLAME_IGNORE_REV_FILE = '.git-blame-ignore-revs' + FREEZE = 'FREEZE' FREEZE_SECTIONS = {'indexed': 'soft', 'unindexed': 'mixed'} FREEZE_MATCHER = re.compile(r'%s.(%s)' % (FREEZE, '|'.join(FREEZE_SECTIONS)))