본문 바로가기
git

Python - git check and git pull

by space father python 2022. 12. 29.
반응형

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import git
 
def remote_git_check():
    for remote in repo.remotes:
        print ("[ " + str(remote) + " branches ]")
        for branch in getattr(repo.remotes, str(remote)).refs:
            print (" " + str(branch).replace( str(remote)+"/""" ))
            chanaged_flag = False
            for commit in repo.iter_commits(branch, max_count=3):
                print ("")
                print (f"Commit: {str(commit)}")
                rmote_last_commit = str(commit)
                print (f"Summary: {commit.summary}")
 
                for k in commit.stats.files.keys():
                    if "git_diff_restart.py" in k:
                        print(f"findout: {k}")
                        chanaged_flag = True
 
                if chanaged_flag:
                    print("chanaged_flag True")
                    break
 
            if "HEAD" in str(branch):
                print("break")
                break
    return rmote_last_commit
 
 
def local_git_check():
    print ("[ Local branches ]")
    for branch in repo.branches:
        print (" " + str(branch))
        chanaged_flag = False
        for commit in repo.iter_commits(branch, max_count=3):
            print ("")
            print (f"Commit: {commit}")
            local_last_commit = str(commit)
            print (f"Summary: {commit.summary}")
 
            for k in commit.stats.files.keys():
                if "git_diff_restart.py" in k:
                    print(f"findout: {k}")
                    chanaged_flag = True
 
            if chanaged_flag:
                print("chanaged_flag True")
                break
 
        if "master" in str(branch):
            print("break")
            break
 
    return local_last_commit
 
 
def check_diff_pull(remote_last_commit, local_last_commit):
    print(f"\nrmote_last_commit={remote_last_commit}")
    print(f"local_last_commit={local_last_commit}\n")
 
    if remote_last_commit == local_last_commit:
        print("rmote_last_commit == local_last_commit\n")
        return True
    elif remote_last_commit != local_last_commit:
        print("rmote_last_commit != local_last_commit\n")
        git_dir = '../'
        repo = git.Repo(git_dir)
        repo.remotes.origin.pull()
        return False
 
 
PATH_TARGET = '../'
repo = git.Repo( PATH_TARGET )
 
remote_last_commit = remote_git_check()
print('\n')
local_last_commit = local_git_check()
 
if not check_diff_pull(remote_last_commit, local_last_commit):
    remote_last_commit = remote_git_check()
    local_last_commit = local_git_check()
    check_diff_pull(remote_last_commit, local_last_commit)
 
 
 
 
 
cs

 

Result

[ origin branches ]
 HEAD

Commit: 287b29c0eed183db067268301514f0201979c31b
Summary: pywinauto ....

Commit: d01330565901b6b30422caa44c2547482db66f30
Summary: seperation .....

Commit: 20dbc762100d5de4a0ea07aaf5b5f5c536cfc918
Summary: blind n_t_c
break


[ Local branches ]
 master

Commit: 287b29c0eed183db067268301514f0201979c31b
Summary: pywinauto ......

Commit: d01330565901b6b30422caa44c2547482db66f30
Summary: seperation .....

Commit: 20dbc762100d5de4a0ea07aaf5b5f5c536cfc918
Summary: blind n_t_c
break

rmote_last_commit=20dbc762100d5de4a0ea07aaf5b5f5c536cfc918
local_last_commit=20dbc762100d5de4a0ea07aaf5b5f5c536cfc918

rmote_last_commit == local_last_commit