1

I'm trying to use the subprocess module to perform a git pull operation in a repository. The problem is that other commands, such as git status work fine, but the git pull doesn't. No output is generated.

This is where I'm stuck:

import subprocess

subprocess.check_output(
  ['git', 'pull', 'origin', 'main']
)

When executed, the following error is thrown:

[WinError 6] The handle is invalid

I also used the os.chdir and cwd to change the dir context to the target repository, but it didn't work as expected:

import os
import subprocess

os.chdir(my_repository_dir)

subprocess.check_output(
  ['git', 'pull', 'origin', 'main'],
  cwd=my_repository_dir
)

None of the following subprocess methods worked: check_output, check_call, run and Popen. Either using the shell attribute or not.

Furthermore, I reached the GitPython module, but it didn't work either.

My Python script runs on a Windows Server 2022, and the installed Python version is 3.6.8. The git pull instruction is evaluated when the exposed Flask app handles an incoming HTTP request.

The target dir is a valid Git repository.

I've already included the SSH key on Gitlab, and the account credentials are stored in the Windows Credentials Manager.

If git pull isn't intended to be invoked from scripts, I'd like to understand the proper way to do this.

user299294
  • 11
  • 3
  • Related/possible duplicate: [Python running as Windows Service: OSError: \[WinError 6\] The handle is invalid](https://stackoverflow.com/questions/40108816/python-running-as-windows-service-oserror-winerror-6-the-handle-is-invalid) – Brian61354270 Feb 20 '23 at 15:17
  • @brian DEVNULL is meant to discard any information it receives. So, I think it would omit the error but wouldn't fix the issue. – user299294 Feb 20 '23 at 15:59
  • That question is about the STDIN handle used by the subprocess. That has no impact on what or how the subprocess writes to STDOUT/STDERR. – Brian61354270 Feb 20 '23 at 16:20
  • @brian I've come across this question, but even with other special values, such as DEVNULL, PIPE, STDOUT, etc. I couldn't get any relevant results :/ – user299294 Feb 20 '23 at 17:03

1 Answers1

0

Looking at other use of check_output with git commands in this library, you can try:

result = subprocess.check_output('git pull', shell=True, stderr=subprocess.STDOUT).decode()
print(result)

By redirecting the stderr stream to the same output as stdout, you will be sure to capture everything.
That matters considering most git commands outputs information message on stderr.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer. I've tried that way, and it did work with `git status`, but when I attempt to run a `git pull` instead, nothing happens; neither the following `print` nor the following instructions are executed. The program just gets blocked... Is it possible that the CMD is waiting for such input, or the `git` could not reach the git credentials even after I've added the SSH key to the repository? – user299294 Feb 21 '23 at 12:38
  • @user299294 It is possible, yes. First, check the remote URL actually used (git remote -v). If it is an HTTPS one, SSH key won't matter. If it is an SSH one, HTTPS credentials won't matter. Second, try a git pull manually (outside your program). Third, make sure your program is running with the same Windows account as the one you are using for a manual test. – VonC Feb 21 '23 at 12:42