0

I would really need a big help on this since I am convinced I did everything OK. I have my exec_command method which was working for all different bash commands until now. I never had issue with it until now.

def exec_cmd(cmd, fail=True):
    """
    Execute shell command

    :param cmd: list containing command and its parameters
    """
    try:
        print cmd
        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        return output
..............

I want for Git repo, to execute specific command which should create a new branch based on a specific commit from the current branch. On some previous question I got a really amazing Git command to execute that only in one line (based on the commit message) Manually on Linux server this command execution works:

git checkout -b newBranch ":/\[AccuRev transaction: 971867\]"
Updating files: 100% (32046/32046), done.
Switched to a new branch 'newBranch'

So now I want to execute it from python:

os.chdir('/home/user/git_repo')
exec_cmd(['git', 'checkout', '-b', 'newBranch', '\":/\[AccuRev transaction: 971867\]\"'])

but it fails although executed command in the log ERROR message looks EXACTLY the same compared to the manual command (but maybe some slashes or white spaces are generating the issue).

exec_cmd:
['git', 'checkout', '-b', 'newBranch', '":/\\[AccuRev transaction: 971867\\]"']
Exception when executing cmd!!!!
ERROR: command "git checkout -b newBranch ":/\[AccuRev transaction: 971867\]"" failed with return code 128
Command output:
fatal: '":/\[AccuRev transaction: 971867\]"' is not a commit and a branch 'newBranch' cannot be created from it
8
  • The quotes are not for git, they're consumed by the shell. Commented Mar 23, 2022 at 15:18
  • If you want to see the literal string that the shell passes to git (which doesn't include the double quotes), run printf '%s\n' ":/\[AccuRev transaction: 971867\]" at your shell. The output of that is what you need to write as a Python string literal. Commented Mar 23, 2022 at 15:18
  • git checkout -b newBranch ":/\[AccuRev transaction: 971867\]" is the command which is executed for every shell command. the first double quotes are not an issue because all other commands WORK!!! they always have first and last double quotes Commented Mar 23, 2022 at 15:20
  • ...in this case, one way to accurately write it might be r':/\[AccuRev transaction: 971867\]' Commented Mar 23, 2022 at 15:20
  • exec_cmd(['git', 'checkout', '-b', 'newBranch', r':/\[AccuRev transaction: 971867\]']) like this??? Commented Mar 23, 2022 at 15:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.