|
| 1 | +import * as fs from "fs"; |
| 2 | +import util from "util"; |
1 | 3 | import * as path from "path"; |
2 | | -import gitP, { SimpleGit, StatusResult } from "simple-git/promise"; |
| 4 | +import gitP, { SimpleGit } from "simple-git/promise"; |
| 5 | +import * as T from "../../typings/tutorial"; |
3 | 6 |
|
4 | | -export async function getCommits() { |
5 | | - const git: SimpleGit = gitP(process.cwd()); |
| 7 | +const mkdir = util.promisify(fs.mkdir); |
| 8 | +const exists = util.promisify(fs.exists); |
| 9 | +const rmdir = util.promisify(fs.rmdir); |
6 | 10 |
|
7 | | - const isRepo = await git.checkIsRepo(); |
| 11 | +type GetCommitOptions = { |
| 12 | + localDir: string; |
| 13 | + codeBranch: string; |
| 14 | +}; |
| 15 | + |
| 16 | +export type CommitLogObject = { [position: string]: string[] }; |
8 | 17 |
|
9 | | - const tmpDirectory = "tmp"; |
10 | | - const localPath = path.join(process.cwd(), tmpDirectory); |
| 18 | +export async function getCommits({ |
| 19 | + localDir, |
| 20 | + codeBranch, |
| 21 | +}: GetCommitOptions): Promise<CommitLogObject> { |
| 22 | + const git: SimpleGit = gitP(localDir); |
| 23 | + |
| 24 | + const isRepo = await git.checkIsRepo(); |
11 | 25 |
|
12 | 26 | if (!isRepo) { |
13 | 27 | throw new Error("No git repo provided"); |
14 | 28 | } |
15 | 29 |
|
16 | | - // if (isRepo) { |
17 | | - // await gitTest.submoduleAdd(repo, workingDir); |
18 | | - |
19 | | - // isSubModule = true; |
20 | | - // } else { |
21 | | - // await gitTest.clone(repo, localPath); |
22 | | - // } |
23 | | - |
24 | | - // await git.fetch(); |
25 | | - |
26 | | - // // checkout the branch to load tutorialuration and content branch |
27 | | - // await git.checkout(setupBranch); |
28 | | - |
29 | | - // // Checkout the code branches |
30 | | - // await git.checkout(codeBranch); |
31 | | - |
32 | | - // // Load all logs |
33 | | - // const logs = await git.log(); |
34 | | - |
35 | | - // // Filter relevant logs |
36 | | - // const parts = new Set(); |
| 30 | + const tmpDir = path.join(localDir, ".tmp"); |
37 | 31 |
|
38 | | - // for (const commit of logs.all) { |
39 | | - // const matches = commit.message.match( |
40 | | - // /^(?<stepId>(?<levelId>L\d+)S\d+)(?<stepType>[QA])?/ |
41 | | - // ); |
42 | | - |
43 | | - // if (matches && !parts.has(matches[0])) { |
44 | | - // // Uses a set to make sure only the latest commit is proccessed |
45 | | - // parts.add(matches[0]); |
46 | | - |
47 | | - // // Add the content and git hash to the tutorial |
48 | | - // if (matches.groups.stepId) { |
49 | | - // // If it's a step: add the content and the setup/solution hashes depending on the type |
50 | | - // const level: T.Level | null = |
51 | | - // tutorial.levels.find( |
52 | | - // (level: T.Level) => level.id === matches.groups.levelId |
53 | | - // ) || null; |
54 | | - // if (!level) { |
55 | | - // console.log(`Level ${matches.groups.levelId} not found`); |
56 | | - // } else { |
57 | | - // const theStep: T.Step | null = |
58 | | - // level.steps.find( |
59 | | - // (step: T.Step) => step.id === matches.groups.stepId |
60 | | - // ) || null; |
61 | | - |
62 | | - // if (!theStep) { |
63 | | - // console.log(`Step ${matches.groups.stepId} not found`); |
64 | | - // } else { |
65 | | - // if (matches.groups.stepType === "Q") { |
66 | | - // theStep.setup.commits.push(commit.hash.substr(0, 7)); |
67 | | - // } else if ( |
68 | | - // matches.groups.stepType === "A" && |
69 | | - // theStep.solution && |
70 | | - // theStep.solution.commits |
71 | | - // ) { |
72 | | - // theStep.solution.commits.push(commit.hash.substr(0, 7)); |
73 | | - // } |
74 | | - // } |
75 | | - // } |
76 | | - // } else { |
77 | | - // // If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial |
78 | | - // const theLevel: T.Level | null = |
79 | | - // tutorial.levels.find( |
80 | | - // (level: T.Level) => level.id === matches.groups.levelId |
81 | | - // ) || null; |
82 | | - |
83 | | - // if (!theLevel) { |
84 | | - // console.log(`Level ${matches.groups.levelId} not found`); |
85 | | - // } else { |
86 | | - // if (_.has(theLevel, "tutorial.commits")) { |
87 | | - // if (theLevel.setup) { |
88 | | - // theLevel.setup.commits.push(commit.hash.substr(0, 7)); |
89 | | - // } |
90 | | - // } |
91 | | - // } |
92 | | - // } |
93 | | - // } |
94 | | - // } |
95 | | - |
96 | | - // // cleanup the submodules |
97 | | - // if (!isLocal) { |
98 | | - // let cleanupErr; |
99 | | - |
100 | | - // if (isSubModule) { |
101 | | - // cleanupErr = await cleanupFiles(workingDir); |
102 | | - // } else { |
103 | | - // cleanupErr = rmDir(path.join(process.cwd(), workingDir)); |
104 | | - // } |
105 | | - |
106 | | - // if (cleanupErr) { |
107 | | - // console.log( |
108 | | - // `Error when deleting temporary files on ${ |
109 | | - // isSubModule ? "module" : "folder" |
110 | | - // } ${workingDir}.` |
111 | | - // ); |
112 | | - // } |
113 | | - // } |
| 32 | + const tmpDirExists = await exists(tmpDir); |
| 33 | + if (tmpDirExists) { |
| 34 | + await rmdir(tmpDir, { recursive: true }); |
| 35 | + } |
| 36 | + await mkdir(tmpDir); |
| 37 | + const tempGit = gitP(tmpDir); |
| 38 | + await tempGit.clone(localDir, tmpDir); |
| 39 | + |
| 40 | + // Checkout the code branches |
| 41 | + await git.checkout(codeBranch); |
| 42 | + |
| 43 | + // Load all logs |
| 44 | + const logs = await git.log(); |
| 45 | + |
| 46 | + // Filter relevant logs |
| 47 | + const commits: CommitLogObject = {}; |
| 48 | + |
| 49 | + for (const commit of logs.all) { |
| 50 | + const matches = commit.message.match( |
| 51 | + /^(?<stepId>(?<levelId>L\d+)(S\d+))(?<stepType>[QA])?/ |
| 52 | + ); |
| 53 | + |
| 54 | + if (matches && matches.length) { |
| 55 | + // Use an object of commit arrays to collect all commits |
| 56 | + const position = matches[0]; |
| 57 | + if (!commits[position]) { |
| 58 | + // does not exist, create the list |
| 59 | + commits[position] = [commit.hash]; |
| 60 | + } else { |
| 61 | + // add to the list |
| 62 | + commits[position].push(commit.hash); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + // cleanup the tmp directory |
| 67 | + await rmdir(tmpDir, { recursive: true }); |
| 68 | + return commits; |
114 | 69 | } |
115 | | - |
116 | | -getCommits(); |
0 commit comments