简体   繁体   中英

how to pull, push with remote branch

I'm trying to automate a change process which currently creates source code that gets manually pushed to Git. I'm trying to wrap that code using GitPython :

from git import *

# create the local repo
repo = Repo.init("/tmp/test/repo", bare=True)
assert repo.bare == True

# check status of active branch
repo.is_dirty()

# clone the remote repo
remote_repo = repo.clone("http://user:pass@git/repo.git")

# compile source code into repository
# ... 

# track untracked files
repo.untracked_files

# commit changes locally
repo.commit("commit changes")

# push changes to master
remote_repo.push()

When I try running this, I get

Traceback (most recent call last):

File "git_test2.py", line 33, in

repo.commit("commit changes")

BadObject: 636f6d6d6974206368616e676573

The script is able to pull the remote repository, but fails on commit. Is there a better approach to this?

Some of the functions you are using may not work the way you expect them to. Generally, Repo methods are not the equivalent of the git sub-command with the same name.

If you are trying to clone a remote repository, this can be achieved in a single line:

repo = Repo.clone_from("http://user:pass@git/repo.git", "/tmp/test/repo")

See the API Reference for more information on how to use GitPython.

You can't commit against a bare repository. You can only push/pull to them. By parallel think about how you would do this locally. Try cloning a bare repo and doing the actions, they won't work.

I'm not intimately familiar with the pythonic git bindings, but would imagine that you would need to clone a working repository, optionally checkout a given branch instead of master, do your work, call git add against just that stuff, and then commit.

Also, repo.untracked_files is a no op that simply lists them, it doesn't add them.

Honestly it looks like you blindly copy pasted from https://pythonhosted.org/GitPython/0.3.1/tutorial.html without actually reading what it had to say.

you'll need to manipulate the Index Object for example

index = repo.index
for ( path, stage ), entry in index.entries.iteritems: pass
index.add(['SOMEFILE'])
new_commit = index.commit("YOUR COMMIT MESSAGE")
#do somethign with new commit    

Another example I found

import git
repo = git.Repo( '/home/me/repodir' )
print repo.git.status()
# checkout and track a remote branch
print repo.git.checkout( 'origin/somebranch', b='somebranch' )
# add a file
print repo.git.add( 'somefile' )
# commit
print repo.git.commit( m='my commit message' )
# now we are one commit ahead
print repo.git.status()
# now push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM