This repository was archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitup.py
More file actions
171 lines (143 loc) · 6.27 KB
/
Copy pathgitup.py
File metadata and controls
171 lines (143 loc) · 6.27 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python3
# coding: utf-8
#
# version: 0.2 @ 20180517
# [*] Python2 to Python3
# [*] Using glob instead of os.walk
# [+] List all available updates and ask for confirmation
# [+] Recursivity can be disabled
# [*] os.system to subprocess.Popen
# [+] Add requirements.txt for easy dependances installation via pip
# [+] Change informative output
# [+] Checking `git status` before updating
#
# version: 0.4 @ 20190308
# [+] Printing the path of the repository when fetching
# [+] Multi-threading the fetching
# [*] Fixing global variable warning
# [+] Adding argument --git-path
#
# version: x.x
# [+] Logs all in a file
# [*] Re-ask if you want to update when enter (empty response) is recieved
#
HEADER = """ ___ ____ ____ __ __ ____ ____ __ ____ ____
/ __)(_ _)(_ _) ( )( )( _ \( _ \ /__\ (_ _)( ___)
( (_-. _)(_ )( )(__)( )___/ )(_) )/(__)\ )( )__)
\___/(____) (__) (______)(__) (____/(__)(__)(__) (____)
version 0.4 - @xajkep
"""
from termcolor import colored
import os, sys, argparse, time, subprocess
from glob import glob
from pathlib import Path
from threading import Thread
GIT_PATH = '/usr/bin/git'
global repositories_to_update
repositories_to_update = []
def fetch(path_to_repo):
if path_to_repo.find('/') > -1:
repository_name = path_to_repo.split('/')[-1]
else:
repository_name = path_to_repo
print(colored("[-] Fetch git repository", 'green'), colored("%s" % repository_name, 'green', attrs=['bold']), colored("(%s)" % path_to_repo, 'green'))
absolute_path = str(Path(path_to_repo).resolve())
# `git fetch` before, otherwise we will have wrong results from `git status`
git_process = subprocess.Popen(
[GIT_PATH, 'fetch'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=absolute_path)
git_process.wait()
git_process = subprocess.Popen(
[GIT_PATH, 'status', '-uno', '.'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=absolute_path)
stdout = git_process.communicate()[0].decode()
if not "Your branch is up-to-date" in stdout:
repositories_to_update.append({
'name': repository_name,
'absolute_path': absolute_path
})
return
if __name__ == '__main__':
print(colored(HEADER, 'magenta'))
parser = argparse.ArgumentParser(description='Recursively update git repositories')
parser.add_argument('path', metavar='<path>', type=str, help='path to git repositories')
parser.add_argument('--not-recursive', dest='recursivity', action='store_false', help='Disable recursivity')
parser.add_argument('-y', dest='skip_confirmation', action='store_true', help='Skip update confirmation')
parser.add_argument('--git-path', dest='git_path', default=GIT_PATH, help='Path to your git binary (default: %s)' % GIT_PATH)
parser.set_defaults(recursivity=True)
parser.set_defaults(skip_confirmation=False)
args = parser.parse_args()
targeted_path = args.path
recursivity = args.recursivity
skip_confirmation = args.skip_confirmation
confirmed = args.skip_confirmation
GIT_PATH = args.git_path
if not os.path.isdir(args.path):
print(colored("[!] %s is not a existing path" % args.path, "red"))
targeted_dir = args.path
# Search (recursively) all git repositories
git_repositories = set()
for t in glob(targeted_path + '/**/.git/HEAD', recursive=recursivity):
current_dir = t.replace('/.git/HEAD', '')
git_repositories.add(current_dir)
#repositories_to_update = []
threads = []
for path_to_repo in git_repositories:
threads.append(Thread(target=fetch, args=(path_to_repo,)))
threads[-1].start()
for t in threads:
t.join()
"""
if path_to_repo.find('/') > -1:
repository_name = path_to_repo.split('/')[-1]
else:
repository_name = path_to_repo
print(colored("[-] Fetch git repository", 'green'), colored("%s" % repository_name, 'green', attrs=['bold']), colored("(%s)" % path_to_repo, 'green'))
absolute_path = str(Path(path_to_repo).resolve())
# `git fetch` before, otherwise we will have wrong results from `git status`
git_process = subprocess.Popen(
[GIT_PATH, 'fetch'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=absolute_path)
git_process.wait()
git_process = subprocess.Popen(
[GIT_PATH, 'status', '-uno', '.'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=absolute_path)
stdout = git_process.communicate()[0].decode()
if not "Your branch is up-to-date" in stdout:
repositories_to_update.append({
'name': repository_name,
'absolute_path': absolute_path
})
"""
if len(repositories_to_update) == 0:
print(colored("[-] All repositories (%i) are up-to-date" % len(git_repositories), 'blue'))
exit()
print("")
if not skip_confirmation:
print(colored("[-] The following repositories have updates:", 'blue'))
for repo in repositories_to_update:
print(colored(" -", 'blue'), colored("%s" % repo['name'], 'white', attrs=['bold']))
print("")
confirmed = input(colored('[?] Do you want to proceed (y/n)> ', 'blue')).lower() in ['y', 'yes', 'yeah', 'yep', 'yeap', 'ya', 'da', 'ja', 'oui', 'si']
if confirmed:
start_time = time.time()
for repo in repositories_to_update:
print(colored("[+] Updating repository:", 'yellow'), colored(repo['name'], 'yellow', attrs=['bold']))
git_process = subprocess.Popen(
[GIT_PATH, 'pull'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=repo['absolute_path'])
stdout = git_process.communicate()[0].decode()
print(colored(stdout, 'grey'))
elapsed_time = float(time.time() - start_time)
print("")
print(colored("[-] %i/%i repositories updated in %.2f seconds" % (len(repositories_to_update), len(git_repositories), elapsed_time), 'blue'))