diff --git a/test/functional/api/cas/git.py b/test/functional/api/cas/git.py index 0a69dd3..cdcc6e7 100644 --- a/test/functional/api/cas/git.py +++ b/test/functional/api/cas/git.py @@ -10,6 +10,54 @@ from connection.local_executor import LocalExecutor from test_utils.output import CmdException +def get_submodules_paths(from_dut: bool = False): + executor = TestRun.executor if from_dut else LocalExecutor() + repo_path = TestRun.usr.working_dir if from_dut else TestRun.usr.repo_dir + git_params = "config --file .gitmodules --get-regexp path | cut -d' ' -f2" + + output = executor.run(f"git -C {repo_path} {git_params}") + if output.exit_code != 0: + raise CmdException("Failed to get submodules paths", output) + + return output.stdout.splitlines() + + +def get_repo_files( + branch: str = "HEAD", + with_submodules: bool = True, + with_dirs: bool = False, + from_dut: bool = False, +): + executor = TestRun.executor if from_dut else LocalExecutor() + repo_path = TestRun.usr.working_dir if from_dut else TestRun.usr.repo_dir + git_params = f"ls-tree -r --name-only --full-tree {branch}" + + output = executor.run(f"git -C {repo_path} {git_params}") + if output.exit_code != 0: + raise CmdException("Failed to get repo files list", output) + + files = output.stdout.splitlines() + + if with_submodules: + for subm_path in get_submodules_paths(from_dut): + output = executor.run(f"git -C {os.path.join(repo_path, subm_path)} {git_params}") + if output.exit_code != 0: + raise CmdException(f"Failed to get {subm_path} submodule repo files list", output) + + subm_files = [os.path.join(subm_path, file) for file in output.stdout.splitlines()] + files.extend(subm_files) + + if with_dirs: + # use set() to get unique values only + dirs = set(os.path.dirname(file) for file in files) + files.extend(dirs) + + # change to absolute paths and remove empty values + files = [os.path.realpath(os.path.join(repo_path, file)) for file in files if file] + + return files + + def get_current_commit_hash(from_dut: bool = False): executor = TestRun.executor if from_dut else LocalExecutor() repo_path = TestRun.usr.working_dir if from_dut else TestRun.usr.repo_dir