-1

I need to pass from the command line a list of repos and detect their default branches. So far i only found this command that returns the default HEAD git remote show origin | grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g'

However, i'm not sure how should i execute it in my code.

Here's the execution command python3 app.py testrepo.

And below is the code

@app.route('/test')
def get_default_branch():
    repos = sys.argv[1:]
    origin =repos[0]
    return subprocess.Popen([f"'git', 'remote', 'show', '{origin}''" + "| grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g''" ])
Iannis
  • 11
  • 5

1 Answers1

1

If you want to capture the output, using the check_output api is probably easier

@app.route('/test')
def get_default_branch():
    repos = sys.argv[1:]
    origin =repos[0]
    return subprocess.check_output(
        f"git remote show {origin} | grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g'",
        shell=True
    )

https://docs.python.org/3/library/subprocess.html#subprocess.check_output

Withh shell=True it is also recommended to use strings instead of a list

Tzane
  • 2,752
  • 1
  • 10
  • 21