From 23e24a4bac8c5014450038d583f535947f93f052 Mon Sep 17 00:00:00 2001 From: Abhishek Arya Date: Wed, 27 Jan 2021 14:28:27 -0800 Subject: Get list of changed files from branch head, instead of master. (#5048) * Get list of changed files from branch head, instead of master. Fixes https://github.com/google/oss-fuzz/issues/5022 * Add debug with subprocess.call. * Try again debugginig. * Try again * Fix works! --- infra/presubmit.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'infra/presubmit.py') diff --git a/infra/presubmit.py b/infra/presubmit.py index 268a1149..cd6cbb6a 100755 --- a/infra/presubmit.py +++ b/infra/presubmit.py @@ -323,14 +323,29 @@ def yapf(paths, validate=True): def get_changed_files(): """Return a list of absolute paths of files changed in this git branch.""" - # FIXME: This doesn't work if branch is behind master. - diff_command = ['git', 'diff', '--name-only', 'FETCH_HEAD'] - return [ - os.path.abspath(path) - for path in subprocess.check_output(diff_command).decode().splitlines() - if os.path.isfile(path) + main_branch = subprocess.check_output( + ['git', 'rev-parse', '--abbrev-ref', 'origin/HEAD']).strip().decode() + branch_commit_hash = subprocess.check_output( + ['git', 'merge-base', 'FETCH_HEAD', main_branch]).strip().decode() + + diff_commands = [ + # Return list of modified files in the commits on this branch. + ['git', 'diff', '--name-only', branch_commit_hash + '..'], + # Return list of modified files from uncommitted changes. + ['git', 'diff', '--name-only'] ] + changed_files = set() + for command in diff_commands: + file_paths = subprocess.check_output(command).decode().splitlines() + for file_path in file_paths: + if not os.path.isfile(file_path): + continue + changed_files.add(file_path) + print('Changed files: {changed_files}'.format( + changed_files=' '.join(changed_files))) + return [os.path.abspath(f) for f in changed_files] + def is_test_dir_blocklisted(directory): """Returns True if |directory| is blocklisted.""" -- cgit v1.2.3