First, make sure you have the simple-git library imported in your TypeScript file:
1
| import simpleGit, { SimpleGit, DiffResult } from "simple-git";
|
Create a function that will perform the Git diff
operation between two tags. The function should accept the tag names as parameters and return a promise that resolves to the diff output string.
1
2
3
4
5
6
| async function getGitDiff(tag1: string, tag2: string): Promise<string> {
const git: SimpleGit = simpleGit();
const diff: DiffResult = await git.diff([`${tag1}..${tag2}`]);
return diff.diff;
}
|
In the above code, we use the simpleGit function from the simple-git
library to instantiate a new SimpleGit
object. Then, we call the diff method with the tag range specified as ${tag1}..${tag2}
. This will retrieve the diff result as a DiffResult
object, from which we extract the diff
property containing the actual diff output
.
Now you can call the getGitDiff function and retrieve the diff output between two tags:
1
2
3
4
5
| const tag1: string = "v1.0.0";
const tag2: string = "v1.1.0";
const diff: string = await getGitDiff(tag1, tag2);
console.log(diff);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| import simpleGit, { SimpleGit, DiffResult } from "simple-git";
import * as fs from "fs-extra";
async function gitDiff(
repositoryUrl: string,
tag1: string,
tag2: string
): Promise<void> {
const git: SimpleGit = simpleGit();
const tempDir = "./temp-repo";
// Delete the temporary directory if it exists
if (fs.existsSync(tempDir)) {
console.log("tempDir exists, removing it");
await fs.remove(tempDir);
}
// Create a new empty temporary directory
console.log("creating tempDir");
await fs.mkdir(tempDir);
// Clone the repository from the provided URL
await git.clone(repositoryUrl, "./temp-repo");
// Change the working directory to the cloned repository
git.cwd("./temp-repo");
// Perform the 'git diff' operation
let diff: string = "";
try {
diff = await git.diff([`${tag1}..${tag2}`]);
console.log("diff", diff);
// Rest of the code...
} catch (error) {
console.error("An error occurred while performing git diff:", error);
}
// Remove the temporary repository
console.log(diff);
await fs.remove(tempDir);
}
|
Cheers! 🍺