Coverage for pyquickhelper/loghelper/pyrepo_helper.py: 90%

29 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief a repository class independent from the repository system (it will be guessed) 

5""" 

6from .repositories import pysvn_helper as SVN 

7from .repositories import pygit_helper as GIT 

8 

9 

10class SourceRepository: 

11 

12 """ 

13 Proposes the same functionality independent from the 

14 source chosen repository (GIT or SVN). 

15 

16 On Windows, it might help to install either 

17 :epkg:`TortoiseSVN` or the :epkg:`GitHub` application. 

18 """ 

19 

20 def __init__(self, commandline=True): 

21 """ 

22 @param commandline use command line or a specific module (like pysvn for example) 

23 """ 

24 self.commandline = commandline 

25 self.module = None 

26 

27 def SetGuessedType(self, location): 

28 """ 

29 Guesses the repository type given a location 

30 and changes a member of the class. 

31 

32 @param location location 

33 @return module to use 

34 """ 

35 git = GIT.IsRepo(location, commandline=self.commandline) 

36 if not git: # pragma: no cover 

37 svn = SVN.IsRepo(location, commandline=self.commandline) 

38 if not svn: 

39 try: 

40 GIT.get_repo_version( 

41 location, commandline=self.commandline, log=False) 

42 self.module = GIT 

43 except Exception as e: 

44 raise RuntimeError( 

45 f"Unable to guess source repository type for location '{location}'. Error: '{e}'.") 

46 else: 

47 self.module = SVN 

48 else: 

49 self.module = GIT 

50 return self.module 

51 

52 def ls(self, path): 

53 """ 

54 Extracts the content of a location. 

55 

56 @param path path 

57 @return a list 

58 """ 

59 if self.module is None: 

60 self.SetGuessedType(path) 

61 return self.module.repo_ls(path, commandline=self.commandline) 

62 

63 def log(self, path=None, file_detail=False): 

64 """ 

65 Gets the latest changes operated on a file in a folder or a subfolder. 

66 

67 @param path path to look 

68 @param file_detail if True, add impacted files 

69 @return list of changes, each change is a list of tuples: 

70 (author, change number (int), date (datetime), 

71 comment, full hash, link) 

72 

73 The function uses a command line if an error occurred. 

74 It uses the xml format: 

75 

76 :: 

77 

78 <logentry revision="161"> 

79 <author>xavier dupre</author> 

80 <date>2013-03-23T15:02:50.311828Z</date> 

81 <msg>pyquickhelper: first version</msg> 

82 </logentry> 

83 """ 

84 if self.module is None: 

85 self.SetGuessedType(path) 

86 return self.module.get_repo_log( 

87 path, file_detail, commandline=self.commandline) 

88 

89 def version(self, path=None): 

90 """ 

91 Gets the latest check in number for a specific path. 

92 

93 @param path path to look 

94 @return string or int (check in number) 

95 """ 

96 if self.module is None: 

97 self.SetGuessedType(path) 

98 return self.module.get_repo_version(path, commandline=self.commandline) 

99 

100 def nb_commits(self, path=None): 

101 """ 

102 Returns the number of commits. 

103 

104 @param path path to look 

105 @return number of commit 

106 """ 

107 if self.module is None: 

108 self.SetGuessedType(path) 

109 return self.module.get_nb_commits(path, commandline=self.commandline) 

110 

111 def get_last_commit_hash(self, path=None): 

112 """ 

113 Returns the last commit. 

114 

115 @param path path 

116 @return last commit 

117 """ 

118 return self.log(path)[0][-2]