-
Notifications
You must be signed in to change notification settings - Fork 69
/
git_util.py
836 lines (723 loc) · 31.6 KB
/
git_util.py
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
import os
import re
from pathlib import Path
from typing import Optional, Set, Tuple, Union
import click
import gitdb
from git import (
InvalidGitRepositoryError,
Repo, # noqa: TID251: required to create GitUtil
)
from git.diff import Lit_change_type
from git.remote import Remote
from demisto_sdk.commands.common.constants import PACKS_FOLDER
class GitUtil:
# in order to use Repo class/static methods
REPO_CLS = Repo
def __init__(
self,
path: Optional[Path] = None,
search_parent_directories: bool = True,
):
if isinstance(path, str):
repo_path = Path(path)
else:
repo_path = path or Path.cwd()
try:
self.repo = Repo(
repo_path, search_parent_directories=search_parent_directories
)
except InvalidGitRepositoryError:
raise InvalidGitRepositoryError(
f"Unable to find Repository from current {repo_path.absolute()} - aborting"
)
def get_all_files(self) -> Set[Path]:
return set(map(Path, self.repo.git.ls_files().split("\n")))
def modified_files(
self,
prev_ver: str = "",
committed_only: bool = False,
staged_only: bool = False,
debug: bool = False,
include_untracked: bool = False,
) -> Set[Path]:
"""Gets all the files that are recognized by git as modified against the prev_ver.
Args:
prev_ver (str): The base branch against which the comparison is made.
committed_only (bool): Whether to return only committed files.
staged_only (bool): Whether to return only staged files.
debug (bool): Whether to print the debug logs.
include_untracked (bool): Whether to include untracked files.
Returns:
Set: A set of Paths to the modified files.
"""
remote, branch = self.handle_prev_ver(prev_ver)
current_branch_or_hash = self.get_current_git_branch_or_hash()
# when checking branch against itself only return the last commit.
last_commit = self._only_last_commit(prev_ver, requested_status="M")
if last_commit:
self.debug_print(
debug=debug, status="Modified", staged=set(), committed=last_commit
)
return last_commit
# get all renamed files - some of these can be identified as modified by git,
# but we want to identify them as renamed - so will remove them from the returned files.
renamed = {item[0] for item in self.renamed_files(prev_ver, committed_only, staged_only)} # type: ignore[index]
# handle a case where a file is wrongly recognized as renamed (not 100% score) and
# is actually of modified status
untrue_rename_staged = self.handle_wrong_renamed_status(
status="M", remote=remote, branch=branch, staged_only=True
)
untrue_rename_committed = self.handle_wrong_renamed_status(
status="M", remote=remote, branch=branch, staged_only=False
)
deleted = self.deleted_files(prev_ver, committed_only, staged_only)
committed = set()
if not staged_only:
# get all committed files identified as modified which are changed from prev_ver.
# this can result in extra files identified which were not touched on this branch.
if remote:
committed = {
Path(os.path.join(item.a_path))
for item in self.repo.remote(name=remote)
.refs[branch]
.commit.diff(current_branch_or_hash)
.iter_change_type("M")
}.union(untrue_rename_committed)
# if remote does not exist we are checking against the commit sha1
else:
committed = {
Path(os.path.join(item.a_path))
for item in self.repo.commit(rev=branch)
.diff(current_branch_or_hash)
.iter_change_type("M")
}.union(untrue_rename_committed)
# identify all files that were touched on this branch regardless of status
# intersect these with all the committed files to identify the committed modified files.
all_branch_changed_files = self._get_all_changed_files(prev_ver)
committed = committed.intersection(all_branch_changed_files)
# remove the renamed and deleted files from the committed
committed = committed - renamed - deleted
if committed_only:
self.debug_print(
debug=debug, status="Modified", staged=set(), committed=committed
)
return committed
untracked: Set = set()
if include_untracked:
# get all untracked modified files
untracked = self._get_untracked_files("M")
# get all the files that are staged on the branch and identified as modified.
staged = (
{
Path(os.path.join(item.a_path))
for item in self.repo.head.commit.diff().iter_change_type("M")
}
.union(untracked)
.union(untrue_rename_staged)
)
# If a file is Added in regards to prev_ver
# and is then modified locally after being committed - it is identified as modified
# but we want to identify the file as Added (its actual status against prev_ver) -
# so will remove it from the staged modified files.
# also remove the deleted and renamed files as well.
if remote:
committed_added = {
Path(os.path.join(item.a_path))
for item in self.repo.remote(name=remote)
.refs[branch]
.commit.diff(current_branch_or_hash)
.iter_change_type("A")
}
# if remote does not exist we are checking against the commit sha1
else:
committed_added = {
Path(os.path.join(item.a_path))
for item in self.repo.commit(rev=branch)
.diff(current_branch_or_hash)
.iter_change_type("A")
}
staged = staged - committed_added - renamed - deleted
if staged_only:
self.debug_print(
debug=debug, status="Modified", staged=staged, committed=set()
)
return staged
self.debug_print(
debug=debug, status="Modified", staged=staged, committed=committed
)
return staged.union(committed)
def added_files(
self,
prev_ver: str = "",
committed_only: bool = False,
staged_only: bool = False,
debug: bool = False,
include_untracked: bool = False,
) -> Set[Path]:
"""Gets all the files that are recognized by git as added against the prev_ver.
Args:
prev_ver (str): The base branch against which the comparison is made.
committed_only (bool): Whether to return only committed files.
staged_only (bool): Whether to return only staged files.
debug (bool): Whether to print the debug logs.
include_untracked (bool): Whether to include untracked files.
Returns:
Set: A set of Paths to the added files.
"""
remote, branch = self.handle_prev_ver(prev_ver)
current_branch_or_hash = self.get_current_git_branch_or_hash()
# when checking branch against itself only return the last commit.
last_commit = self._only_last_commit(prev_ver, requested_status="A")
if last_commit:
self.debug_print(
debug=debug, status="Added", staged=set(), committed=last_commit
)
return last_commit
deleted = self.deleted_files(prev_ver, committed_only, staged_only)
# handle a case where a file is wrongly recognized as renamed (not 100% score) and is actually of added status
untrue_rename_staged = self.handle_wrong_renamed_status(
status="A", remote=remote, branch=branch, staged_only=True
)
untrue_rename_committed = self.handle_wrong_renamed_status(
status="A", remote=remote, branch=branch, staged_only=False
)
# get all committed files identified as added which are changed from prev_ver.
# this can result in extra files identified which were not touched on this branch.
if remote:
committed = {
Path(os.path.join(item.a_path))
for item in self.repo.remote(name=remote)
.refs[branch]
.commit.diff(current_branch_or_hash)
.iter_change_type("A")
}.union(untrue_rename_committed)
# if remote does not exist we are checking against the commit sha1
else:
committed = {
Path(os.path.join(item.a_path))
for item in self.repo.commit(rev=branch)
.diff(current_branch_or_hash)
.iter_change_type("A")
}.union(untrue_rename_committed)
# identify all files that were touched on this branch regardless of status
# intersect these with all the committed files to identify the committed added files.
all_branch_changed_files = self._get_all_changed_files(prev_ver)
committed = committed.intersection(all_branch_changed_files)
# remove deleted files
committed = committed - deleted
if committed_only:
self.debug_print(
debug=debug, status="Added", staged=set(), committed=committed
)
return committed
untracked_added: Set = set()
untracked_modified: Set = set()
if include_untracked:
# get all untracked added files
untracked_added = self._get_untracked_files("A")
# get all untracked modified files
untracked_modified = self._get_untracked_files("M")
# get all the files that are staged on the branch and identified as added.
staged = {
Path(os.path.join(item.a_path))
for item in self.repo.head.commit.diff().iter_change_type("A")
}.union(untrue_rename_staged)
# If a file is Added in regards to prev_ver
# and is then modified locally after being committed - it is identified as modified
# but we want to identify the file as Added (its actual status against prev_ver) -
# so will added it from the staged added files.
# same goes to untracked files - can be identified as modified but are actually added against prev_ver
committed_added_locally_modified = {
Path(os.path.join(item.a_path))
for item in self.repo.head.commit.diff().iter_change_type("M")
}.intersection(committed)
untracked = untracked_added.union(untracked_modified.intersection(committed))
staged = staged.union(committed_added_locally_modified).union(untracked)
# remove deleted files.
staged = staged - deleted
if staged_only:
self.debug_print(
debug=debug, status="Added", staged=staged, committed=set()
)
return staged
self.debug_print(
debug=debug, status="Added", staged=staged, committed=committed
)
return staged.union(committed)
def deleted_files(
self,
prev_ver: str = "",
committed_only: bool = False,
staged_only: bool = False,
include_untracked: bool = False,
) -> Set[Path]:
"""Gets all the files that are recognized by git as deleted against the prev_ver.
Args:
prev_ver (str): The base branch against which the comparison is made.
committed_only (bool): Whether to return only committed files.
staged_only (bool): Whether to return only staged files.
include_untracked (bool): Whether to include untracked files.
Returns:
Set: A set of Paths to the deleted files.
"""
remote, branch = self.handle_prev_ver(prev_ver)
current_branch_or_hash = self.get_current_git_branch_or_hash()
# when checking branch against itself only return the last commit.
last_commit = self._only_last_commit(prev_ver, requested_status="D")
if last_commit:
return last_commit
committed = set()
if not staged_only:
# get all committed files identified as added which are changed from prev_ver.
# this can result in extra files identified which were not touched on this branch.
if remote:
committed = {
Path(os.path.join(item.a_path))
for item in self.repo.remote(name=remote)
.refs[branch]
.commit.diff(current_branch_or_hash)
.iter_change_type("D")
}
# if remote does not exist we are checking against the commit sha1
else:
committed = {
Path(os.path.join(item.a_path))
for item in self.repo.commit(rev=branch)
.diff(current_branch_or_hash)
.iter_change_type("D")
}
# identify all files that were touched on this branch regardless of status
# intersect these with all the committed files to identify the committed added files.
all_branch_changed_files = self._get_all_changed_files(prev_ver)
committed = committed.intersection(all_branch_changed_files)
if committed_only:
return committed
untracked: Set = set()
if include_untracked:
# get all untracked deleted files
untracked = self._get_untracked_files("D")
# get all the files that are staged on the branch and identified as added.
staged = {
Path(os.path.join(item.a_path))
for item in self.repo.head.commit.diff().iter_change_type("D")
}.union(untracked)
if staged_only:
return staged
return staged.union(committed)
def renamed_files(
self,
prev_ver: str = "",
committed_only: bool = False,
staged_only: bool = False,
debug: bool = False,
include_untracked: bool = False,
get_only_current_file_names: bool = False,
) -> Union[Set[Tuple[Path, Path]], Set[Path]]:
"""Gets all the files that are recognized by git as renamed against the prev_ver.
Args:
prev_ver (str): The base branch against which the comparison is made.
committed_only (bool): Whether to return only committed files.
staged_only (bool): Whether to return only staged files.
debug (bool): Whether to print the debug logs.
include_untracked (bool): Whether to include untracked files.
get_only_current_file_names (bool): Whether to get only the current file names and not the old file names.
Returns:
Set: A set of Tuples of Paths to the renamed files -
first element being the old file path and the second is the new.
"""
remote, branch = self.handle_prev_ver(prev_ver)
current_branch_or_hash = self.get_current_git_branch_or_hash()
# when checking branch against itself only return the last commit.
last_commit = self._only_last_commit(prev_ver, requested_status="R")
if last_commit:
self.debug_print(
debug=debug, status="Renamed", staged=set(), committed=last_commit
)
return last_commit
deleted = self.deleted_files(prev_ver, committed_only, staged_only)
committed = set()
if not staged_only:
# get all committed files identified as renamed which are changed from prev_ver and are with 100% score.
# this can result in extra files identified which were not touched on this branch.
if remote:
committed = {
(Path(item.a_path), Path(item.b_path))
for item in self.repo.remote(name=remote)
.refs[branch]
.commit.diff(current_branch_or_hash)
.iter_change_type("R")
if item.score == 100
}
# if remote does not exist we are checking against the commit sha1
else:
committed = {
(Path(item.a_path), Path(item.b_path))
for item in self.repo.commit(rev=branch)
.diff(current_branch_or_hash)
.iter_change_type("R")
if item.score == 100
}
# identify all files that were touched on this branch regardless of status
# intersect these with all the committed files to identify the committed added files.
all_branch_changed_files = self._get_all_changed_files(prev_ver)
committed = {
tuple_item
for tuple_item in committed
if (
tuple_item[1] in all_branch_changed_files
and tuple_item[1] not in deleted
)
}
if committed_only:
self.debug_print(
debug=debug, status="Renamed", staged=set(), committed=committed
)
if get_only_current_file_names:
committed_only_new = {file[1] for file in committed}
return committed_only_new
return committed
untracked: Set = set()
if include_untracked:
# get all untracked renamed files
untracked = self._get_untracked_files("R")
# get all the files that are staged on the branch and identified as renamed and are with 100% score.
staged = {
(Path(item.a_path), Path(item.b_path))
for item in self.repo.head.commit.diff().iter_change_type("R")
if item.score == 100
}.union(untracked)
if staged_only:
self.debug_print(
debug=debug, status="Renamed", staged=staged, committed=set()
)
if get_only_current_file_names:
staged_only_new = {file[1] for file in staged}
return staged_only_new
return staged
self.debug_print(
debug=debug, status="Renamed", staged=staged, committed=committed
)
all_renamed_files = staged.union(committed)
if get_only_current_file_names:
all_renamed_files_only_new = {file[1] for file in all_renamed_files}
return all_renamed_files_only_new
return all_renamed_files
def get_all_changed_pack_ids(self, prev_ver: str) -> Set[str]:
return {
file.parts[1]
for file in self.get_all_changed_files(prev_ver)
if file.parts[0] == PACKS_FOLDER
}
def _get_untracked_files(self, requested_status: str) -> set:
"""return all untracked files of the given requested status.
Args:
requested_status (str): M, A, R, D - the git status to return
Returns:
Set: of path strings which include the untracked files of a certain status.
"""
git_status = self.repo.git.status("--short", "-u").split("\n")
# in case there are no local changes - return
if git_status == [""]:
return set()
extracted_paths = set()
for line in git_status:
line = line.strip()
file_status = line.split()[0].upper() if not line.startswith("?") else "A"
if file_status.startswith(requested_status):
if requested_status == "R":
if file_status == "R100":
extracted_paths.add(
(Path(line.split()[-2]), Path(line.split()[-1]))
)
else:
extracted_paths.add(Path(line.split()[-1])) # type: ignore
return extracted_paths
def _get_staged_files(self) -> Set[Path]:
"""Get only staged files
Returns:
Set[Path]: The staged files to return
"""
return {
Path(item)
for item in self.repo.git.diff("--cached", "--name-only").split("\n")
if item
}
def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]:
"""Get all the files changed in the current branch without status distinction.
Args:
prev_ver (str): The base branch against which the comparison is made.
Returns:
Set: of Paths to files changed in the current branch.
"""
remote, branch = self.handle_prev_ver(prev_ver)
current_hash = self.get_current_commit_hash()
if remote:
return {
Path(os.path.join(item))
for item in self.repo.git.diff(
"--name-only", f"{remote}/{branch}...{current_hash}"
).split("\n")
if item
}
# if remote does not exist we are checking against the commit sha1
else:
return {
Path(os.path.join(item))
for item in self.repo.git.diff(
"--name-only", f"{branch}...{current_hash}"
).split("\n")
if item
}
def _only_last_commit(
self, prev_ver: str, requested_status: Lit_change_type
) -> Set: # pragma: no cover
"""Get all the files that were changed in the last commit of a given type when checking a branch against itself.
Args:
prev_ver (str): The base branch against which the comparison is made.
requested_status (Lit_change_type): M(odified), A(dded), R(enamed), D(eleted) - the git status to return
Returns:
Set: of Paths to files changed in the the last commit or an empty set if not
running on master against master.
"""
# when checking branch against itself only return the last commit.
if self.get_current_git_branch_or_hash() != self.handle_prev_ver(prev_ver)[1]:
return set()
try:
if requested_status != "R":
return {
Path(os.path.join(item.a_path))
for item in self.repo.commit("HEAD~1")
.diff()
.iter_change_type(requested_status)
if item.score == 100
}
else:
return {
(Path(item.a_path), Path(item.b_path))
for item in self.repo.commit("HEAD~1")
.diff()
.iter_change_type(requested_status)
}
except gitdb.exc.BadName:
# in case no last commit exists - just pass
pass
return set()
def check_if_remote_exists(self, remote):
if "/" in remote:
remote = remote.split("/")[0]
return remote in self.repo.remotes
@staticmethod
def find_primary_branch(repo: Repo) -> str:
# Try to get the main branch
if not repo:
# Null input
return ""
if not hasattr(repo, "remotes"):
# No remotes
return ""
for current_remote in repo.remotes:
# No refs in this remote
if not hasattr(current_remote, "refs"):
return ""
for current_remote_ref in current_remote.refs:
current_remote_ref_str = str(current_remote_ref)
if current_remote_ref_str == "origin/main":
return "main"
elif current_remote_ref_str == "origin/master":
return "master"
return ""
def handle_prev_ver(self, prev_ver: str = ""):
# check for sha1 in regex
sha1_pattern = re.compile(r"\b[0-9a-f]{40}\b", flags=re.IGNORECASE)
if prev_ver and sha1_pattern.match(prev_ver):
return None, prev_ver
if prev_ver and "/" in prev_ver:
remote, branch = prev_ver.split("/", 1)
remote = (
remote
if self.check_if_remote_exists(remote)
else str(self.repo.remote())
)
else:
remote = str(self.repo.remote())
branch = ""
if prev_ver:
branch = prev_ver
else:
branch = self.find_primary_branch(repo=self.repo)
if not branch:
raise Exception(
"Unable to find main or master branch from current working directory - aborting."
)
return remote, branch
def get_current_git_branch_or_hash(self) -> str:
try:
return self.get_current_working_branch()
except TypeError:
return self.get_current_commit_hash()
def get_current_working_branch(self) -> str:
return str(self.repo.active_branch)
def get_current_commit_hash(self) -> str:
return str(self.repo.head.object.hexsha)
def git_path(self) -> str:
git_path = self.repo.git.rev_parse("--show-toplevel")
return git_path.replace("\n", "")
def debug_print(
self, debug: bool, status: str, staged: Set, committed: Set
) -> None:
if debug:
click.echo(f"######## - {status} staged:")
click.echo(staged)
click.echo("\n")
click.echo(f"######## - {status} committed:")
click.echo(committed)
click.echo("\n")
def handle_wrong_renamed_status(
self, status: str, remote: str, branch: str, staged_only: bool
) -> Set[Path]:
"""Get all the files that are recognized as non-100% rename in a given file status.
Args:
status (str): the requested file status
remote (str): the used git remote
branch (str): the used git branch
staged_only (bool): whether to bring only staged files
Returns:
Set: of Paths to non 100% renamed files which are of a given status.
"""
current_branch_or_hash = self.get_current_git_branch_or_hash()
if staged_only:
return {
Path(item.b_path)
for item in self.repo.head.commit.diff().iter_change_type("R")
if item.score < 100
and self._check_file_status(
file_path=str(item.b_path), remote=remote, branch=branch
)
== status
}
if remote:
return {
Path(item.b_path)
for item in self.repo.remote(name=remote)
.refs[branch]
.commit.diff(current_branch_or_hash)
.iter_change_type("R")
if item.score < 100
and self._check_file_status(
file_path=str(item.b_path), remote=remote, branch=branch
)
== status
}
# if remote does not exist we are checking against the commit sha1
return {
Path(item.b_path)
for item in self.repo.commit(rev=branch)
.diff(current_branch_or_hash)
.iter_change_type("R")
if item.score < 100
and self._check_file_status(
file_path=str(item.b_path), remote=remote, branch=branch
)
== status
}
def _check_file_status(self, file_path: str, remote: str, branch: str) -> str:
"""Get the git status of a given file path
Args:
file_path (str): the file path to check
remote (str): the used git remote
branch (str): the used git branch
Returns:
str: the git status of the file (M, A, R, D).
"""
current_branch_or_hash = self.get_current_git_branch_or_hash()
if remote:
diff_line = self.repo.git.diff(
"--name-status",
f"{remote}/{branch}...{current_branch_or_hash}",
"--",
file_path,
)
# if remote does not exist we are checking against the commit sha1
else:
diff_line = self.repo.git.diff(
"--name-status", f"{branch}...{current_branch_or_hash}", "--", file_path
)
if not diff_line:
return ""
return diff_line.split()[0].upper() if not diff_line.startswith("?") else "A"
def get_local_remote_file_content(self, git_file_path: str) -> str:
"""Get local file content from remote branch. For example get origin/master:README.md
Args:
git_file_path: The git file path. For example get origin/master:README.md
Returns:
The fetched file content.
"""
file_content = self.repo.git.show(git_file_path)
return file_content
def get_local_remote_file_path(self, full_file_path: str, tag: str) -> str:
"""Get local file path of remote branch. For example get origin/master:README.md
Args:
full_file_path: The file path to fetch. For example 'content/README.md'
tag: The tag of the branch. For example 'master'
Returns:
The git file path. For example get origin/master:README.md
"""
relative_file_path = os.path.relpath(full_file_path, self.git_path())
try:
remote_name: Union[Remote, str] = self.repo.remote()
except ValueError as exc:
if "Remote named 'origin' didn't exist" in str(exc):
remote_name = "origin"
else:
raise exc
return f"{remote_name}/{tag}:{relative_file_path}"
def get_all_changed_files(
self,
prev_ver: str = "",
committed_only: bool = False,
staged_only: bool = False,
debug: bool = False,
include_untracked: bool = False,
) -> Set[Path]:
"""Get a set of all changed files in the branch (modified, added and renamed)
Args:
prev_ver (str): The base branch against which the comparison is made.
committed_only (bool): Whether to return only committed files.
staged_only (bool): Whether to return only staged files.
debug (bool): Whether to print the debug logs.
include_untracked (bool): Whether to include untracked files.
Returns:
Set. A set of all the changed files in the given branch when comparing to prev_ver
"""
modified_files: Set[Path] = self.modified_files(
prev_ver=prev_ver,
committed_only=committed_only,
staged_only=staged_only,
debug=debug,
include_untracked=include_untracked,
)
added_files: Set[Path] = self.added_files(
prev_ver=prev_ver,
committed_only=committed_only,
staged_only=staged_only,
debug=debug,
include_untracked=include_untracked,
)
renamed_files: Set[Path] = self.renamed_files(
prev_ver=prev_ver,
committed_only=committed_only, # type: ignore[assignment]
staged_only=staged_only,
debug=debug,
include_untracked=include_untracked,
get_only_current_file_names=True,
)
return modified_files.union(added_files).union(renamed_files)
def _is_file_git_ignored(self, file_path: str) -> bool:
"""return wether the file is in .gitignore file or not.
Args:
file_path (str): the file to check.
Returns:
bool: True if the file is ignored. Otherwise, return False.
"""
return bool(self.repo.ignored(file_path))