#!/usr/bin/env python3
import os, git, datetime

class my_git():
    def __init__(self, check_folder = str) -> None:
        self.check_folders = check_folder
    def check_folder(self):
        li_all_fo_git = []
        for root, dirs, files in os.walk(self.check_folders, topdown=False):
            for name in dirs: 
                FullPATH = str(os.path.join(root, name))
                if name == ".git":
                    li_all_fo_git.append(root)
        return li_all_fo_git
    
    def git_pull(self, git_folder):
        repo = git.Repo(git_folder)
        for remote in repo.remotes:
            remote_name = repo.remote(name=remote.name)
            remote_name.pull()
    
    def git_push(self, git_folder):
        repo = git.Repo(git_folder)
        if not repo.is_dirty(untracked_files=True):
            print('No Changes detected in Folder', git_folder, sep=" ")
        else:
            print("Changes detected in Folder", git_folder, sep=" ") 
            for remote in repo.remotes:
                repo.git.add('--all')
                repo.git.commit('-m', 'commit message from python script')
                remote_name = repo.remote(name=remote.name)
                remote_name.push()
                print("Push to", remote.url, sep=" ")
            
    def git_main(self):
        list_git_folder = my_git.check_folder(self)
        for git_folder_s in list_git_folder:
            #print(git_folder_s)
            #my_git.git_pull(self, git_folder=git_folder_s)
            my_git.git_push(self, git_folder=git_folder_s)
        
if __name__ == "__main__":
    print(datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S")  + " start...")
    check_my_git = my_git(check_folder= os.sep + "home" + os.sep + os.environ["USER"] + os.sep + "Projekte")
    check_my_git.git_main()
    print(datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S") + " ...end")
