-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgbt
executable file
·1034 lines (693 loc) · 31 KB
/
gbt
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# Git bulk toolkit
from pathlib import Path
from subprocess import DEVNULL, Popen, PIPE, check_output
from threading import Thread
from time import sleep
from random import random
import configparser
import os
import re
import shutil
import sys
import datetime
import math
class ConfigValue:
DEVELOPMENT_DIR = "development_directory"
REPO_BLOCKLIST = "repo_blocklist"
class TerminalStyle:
WHITE = "\033[97m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
DIM = "\033[2m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
CLEAR = "\033[0m"
class OutputFormat:
COLUMN_SEPARATOR = " │ "
COLUMN_SEPARATOR_FORMATTED = TerminalStyle.DIM + COLUMN_SEPARATOR + TerminalStyle.CLEAR
class LogEntry:
def __init__(self, repo, timestamp, relative_date, author, message, branch_list):
self._repo = repo
self._timestamp = timestamp
self._relative_date = relative_date
self._author = author
self._message = message
self._branch_list = branch_list
def repo(self):
return self._repo
def timestamp(self):
return self._timestamp
def relative_date(self):
return self._relative_date
def author(self):
return self._author
def message(self):
return self._message
def branch_list(self):
return self._branch_list
class ComparisonBranchEntry:
def __init__(self, repo, branch_for_comparison, commits_behind, commits_ahead):
self._repo = repo
self._branch_for_comparison = branch_for_comparison
self._commits_behind = commits_behind
self._commits_ahead = commits_ahead
def repo(self):
return self._repo
def branch_for_comparison(self):
return self._branch_for_comparison
def commits_behind(self):
return self._commits_behind
def commits_ahead(self):
return self._commits_ahead
class GitStatusWorker:
def __init__(self, directory, worker_id, submodule_depth):
self._directory = directory
self._worker_id = worker_id
self._submodule_depth = submodule_depth
self._work_in_progress = False
# Fields for status
self._repo_id = ""
self._branch = "unknown"
self._default_branch = "unknown"
self._commits_behind = 0
self._commits_ahead = 0
self._modified_files = []
self._untracked_files = []
# Fields for log
self._log_entries = []
# Fields for branch comparison
self._comparison_branch_entries = []
self._error_pulling = ""
self._error_checking_out = ""
self._error_getting_status = ""
self._error_fetching = ""
self._error_getting_log = ""
def status(self):
if self._work_in_progress is False:
self._error_getting_status = ""
self._work_in_progress = True
self._thread = Thread(target=self._thread_method_status)
self._thread.start()
def list_branches(self):
if self._work_in_progress is False:
self._error_getting_branches = ""
self._work_in_progress = True
self._thread = Thread(target=self._thread_method_list_branches)
self._thread.start()
def fetch(self):
if self._work_in_progress is False:
self._error_fetching = ""
self._work_in_progress = True
self._thread = Thread(target=self._thread_method_fetch)
self._thread.start()
def pull(self):
if self._work_in_progress is False:
self._error_pulling = ""
self._work_in_progress = True
self._thread = Thread(target=self._thread_method_pull)
self._thread.start()
def checkout(self, branch_name):
if self._work_in_progress is False:
self._error_checking_out = ""
self._work_in_progress = True
self._thread = Thread(target=self._thread_method_checkout, args=(branch_name,))
self._thread.start()
def log(self, days_to_log):
if self._work_in_progress is False:
self._error_getting_log = ""
self._work_in_progress = True
self._thread = Thread(target=self._thread_method_log, args=(days_to_log,))
self._thread.start()
def directory(self):
return self._directory
def is_submodule(self):
return self._submodule_depth > 0
def short_name(self):
return os.path.basename(os.path.normpath(self._directory))
def display_name(self):
if self.is_submodule():
return (" " * (self._submodule_depth - 1)) + "↳ " + self.short_name()
else:
return self.short_name()
def branch(self):
return self._branch
def default_branch(self):
return self._default_branch
def is_gbt_repo(self):
return self._repo_id == "a5eab786a76c18fb765ae60742f970da2f5408fc"
def relative_commits(self):
commits_behind = f"⇣{self._commits_behind}" if self._commits_behind > 0 else ""
commits_ahead = f"⇡{self._commits_ahead}" if self._commits_ahead > 0 else ""
return f"{commits_behind}{commits_ahead}"
def commits_behind(self):
return self._commits_behind
def commits_ahead(self):
return self._commits_ahead
def modified_files(self):
return self._modified_files
def untracked_files(self):
return self._untracked_files
def log_entries(self):
return self._log_entries
def comparison_branch_entries(self):
return self._comparison_branch_entries
def work_in_progress(self):
return self._work_in_progress
def error_pulling(self):
return self._error_pulling
def error_checking_out(self):
return self._error_checking_out
def error_getting_status(self):
return self._error_getting_status
def error_fetching(self):
return self._error_fetching
def error_getting_log(self):
return self._error_getting_log
def error_occurred(self):
return self.error_pulling() != "" or self.error_checking_out() != "" or self.error_getting_status() != "" or self.error_fetching() != "" or self.error_getting_log() != ""
def join(self):
self._thread.join()
def _get_default_branch(self):
output = check_output(["git", "symbolic-ref", "refs/remotes/origin/HEAD"], cwd=self._directory)
output = output.decode("UTF-8")
self._default_branch = output.replace("refs/remotes/", "").replace("\n", "")
def _get_repo_id(self):
try:
output = check_output(["git", "rev-list", "HEAD"], cwd=self._directory, stderr=DEVNULL)
output = output.decode("UTF-8")
self._repo_id = output.split("\n")[-2]
except:
pass
def _thread_method_status(self):
self._modified_files.clear()
self._untracked_files.clear()
self._get_default_branch()
self._get_repo_id()
output = check_output(["git", "status", "-sb"], cwd=self._directory)
output = output.decode("UTF-8")
self._process_status_output(output)
self._work_in_progress = False
def _thread_method_list_branches(self):
self._get_default_branch()
output = check_output(["git", "branch", "-r"], cwd=self._directory)
output = output.decode("UTF-8")
self._process_branch_output(output)
self._work_in_progress = False
def _thread_method_log(self, days_to_log):
try:
delimiter = "~|~"
since_date = (datetime.date.today() - datetime.timedelta(days=days_to_log)).strftime("%Y-%m-%d")
output = check_output(["git", "log", "--pretty=format:%h" + delimiter + "%ct" + delimiter + "%cr" + delimiter + "%cn" + delimiter + "%s", "--since=" + since_date, "--all"], cwd=self._directory)
output = output.decode("UTF-8")
lines = output.split("\n")
for line in lines:
if len(line) > 0:
parts = line.split(delimiter)
if len(parts) == 5:
commit_hash = parts[0]
timestamp = parts[1]
relative_date = parts[2]
author = parts[3]
message = parts[4]
branch_list = self._get_branches_containing_commit(commit_hash)
relative_date = self._abbreviate_relative_date(relative_date)
entry = LogEntry(self.short_name(), timestamp, relative_date, author, message, branch_list)
self._log_entries.append(entry)
else:
self._error_getting_log = "Could not parse log entry"
except Exception as ex:
self._error_getting_log = str(ex)
self._work_in_progress = False
def _abbreviate_relative_date(self, relative_date):
relative_date = relative_date.replace(" seconds", "s")
relative_date = relative_date.replace(" second", "s")
relative_date = relative_date.replace(" minutes", "m")
relative_date = relative_date.replace(" minute", "m")
relative_date = relative_date.replace(" hours", "h")
relative_date = relative_date.replace(" hour", "h")
relative_date = relative_date.replace(" days", "d")
relative_date = relative_date.replace(" day", "d")
relative_date = relative_date.replace(" weeks", "w")
relative_date = relative_date.replace(" week", "w")
relative_date = relative_date.replace(" months", "mo")
relative_date = relative_date.replace(" month", "mo")
relative_date = relative_date.replace(" years", "y")
relative_date = relative_date.replace(" year", "y")
return relative_date
def _get_branches_containing_commit(self, commit_hash):
output = check_output(["git", "branch", "-r", "--contains", commit_hash], cwd=self._directory)
output = output.decode("UTF-8")
output = output.replace("origin/", "") # Remove origin parts
lines = output.split("\n")
lines = [line.strip() for line in lines] # Remove whitespace
lines = list(filter(None, lines)) # Remove any empty entries
lines = [line for line in lines if "HEAD ->" not in line] # Remove the HEAD -> entries
return ', '.join(lines)
def _thread_method_fetch(self):
if self.is_submodule() == False:
random_delay()
process = Popen(["git", "fetch"], stdout=PIPE, stderr=PIPE, cwd=self._directory)
results = process.communicate()
if process.returncode != 0:
self._error_fetching = results[1].decode("utf-8")
self._work_in_progress = False
def _thread_method_checkout(self, branch_name):
if self.is_submodule() == False:
process = Popen(["git", "checkout", branch_name], stdout=PIPE, stderr=PIPE, cwd=self._directory)
results = process.communicate()
if process.returncode != 0:
self._error_checking_out = results[1].decode("utf-8")
self._work_in_progress = False
def _thread_method_pull(self):
if self.is_submodule() == False:
random_delay()
process = Popen(["git", "pull", "--recurse-submodules"], stdout=PIPE, stderr=PIPE, cwd=self._directory)
results = process.communicate()
if process.returncode != 0:
self._error_pulling = results[1].decode("utf-8")
self._work_in_progress = False
def _process_status_output(self, output):
lines = output.split("\n")
for line in lines:
if line.startswith("##"):
line = line.replace("## ", "")
if "..." in line:
self._branch = line[: line.find("...")]
else:
self._branch = line
result = re.search("\[.*behind (\d+).*\]", line)
if result:
self._commits_behind = int(result.group(1))
result = re.search("\[.*ahead (\d+).*\]", line)
if result:
self._commits_ahead = int(result.group(1))
if "No commits yet on" in self._branch:
self._branch = self._branch.split()[-1]
else:
parts = line.split()
if len(parts) == 2:
indicator = parts[0]
filename = parts[1]
if "M" in indicator:
self._modified_files.append(filename)
if "??" in indicator:
self._untracked_files.append(filename)
def _process_branch_output(self, output):
lines = output.split("\n")
branches = []
for line in lines:
line = line.strip()
if "origin/HEAD" in line:
continue
indexOfSpace = line.find(" ")
if indexOfSpace > 0:
line = line[:indexOfSpace]
branches.append(line)
branches = set(filter(None, branches)) # Remove any empty entries and make unique
branches = list(branches)
branches.sort()
if self.default_branch() not in branches:
return
for branch in branches:
if branch != self.default_branch():
output = check_output(["git", "rev-list", "--left-right", "--count", f"{self.default_branch()}...{branch}"], cwd=self._directory)
output = output.decode("UTF-8")
parts = output.split()
branch_entry = ComparisonBranchEntry(self.short_name(), branch, parts[0], parts[1])
self._comparison_branch_entries.append(branch_entry)
def get_repositories(parent_directory, git_workers, repo_blocklist, submodule_depth=0):
for path in os.listdir(parent_directory):
abs_path = os.path.join(parent_directory, path)
git_config_path = os.path.join(abs_path, ".git")
if submodule_depth > 0:
if os.path.isfile(git_config_path) and path not in repo_blocklist:
git_workers.append(GitStatusWorker(abs_path, len(git_workers), submodule_depth))
get_repositories(abs_path, git_workers, repo_blocklist, submodule_depth + 1)
elif os.path.isdir(git_config_path):
if path not in repo_blocklist:
git_workers.append(GitStatusWorker(abs_path, len(git_workers), submodule_depth))
get_repositories(abs_path, git_workers, repo_blocklist, submodule_depth + 1)
def upgrade_existing_config():
config_file_path = get_config_file_path()
development_dir = ""
if os.path.exists(config_file_path):
with open(config_file_path, "r") as f:
for line in f:
if line != "":
if "[default]" not in line:
development_dir = line
break
if development_dir != "":
os.remove(config_file_path)
set_config_value(ConfigValue.DEVELOPMENT_DIR, development_dir)
def get_config_value(name):
config_file_path = get_config_file_path()
config = configparser.ConfigParser()
config.read(config_file_path)
if "default" not in config:
return ""
default_section = config["default"]
if name not in default_section:
return ""
return default_section[name]
def set_config_value(name, value):
config_file_path = get_config_file_path()
config = configparser.ConfigParser()
config.read(config_file_path)
if "default" not in config:
config["default"] = {}
config["default"][name] = value
with open(config_file_path, "w") as config_file:
config.write(config_file)
def get_progress_bar_string(progress):
progress_bar_width = 30
bar_characters = [" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"]
progress_bar = "▕" + TerminalStyle.DIM + TerminalStyle.BLUE
total_width = math.floor(progress * progress_bar_width)
remaining_width = (progress * progress_bar_width) % 1
partial_width = math.floor(remaining_width * len(bar_characters))
partial_char = bar_characters[partial_width]
if (progress_bar_width - total_width - 1) < 0:
partial_char = ""
progress_bar += "█" * total_width + partial_char + " " * (progress_bar_width - total_width - 1)
progress_bar += TerminalStyle.CLEAR + "▏"
return progress_bar
def display_progress(action_text):
while True:
workers_busy = sum(1 for worker in git_workers if worker.work_in_progress())
workers_complete = len(git_workers) - workers_busy
progress_bar = get_progress_bar_string(workers_complete / len(git_workers))
progress_line = "\r" + progress_bar + " " + action_text + " (" + str(workers_busy) + " workers still busy) "
sys.stdout.write(progress_line)
if workers_busy == 0:
sys.stdout.write("\r")
sys.stdout.flush()
break
for worker in git_workers:
worker.join()
def pull_all(git_workers):
for worker in git_workers:
worker.pull()
display_progress("Pulling")
def checkout_all(git_workers, branch_name):
for worker in git_workers:
worker.checkout(branch_name)
display_progress("Checking out " + branch_name)
def log_all(git_workers, days_to_log):
for worker in git_workers:
worker.log(days_to_log)
display_progress("Getting logs for last " + str(days_to_log) + " day(s)")
def fetch_all(git_workers):
for worker in git_workers:
worker.fetch()
display_progress("Fetching")
def status_all(git_workers):
for worker in git_workers:
worker.status()
display_progress("Getting status")
def list_branches_all(git_workers):
for worker in git_workers:
worker.list_branches()
display_progress("Getting branches")
def get_config_file_path():
return str(Path.home()) + "/.config/gbt.conf"
def get_development_dir():
development_dir = get_config_value(ConfigValue.DEVELOPMENT_DIR)
if development_dir == "":
show_help()
while development_dir == "":
if development_dir == "":
provided_directory = input("Development directory not set.\nProvide the directory under which your git repositories reside: [" + os.getcwd() + "]:")
if provided_directory == "":
development_dir = os.getcwd()
else:
if os.path.isdir(provided_directory):
development_dir = provided_directory
else:
print("Directory does not exist")
set_config_value(ConfigValue.DEVELOPMENT_DIR, development_dir)
return development_dir
def print_statuses(git_workers):
horizontal_line()
longest_subdirectory_name = 0
longest_branch_name = 0
longest_relative_commits = 0
for worker in git_workers:
if len(worker.display_name()) > longest_subdirectory_name:
longest_subdirectory_name = len(worker.display_name())
if len(worker.branch()) > longest_branch_name:
longest_branch_name = len(worker.branch())
if len(worker.relative_commits()) > longest_relative_commits:
longest_relative_commits = len(worker.relative_commits())
work_to_do = False
gbt_has_update = False
for worker in git_workers:
if worker.error_occurred():
status_string = TerminalStyle.RED + worker.display_name().ljust(longest_subdirectory_name + longest_branch_name + longest_relative_commits + 10)
status_string += "Error "
term_columns, _ = shutil.get_terminal_size((80, 20))
if worker.error_fetching() != "":
error = "fetching: " + worker.error_fetching().replace("\r\n", " ").replace("\n", " ")
status_string += error[:term_columns - len(status_string)]
if worker.error_pulling() != "":
error = "pulling: " + worker.error_pulling().replace("\r\n", " ").replace("\n", " ")
status_string += error[:term_columns - len(status_string)]
if worker.error_checking_out() != "":
error = "checking out branch: " + worker.error_checking_out().replace("\r\n", " ").replace("\n", " ")
status_string += error[:term_columns - len(status_string)]
status_string += TerminalStyle.CLEAR
print(status_string)
else:
name_string = TerminalStyle.DIM if worker.is_submodule() else ""
name_string += worker.display_name().ljust(longest_subdirectory_name)
name_string += TerminalStyle.CLEAR if worker.is_submodule() else ""
relative_commits_string = ""
if worker.commits_behind() > 0:
gbt_has_update = worker.is_gbt_repo()
relative_commits_string += TerminalStyle.YELLOW
relative_commits_string += worker.relative_commits().rjust(longest_relative_commits)
work_to_do = True
elif worker.commits_ahead() > 0:
relative_commits_string += TerminalStyle.GREEN + worker.relative_commits().rjust(longest_relative_commits)
work_to_do = True
else:
relative_commits_string += worker.relative_commits().rjust(longest_relative_commits)
relative_commits_string += TerminalStyle.CLEAR
branch_string = worker.branch().ljust(longest_branch_name)
if worker.branch() != worker.default_branch() and worker.branch() != "HEAD (no branch)":
branch_string = TerminalStyle.BLUE + worker.branch().ljust(longest_branch_name) + TerminalStyle.CLEAR
status_string = ""
if len(worker.modified_files()) + len(worker.untracked_files()) == 0:
status_string += "Nothing to commit"
else:
work_to_do = True
status_string += TerminalStyle.YELLOW
status_string += str(len(worker.modified_files()) + len(worker.untracked_files())) + " file(s) modified/untracked"
status_string += TerminalStyle.CLEAR
print(
name_string
+ OutputFormat.COLUMN_SEPARATOR_FORMATTED
+ relative_commits_string
+ TerminalStyle.DIM
+ " on "
+ TerminalStyle.CLEAR
+ branch_string
+ OutputFormat.COLUMN_SEPARATOR_FORMATTED
+ status_string
)
horizontal_line()
return work_to_do, gbt_has_update
def print_branch_comparisons(git_workers):
horizontal_line()
repos_branch_pairs = []
all_comparison_branches = []
for worker in git_workers:
for branch_entry in worker.comparison_branch_entries():
if branch_entry.repo() + branch_entry.branch_for_comparison() not in repos_branch_pairs:
all_comparison_branches.append(branch_entry)
repos_branch_pairs.append(branch_entry.repo() + branch_entry.branch_for_comparison())
all_comparison_branches.sort(key=lambda x: x.repo())
longest_repo = 0
longest_branch_for_comparison = 0
longest_commits_behind = 0
longest_commits_ahead = 0
for entry in all_comparison_branches:
if len(entry.repo()) > longest_repo:
longest_repo = len(entry.repo())
if len(entry.branch_for_comparison()) > longest_branch_for_comparison:
longest_branch_for_comparison = len(entry.branch_for_comparison())
if len(entry.commits_behind()) > longest_commits_behind:
longest_commits_behind = len(entry.commits_behind())
if len(entry.commits_ahead()) > longest_commits_ahead:
longest_commits_ahead = len(entry.commits_ahead())
for entry in all_comparison_branches:
if entry.commits_ahead() != "0":
repo_string = entry.repo().ljust(longest_repo)
branch_string = entry.branch_for_comparison().ljust(longest_branch_for_comparison)
commits_behind_string = entry.commits_behind().ljust(longest_commits_behind)
commits_ahead_string = entry.commits_ahead().ljust(longest_commits_ahead)
output_string = repo_string + OutputFormat.COLUMN_SEPARATOR_FORMATTED + branch_string + OutputFormat.COLUMN_SEPARATOR_FORMATTED
if entry.commits_behind() != "0":
output_string += TerminalStyle.BLUE
output_string += "-"
else:
output_string += " "
output_string += commits_behind_string
if entry.commits_behind() != "0":
output_string += TerminalStyle.CLEAR
output_string += OutputFormat.COLUMN_SEPARATOR_FORMATTED
if entry.commits_ahead() != "0":
output_string += TerminalStyle.YELLOW
output_string += "+"
else:
output_string += " "
output_string += commits_ahead_string
if entry.commits_ahead() != "0":
output_string += TerminalStyle.CLEAR
output_string += OutputFormat.COLUMN_SEPARATOR_FORMATTED
print(output_string)
horizontal_line()
def print_logs(git_workers):
horizontal_line()
all_log_entries = []
for worker in git_workers:
all_log_entries.extend(worker.log_entries())
all_log_entries.sort(key=lambda x: x.timestamp(), reverse=True)
longest_date = 0
longest_repo = 0
longest_author = 0
longest_branch_list = 0
for entry in all_log_entries:
if len(entry.relative_date()) > longest_date:
longest_date = len(entry.relative_date())
if len(entry.repo()) > longest_repo:
longest_repo = len(entry.repo())
if len(entry.author()) > longest_author:
longest_author = len(entry.author())
if len(entry.branch_list()) > longest_branch_list:
longest_branch_list = len(entry.branch_list())
term_columns, term_lines = shutil.get_terminal_size((120, 20))
for entry in all_log_entries:
repo_string = entry.repo().ljust(longest_repo)
date_string = entry.relative_date().ljust(longest_date)
author_string = entry.author().ljust(longest_author)
branch_list_string = entry.branch_list().ljust(longest_branch_list)
meta_data_string_clean = repo_string + author_string + date_string + branch_list_string
meta_data_string = repo_string + OutputFormat.COLUMN_SEPARATOR_FORMATTED + author_string + OutputFormat.COLUMN_SEPARATOR_FORMATTED + date_string + OutputFormat.COLUMN_SEPARATOR_FORMATTED + TerminalStyle.BLUE + branch_list_string + TerminalStyle.CLEAR + OutputFormat.COLUMN_SEPARATOR_FORMATTED
space_remaining = term_columns - len(meta_data_string_clean) - (len(OutputFormat.COLUMN_SEPARATOR) * 4)
message = entry.message()
message_string = (message[: space_remaining - 3] + "...") if len(message) > space_remaining else message
print(meta_data_string + TerminalStyle.YELLOW + message_string + TerminalStyle.CLEAR)
horizontal_line()
def horizontal_line():
term_columns, term_lines = shutil.get_terminal_size((120, 20))
print(TerminalStyle.DIM + "─" * term_columns + TerminalStyle.CLEAR)
def create_workers():
repo_blocklist = get_config_value(ConfigValue.REPO_BLOCKLIST)
repo_blocklist = repo_blocklist.split(",")
git_workers = []
get_repositories(development_dir, git_workers, repo_blocklist)
git_workers.sort(key=lambda x: x.directory())
return git_workers
def random_delay():
sleep(random() * 10)
def show_help():
print(TerminalStyle.YELLOW + "Git Bulk Toolkit (gbt) by Ben Pring" + TerminalStyle.CLEAR)
print("")
print("gbt status")
print(" - Show status for all repositories under development directory")
print("")
print("gbt fetch")
print(" - Run 'git fetch' on all repositories under development directory")
print("")
print("gbt pull")
print(" - Run 'git pull --recurse-submodules' on all repositories under development directory")
print("")
print("gbt fetch status")
print(" - Run 'git fetch' on all repositories under development directory, and show status")
print("")
print("gbt pull status")
print(" - Run 'git pull' on all repositories under development directory, and show status")
print("")
print("gbt checkout <branch_name>")
print(" - Run 'git checkout <branch_name>' on all repositories under development directory")
print("")
print("gbt log [<days_to_log>]")
print(" - Run 'git log' on all repositories under development directory, and display <days_to_log> worth of commits (default 7)")
print("")
# Start program
try:
args = sys.argv[1:]
fetch = "fetch" in args
log = "log" in args
pull = "pull" in args
status = "status" in args
checkout = "checkout" in args
branches = "branches" in args
help = "help" in args or "--help" in args
branch_name = None
days_to_log = 7
if help:
show_help()
exit(0)
if pull and fetch:
print("fetch and pull are incompatible")
exit(1)
if checkout and (pull or fetch or status or log or branches):
print("checkout is not compatible with other commands")
exit(1)
if log and (pull or fetch or status or checkout or branches):
print("log is not compatible with other commands")
exit(1)
if branches and (status or checkout):
print("branches is not compatible with status or checkout")
exit(1)
# Check that there's a branch param for checkout
if checkout:
if len(args) == 2:
branch_name = args[1]
else:
print("checkout requires a branch name")
exit(1)
# Get the optional days to log param
if log:
if len(args) == 2:
days_to_log = int(args[1])
# Set the defaults
if (fetch or pull or status or checkout or log or branches) is False:
fetch = True
status = True
# Upgrade any old config file
upgrade_existing_config()
# Get the configured development directory, or ask for it
development_dir = get_development_dir()
print("Working on [ " + development_dir + " ]")
# Do the work