0

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

Community
  • 1
  • 1

2 Answers2

2

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"
Lakshmi narayana
  • 184
  • 1
  • 3
  • 10
0

Hope this should help.

                def git_commit_push(self):                
                    repoDir = self.backupRepositoryPath   
                    repo = git.Repo( repoDir )
                    print repo.git.status()
                    repo.git.add('--all')
                    changedFiles = repo.git.diff('HEAD~1..HEAD', name_only=True)
                    print "====================================="
                    print "changedFiles are :", changedFiles    
                    print "====================================="  
                    if ( changedFiles ):
                        repo.git.commit( m=changedFiles )
                        repo.git.push()
                    else:
                        print "No files updated"
Lakshmi narayana
  • 184
  • 1
  • 3
  • 10