From 9137f2a04b70d0cb1647f10495e55cec7317764e Mon Sep 17 00:00:00 2001 From: Konboi Date: Fri, 14 Jul 2023 09:23:12 +0900 Subject: [PATCH 1/2] Set shell=True to pass stdout to next command --- launchable/commands/record/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launchable/commands/record/build.py b/launchable/commands/record/build.py index 88c7011c5..fa5f94166 100644 --- a/launchable/commands/record/build.py +++ b/launchable/commands/record/build.py @@ -283,7 +283,7 @@ def _get_branch_name(repo_dist: str) -> str: try: refs = subprocess.check_output( "git show-ref | grep '^'$(git rev-parse HEAD)", - cwd=repo_dist).decode().split("\n") + cwd=repo_dist, shell=True).decode().split("\n") if len(refs) > 0: # e.g) ed6de84bde58d51deebe90e01ddfa5fa78899b1c refs/heads/branch-name branch_name = refs[0].split("/")[-1] From ae0cc90579467e6f0013d3989f678e7d67112f6b Mon Sep 17 00:00:00 2001 From: Konboi Date: Fri, 14 Jul 2023 15:01:14 +0900 Subject: [PATCH 2/2] fixed not to use shell=True --- launchable/commands/record/build.py | 7 ++++--- tests/commands/record/test_build.py | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/launchable/commands/record/build.py b/launchable/commands/record/build.py index fa5f94166..ffe039467 100644 --- a/launchable/commands/record/build.py +++ b/launchable/commands/record/build.py @@ -281,9 +281,10 @@ def _get_branch_name(repo_dist: str) -> str: branch_name = "" try: - refs = subprocess.check_output( - "git show-ref | grep '^'$(git rev-parse HEAD)", - cwd=repo_dist, shell=True).decode().split("\n") + head = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=repo_dist).decode().strip() + show_ref = subprocess.check_output(["git", "show-ref"], cwd=repo_dist).decode() + refs = [ref for ref in show_ref.split("\n") if head in ref] + if len(refs) > 0: # e.g) ed6de84bde58d51deebe90e01ddfa5fa78899b1c refs/heads/branch-name branch_name = refs[0].split("/")[-1] diff --git a/tests/commands/record/test_build.py b/tests/commands/record/test_build.py index 0dabba6fb..130c798c8 100644 --- a/tests/commands/record/test_build.py +++ b/tests/commands/record/test_build.py @@ -21,9 +21,11 @@ def test_submodule(self, mock_check_output): mock_check_output.side_effect = [ # the first call is git rev-parse HEAD ('c50f5de0f06fe16afa4fd1dd615e4903e40b42a2').encode(), - # the second call is git rev-parse --abbrev-ref HEAD - ('ed6de84bde58d51deebe90e01ddfa5fa78899b1c refs/head/main\ned6de84bde58d51deebe90e01ddfa5fa78899b1c refs/remotes/origin/main\n').encode(), # noqa: E501 - # the third call is git submodule status --recursive + # the second call is git rev-parse HEAD for detect branch name + ('c50f5de0f06fe16afa4fd1dd615e4903e40b42a2').encode(), + # the third call is git show-ref for detect branch name + ('c50f5de0f06fe16afa4fd1dd615e4903e40b42a2 refs/head/main\nc50f5de0f06fe16afa4fd1dd615e4903e40b42a2 refs/remotes/origin/main\n').encode(), # noqa: E501 + # the forth call is git submodule status --recursive ( ' 491e03096e2234dab9a9533da714fb6eff5dcaa7 foo (v1.51.0-560-g491e030)\n' ' 8bccab48338219e73c3118ad71c8c98fbd32a4be bar-zot (v1.32.0-516-g8bccab4)\n'