-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.ts
179 lines (162 loc) · 4.49 KB
/
git.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// =============================================================================
// File : git.ts
// Author : yukimemi
// Last Change : 2024/06/30 12:59:51.
// =============================================================================
import * as path from "jsr:@std/path@1.0.2";
import { exists } from "jsr:@std/fs@1.0.1";
import { TextLineStream } from "jsr:@std/streams@1.0.1";
/**
* Git
*/
export class Git {
/// Returns the path to the .git directory
public gitDir: string;
/// Creates a new Git instance
constructor(public base: string) {
this.gitDir = path.join(base, ".git");
}
/// Executes a git command
private async git(args: string[]): Promise<Deno.CommandOutput> {
const cmd = new Deno.Command("git", { args: ["-C", this.base, ...args] });
return await cmd.output();
}
/// Converts a CommandOutput to a string
private cmdOutToString(output: Deno.CommandOutput): string {
return new TextDecoder().decode(output.stdout).trim();
}
/**
* Get the HEAD
*/
public async getHead(): Promise<string> {
const headFile = path.join(this.gitDir, "HEAD");
return (await Deno.readTextFile(headFile)).trim();
}
/**
* Get the revision from git
*/
public async getRevisionGit(): Promise<string> {
return this.cmdOutToString(await this.git(["rev-parse", "HEAD"]));
}
/**
* Checkout a revision
*/
public async checkout(rev: string): Promise<void> {
await this.git(["checkout", rev]);
}
/**
* Get the current branch from git
*/
public async getBranchGit(): Promise<string> {
return this.cmdOutToString(
await this.git(["rev-parse", "--abbrev-ref", "HEAD"]),
);
}
/**
* Get the default branch from git
*/
public async getDefaultBranchGit(): Promise<string> {
const branch = path.basename(this.cmdOutToString(
await this.git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]),
));
if (branch.match(/fatal: /)) {
await this.git(["remote", "set-head", "origin", "--auto"]);
return path.basename(this.cmdOutToString(
await this.git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]),
));
}
return branch;
}
/**
* Get the revision
*/
public async getRevision(): Promise<string> {
const head = await this.getHead();
const ref = head.substring(5);
const refFile = path.join(this.gitDir, ref);
if (await exists(refFile)) {
const ref = await Deno.readTextFile(refFile);
return ref.split("\n")[0].trim();
}
const packedRefs = await Deno.open(path.join(this.gitDir, "packed-refs"));
const lineStream = packedRefs.readable.pipeThrough(new TextDecoderStream())
.pipeThrough(
new TextLineStream(),
);
for await (const line of lineStream) {
if (line.match(`/ ${ref}/`)) {
return line.split(" ")[0];
}
}
return await this.getRevisionGit();
}
/**
* Get the current branch
*/
public async getBranch(): Promise<string> {
const head = await this.getHead();
if (head.match(/^ref: refs\/heads\//)) {
return head.substring(16);
}
return await this.getBranchGit();
}
/**
* Get the git log
*/
public async getLog(
from: string,
to: string,
argOption: string[] = [],
): Promise<Deno.CommandOutput> {
return await this.git(["log", ...argOption, `${from}..${to}`]);
}
/**
* Get the git diff
*/
public async getDiff(
from: string,
to: string,
): Promise<Deno.CommandOutput> {
return await this.git([
"diff",
`${from}..${to}`,
"--",
"doc",
"README",
"README.md",
]);
}
/**
* Clone a git repository
*/
public static async clone(
url: string,
dst: string,
branch?: string,
depth = 0,
): Promise<Deno.CommandOutput> {
let args = ["clone", "--recursive", "--filter=blob:none"];
if (branch) {
args.push(`--branch=${branch}`);
}
if (depth > 0) {
args.push(`--depth=${depth}`);
}
args = args.concat([url, dst]);
const cmd = new Deno.Command("git", { args });
return await cmd.output();
}
/**
* Pull a git repository
*/
public async pull(branch?: string): Promise<Deno.CommandOutput> {
const currentBranch = await this.getBranch();
branch ??= await this.getDefaultBranchGit();
if (branch !== currentBranch) {
await this.checkout(branch);
}
const args = ["-C", this.base, "pull", "--ff"];
const cmd = new Deno.Command("git", { args });
return await cmd.output();
}
}