Scripting Git tasks is made simpler using GitPython. We can use it to create local Git applications. Here is a simple example script to perform initial Git setup for our project directory /home/user/myproject. We have already created a new repository in GitHub with url https://github.com/user/test.git. Run the script as follows
python initwithGit /home/user/myproject https://github.com/dileepnandanam/test.git
# initWithGit.py import git import commands import sys import os projectdir=sys.argv[1] remote=sys.argv[2] modules=os.listdir(projectdir) g=git.cmd.Git(projectdir) g.init() for module in modules: g.add(module) message='first commit' (stats,op)=commands.getstatusoutput("git commit -m '"+message+"'"+projectdir) g.push(remote)Class git makes bindings with git binaries possible
The statement g=git.cmd.Git(projectdir)
enables us to manage the local repository using object g. We can issue git commands as
- g.init()
- g.commit()
- g.branch(branchname)
- g.checkout(branchname)
- g.push(remoteRepo)
We can monitor current repository status through git.Repo
eg.
repo= git.Repo(projectdir) for branch in repo.heads: print branch.name print branch.commitThis will display all branches and details of commits corresponding to repository
No comments:
Post a Comment