Search code examples
gitpython

pythongit - check before git commit


written small snippet to automate git add , commit and push using pythongit.

                def git_commit_push(self):                
                    repoDir = self.backupRepositoryPath   
                    repo = git.Repo( repoDir )
                    print repo.git.status()
                    repo.git.add('--all')
                    print repo.git.status()       
                    repo.git.commit( m='pusing for backup' )
                    repo.git.push()
                    print repo.git.status()
                    

Need to add below mentioned check points

1: Before commit , check any files are modified. If no files then skip commit

2: Before push , check any committed files to be pushed. If no files then skip push

Please help writing the if condition for these two check points.

Regards, Prasad


Solution

  • Logic is tuned here...

                    def git_commit_push(self):                
                        repoDir = self.backupRepositoryPath   
                        repo = git.Repo( repoDir )
                        print repo.git.status()
                        repo.git.add('--all')
                        changedFiles = repo.index.diff("HEAD")
                        print "====================================="
                        print "changedFiles are :", changedFiles    
                        print "====================================="  
                        if ( changedFiles ):
                            repo.git.commit( m='JenkinsBackup' )
                            repo.git.push()
                        else:
                            print "No files updated"