Have you considered using GitPython? It's designed to handle all this nonsense for you.
import git # pip install gitpython
g = git.cmd.Git(git_dir)
g.pull()
To install the module use pip install GitPython
Project can be found here, GitPython
Answer from jleahy on Stack OverflowHave you considered using GitPython? It's designed to handle all this nonsense for you.
import git # pip install gitpython
g = git.cmd.Git(git_dir)
g.pull()
To install the module use pip install GitPython
Project can be found here, GitPython
subprocess.Popen expects a list of the program name and arguments. You're passing it a single string, which is (with the default shell=False) equivalent to:
['git pull']
That means that subprocess tries to find a program named literally git pull, and fails to do so: In Python 3.3, your code raises the exception FileNotFoundError: [Errno 2] No such file or directory: 'git pull'. Instead, pass in a list, like this:
import subprocess
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE)
output = process.communicate()[0]
By the way, in Python 2.7+, you can simplify this code with the check_output convenience function:
import subprocess
output = subprocess.check_output(["git", "pull"])
Also, to use git functionality, it's by no way necessary (albeit simple and portable) to call the git binary. Consider using git-python or Dulwich.
Videos
» pip install git-pull-request
I managed this by getting the repo name directly:
repo = git.Repo('repo_path')
o = repo.remotes.origin
o.pull()
As the accepted answer says it's possible to use repo.remotes.origin.pull(), but the drawback is that it hides the real error messages into it's own generic errors. For example when DNS resolution doesn't work, then repo.remotes.origin.pull() shows the following error message:
git.exc.GitCommandError: 'Error when fetching: fatal: Could not read from remote repository.
' returned with exit code 2
On the other hand using git commands with GitPython like repo.git.pull() shows the real error:
git.exc.GitCommandError: 'git pull' returned with exit code 1
stderr: 'ssh: Could not resolve hostname github.com: Name or service not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.'
GitPython is only a wrapper around Git. I assume you are wanting to create a pull request in a Git hosting service (Github/Gitlab/etc.).
You can't create a pull request using the standard git command line. git request-pull, for example, only Generates a summary of pending changes. It doesn't create a pull request in GitHub.
If you want to create a pull request in GitHub, you can use the PyGithub library.
Or make a simple HTTP request to the Github API with the requests library:
import json
import requests
def create_pull_request(project_name, repo_name, title, description, head_branch, base_branch, git_token):
"""Creates the pull request for the head_branch against the base_branch"""
git_pulls_api = "https://github.com/api/v3/repos/{0}/{1}/pulls".format(
project_name,
repo_name)
headers = {
"Authorization": "token {0}".format(git_token),
"Content-Type": "application/json"}
payload = {
"title": title,
"body": description,
"head": head_branch,
"base": base_branch,
}
r = requests.post(
git_pulls_api,
headers=headers,
data=json.dumps(payload))
if not r.ok:
print("Request Failed: {0}".format(r.text))
create_pull_request(
"<your_project>", # project_name
"<your_repo>", # repo_name
"My pull request title", # title
"My pull request description", # description
"banana-refresh", # head_branch
"banana-integration", # base_branch
"<your_git_token>", # git_token
)
This uses the GitHub OAuth2 Token Auth and the GitHub pull request API endpoint to make a pull request of the branch banana-refresh against banana-integration.
It appears as though pull requests have not been wrapped by this library.
You can call the git command line directly as per the documentation.
repo.git.pull_request(...)
The first step is to create a git.Repo object to represent your repository.
from git import Repo
# rorepo is a Repo instance pointing to the git-python repository.
# For all you know, the first argument to Repo is a path to the repository
# you want to work with
repo = Repo(self.rorepo.working_tree_dir)
assert not repo.bare
In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.
This is what you asked for :
repo = git.Repo('repo_name')
o = repo.remotes.origin
o.pull()
It depends on your OS but you should use a credential helper in order to cache the password (or token if you have 2FA activated) in it.
This is assuming your are trying to pull from a private repository.
On Windows, for example, that would be "manager", and you can cache your credentials with git credential fill.