|
15 | 15 | # ********************************************************************************************* |
16 | 16 |
|
17 | 17 | import bash as sh |
| 18 | +import subprocess as sub |
18 | 19 | import os |
19 | 20 | import warnings |
20 | 21 | from gitnet.exceptions import RepositoryError, ParseError, InputError |
21 | 22 | from gitnet.commit_log import CommitLog |
22 | | -import subprocess as sub |
23 | 23 |
|
24 | | - |
25 | | -def retrieve_commits(path, mode="stat"): |
| 24 | +def get_log(path, mode="stat", commit_source="local git"): |
26 | 25 | """ |
27 | | - Takes a file path string and a mode string and produces the git log for the |
28 | | - specified directory. The default mode, "stat" retrieves the logs by running "git log --stat". |
29 | | - Modes include: "basic" (git log), "raw" ("git log --raw"), and "stat" ("git log --stat"). |
| 26 | + A function for gathering data from a local Git repository. |
30 | 27 |
|
31 | 28 | **Parameters** : |
32 | 29 |
|
33 | 30 | > *path* : `string` |
34 | 31 |
|
35 | | - >> A string identifying the path to the target git repository. |
| 32 | + >> A string containing the path of the Git repository. |
36 | 33 |
|
37 | 34 | > *mode* : `string` |
38 | 35 |
|
39 | | - >> A string identifying the git log mode to be retrieved. Default mode is "stat". |
| 36 | + >> The retrieval mode. Modes currently implemented: "basic", "raw", "stat". |
40 | 37 |
|
41 | | - **Return** : |
| 38 | + > *commit_source* : `string` |
42 | 39 |
|
43 | | - > Returns a large string containing the raw output from the repository's git log. |
| 40 | + >> The source of the Git repository, local is currently the only option. |
44 | 41 |
|
45 | | - """ |
46 | | - print("Attempting local git log retrieval...") |
47 | | - # Log command modes, referenced by "mode" input. |
48 | | - log_commands = {"basic": "git log", "raw": "git log --raw", "stat":"git log --stat"} |
49 | | - if mode not in log_commands.keys(): |
50 | | - raise InputError("{} is not a valid retrieval mode.".format(mode)) |
51 | | - # Save the current directory. Navigate to new directory. Retrieve logs. Return to original directory. |
52 | | - work_dir = os.getcwd() |
53 | | - os.chdir(path) |
54 | | - raw_logs = sh.bash(log_commands[mode]).stdout.decode("utf-8") |
55 | | - os.chdir(work_dir) |
56 | | - # If the retrieval was unsuccessful, raise an error. |
57 | | - if len(raw_logs) == 0: |
58 | | - print("Raising error.") |
59 | | - if "true" in str(sh.bash("git rev-parse --is-inside-work-tree").stdout): |
60 | | - raise RepositoryError("{} is not a Git repository.".format(path)) |
61 | | - else: |
62 | | - raise RepositoryError("{} has no commits.".format(path)) |
63 | | - # If the retrieval was successful, print a summary." |
64 | | - print("Got {} characters from: {}".format(len(raw_logs), path)) |
65 | | - # Record the retrieval mode. |
66 | | - raw_logs = "Mode =\n{}\n".format(mode) + raw_logs |
67 | | - return raw_logs |
| 42 | + **Returns** : `Commitlog` |
68 | 43 |
|
| 44 | + """ |
| 45 | + if commit_source == "local git": |
| 46 | + detect_key = "hash" |
| 47 | + else: |
| 48 | + detect_key = "unknown" |
| 49 | + return CommitLog(dofd=parse_commits(retrieve_commits(path, mode)), |
| 50 | + source=commit_source, |
| 51 | + path=path, |
| 52 | + key_type=detect_key) |
69 | 53 |
|
70 | 54 | def identify(s): |
71 | 55 | """ |
@@ -117,7 +101,6 @@ def identify(s): |
117 | 101 | " so was identified as 'other'.".format(s)) |
118 | 102 | return "none" |
119 | 103 |
|
120 | | - |
121 | 104 | def parse_commits(commit_str): |
122 | 105 | """ |
123 | 106 | Parses a raw string containing a commit Log for a Git repository. It produces a dictionary |
@@ -214,33 +197,46 @@ def parse_commits(commit_str): |
214 | 197 | warnings.warn("Parser was unable to identify {}. Identity string <{}> not recognized".format(line,id)) |
215 | 198 | return collection |
216 | 199 |
|
217 | | - |
218 | | -def get_log(path, mode="stat", commit_source="local git"): |
| 200 | +def retrieve_commits(path, mode="stat"): |
219 | 201 | """ |
220 | | - A function for gathering data from a local Git repository. |
| 202 | + Takes a file path string and a mode string and produces the git log for the |
| 203 | + specified directory. The default mode, "stat" retrieves the logs by running "git log --stat". |
| 204 | + Modes include: "basic" (git log), "raw" ("git log --raw"), and "stat" ("git log --stat"). |
221 | 205 |
|
222 | 206 | **Parameters** : |
223 | 207 |
|
224 | 208 | > *path* : `string` |
225 | 209 |
|
226 | | - >> A string containing the path of the Git repository. |
| 210 | + >> A string identifying the path to the target git repository. |
227 | 211 |
|
228 | 212 | > *mode* : `string` |
229 | 213 |
|
230 | | - >> The retrieval mode. Modes currently implemented: "basic", "raw", "stat". |
231 | | -
|
232 | | - > *commit_source* : `string` |
| 214 | + >> A string identifying the git log mode to be retrieved. Default mode is "stat". |
233 | 215 |
|
234 | | - >> The source of the Git repository, local is currently the only option. |
| 216 | + **Return** : |
235 | 217 |
|
236 | | - **Returns** : `Commitlog` |
| 218 | + > Returns a large string containing the raw output from the repository's git log. |
237 | 219 |
|
238 | 220 | """ |
239 | | - if commit_source == "local git": |
240 | | - detect_key = "hash" |
241 | | - else: |
242 | | - detect_key = "unknown" |
243 | | - return CommitLog(dofd=parse_commits(retrieve_commits(path, mode)), |
244 | | - source=commit_source, |
245 | | - path=path, |
246 | | - key_type=detect_key) |
| 221 | + print("Attempting local git log retrieval...") |
| 222 | + # Log command modes, referenced by "mode" input. |
| 223 | + log_commands = {"basic": "git log", "raw": "git log --raw", "stat":"git log --stat"} |
| 224 | + if mode not in log_commands.keys(): |
| 225 | + raise InputError("{} is not a valid retrieval mode.".format(mode)) |
| 226 | + # Save the current directory. Navigate to new directory. Retrieve logs. Return to original directory. |
| 227 | + work_dir = os.getcwd() |
| 228 | + os.chdir(path) |
| 229 | + raw_logs = sh.bash(log_commands[mode]).stdout.decode("utf-8") |
| 230 | + os.chdir(work_dir) |
| 231 | + # If the retrieval was unsuccessful, raise an error. |
| 232 | + if len(raw_logs) == 0: |
| 233 | + print("Raising error.") |
| 234 | + if "true" in str(sh.bash("git rev-parse --is-inside-work-tree").stdout): |
| 235 | + raise RepositoryError("{} is not a Git repository.".format(path)) |
| 236 | + else: |
| 237 | + raise RepositoryError("{} has no commits.".format(path)) |
| 238 | + # If the retrieval was successful, print a summary." |
| 239 | + print("Got {} characters from: {}".format(len(raw_logs), path)) |
| 240 | + # Record the retrieval mode. |
| 241 | + raw_logs = "Mode =\n{}\n".format(mode) + raw_logs |
| 242 | + return raw_logs |
0 commit comments