Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9import os 

10from pathlib import Path 

11 

12from tsfpga.system_utils import file_is_in_directory 

13 

14 

15def get_git_commit(directory): 

16 """ 

17 Generally, eight to ten characters are more than enough to be unique within a project. 

18 The linux kernel, one of the largest projects, needs 11. 

19 https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Short-SHA-1 

20 """ 

21 sha_length = 16 

22 if "GIT_COMMIT" in os.environ: 

23 return os.environ["GIT_COMMIT"][0:sha_length] 

24 

25 # Import fails if "git" executable is not available, hence it can not be on top level. 

26 # This function should only be called if git is available. 

27 # pylint: disable=import-outside-toplevel 

28 from git import Repo 

29 

30 repo = Repo(directory, search_parent_directories=True) 

31 git_commit = repo.head.commit.hexsha[0:sha_length] 

32 if repo.is_dirty(): 

33 git_commit += " (local changes present)" 

34 

35 return git_commit 

36 

37 

38def git_commands_are_available(directory): 

39 """ 

40 True if "git" command executable is available, and ``directory`` is in a valid git repo. 

41 """ 

42 try: 

43 # pylint: disable=import-outside-toplevel 

44 from git import Repo, InvalidGitRepositoryError 

45 except ImportError: 

46 return False 

47 

48 try: 

49 Repo(directory, search_parent_directories=True) 

50 except InvalidGitRepositoryError: 

51 return False 

52 

53 return True 

54 

55 

56def find_git_files( 

57 directory, 

58 exclude_directories=None, 

59 file_endings_include=None, 

60 file_endings_avoid=None, 

61): 

62 """ 

63 Find files that are checked in to git. 

64 

65 Arguments: 

66 directory (`pathlib.Path`): Search in this directory. 

67 exclude_directories (list(`pathlib.Path`)): Files in these directories will not be included. 

68 file_endings_include (str or tuple(str)). Only files with these endings will be included. 

69 file_endings_avoid (str or tuple(str)): String or tuple of strings. Files with these endings 

70 will not be included. 

71 

72 Returns: 

73 list(`pathlib.Path`): The files that are available in git. 

74 """ 

75 

76 # Import fails if "git" executable is not available, hence it can not be on top level. 

77 # This function should only be called if git is available. 

78 # pylint: disable=import-outside-toplevel 

79 from git import Repo 

80 

81 exclude_directories = ( 

82 [] 

83 if exclude_directories is None 

84 else [exclude_directory.resolve() for exclude_directory in exclude_directories] 

85 ) 

86 

87 def list_paths(root_tree, path): 

88 for blob in root_tree.blobs: 

89 yield path / blob.name 

90 for tree in root_tree.trees: 

91 yield from list_paths(tree, path / tree.name) 

92 

93 repo = Repo(directory, search_parent_directories=True) 

94 repo_root = Path(repo.git_dir).parent.resolve() 

95 

96 for file_path in list_paths(repo.tree(), repo_root): 

97 if file_endings_include is not None and not file_path.name.endswith(file_endings_include): 

98 continue 

99 

100 if file_endings_avoid is not None and file_path.name.endswith(file_endings_avoid): 

101 continue 

102 

103 if file_is_in_directory(file_path, exclude_directories): 

104 continue 

105 

106 if file_is_in_directory(file_path, [directory]): 

107 yield file_path