Coverage for jaypore_ci/repos/git.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-30 09:04 +0000

1import subprocess 

2from typing import List 

3 

4from jaypore_ci.interfaces import Repo 

5 

6 

7class Git(Repo): 

8 def files_changed(self, target: str) -> List[str]: 

9 "Returns list of files changed between current sha and target" 

10 return ( 

11 subprocess.check_output( 

12 f"git diff --name-only {target} {self.sha}", shell=True 

13 ) 

14 .decode() 

15 .strip() 

16 .split("\n") 

17 ) 

18 

19 @classmethod 

20 def from_env(cls) -> "Git": 

21 """ 

22 Gets repo status from the environment and git repo on disk. 

23 """ 

24 remote = ( 

25 subprocess.check_output( 

26 "git remote -v | grep push | head -n1 | grep https | awk '{print $2}'", 

27 shell=True, 

28 ) 

29 .decode() 

30 .strip() 

31 ) 

32 assert "https://" in remote, f"Only https remotes supported: {remote}" 

33 assert ".git" in remote 

34 # NOTE: Later on perhaps we should support non-https remotes as well 

35 # since JCI does not actually do anything with the remote. 

36 branch = ( 

37 subprocess.check_output( 

38 r"git branch | grep \* | awk '{print $2}'", shell=True 

39 ) 

40 .decode() 

41 .strip() 

42 ) 

43 sha = subprocess.check_output("git rev-parse HEAD", shell=True).decode().strip() 

44 message = ( 

45 subprocess.check_output("git log -1 --pretty=%B", shell=True) 

46 .decode() 

47 .strip() 

48 ) 

49 return cls(sha=sha, branch=branch, remote=remote, commit_message=message)