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
vel
  • 1,000
  • 1
  • 13
  • 35
  • The quotes are not for `git`, they're consumed by the shell. – Charles Duffy Mar 23 '22 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. – Charles Duffy Mar 23 '22 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 – vel Mar 23 '22 at 15:20
  • ...in this case, one way to accurately write it might be `r':/\[AccuRev transaction: 971867\]'` – Charles Duffy Mar 23 '22 at 15:20
  • `exec_cmd(['git', 'checkout', '-b', 'newBranch', r':/\[AccuRev transaction: 971867\]'])` like this??? – vel Mar 23 '22 at 15:21
  • Yes, like that. – Charles Duffy Mar 23 '22 at 15:22
  • I need to have included single or double quotes surrounding `:/\[AccuRev transaction: 971867\]` it will not work without single or double quotes - if you see every subcommand has single quote – vel Mar 23 '22 at 15:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243239/discussion-between-charles-duffy-and-veljkosbbb). – Charles Duffy Mar 23 '22 at 15:22

0 Answers0