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
|
import { defined, get } from "../../utils/anonymous.ts";
import { type MaybeIterable, surelyIterable } from "../../utils/iterator.ts";
import { gitDir } from "./index.ts";
import type { Commit, CommitFile } from "./types.ts";
const format = [
"H",
"h",
"aI",
"aN",
"aE",
"cI",
"cN",
"cE",
// "G?",
"GS",
"GK",
"GF",
"GG",
];
export async function getLastCommitForOneOfFiles(
sources: MaybeIterable<URL>,
): Promise<Commit | undefined> {
const files = surelyIterable(sources);
const gitLogs = (await Promise.all(
Iterator.from(files).map(async ({ pathname }) => {
const gitLog = new Deno.Command("git", {
args: [
"log",
"--follow",
"-1",
`--pretty=format:${format.map((x) => `%${x}`).join("%n")}`,
"--",
pathname,
],
});
const { stdout } = await gitLog.output();
const result = new TextDecoder().decode(stdout).trim();
if (result.length <= 0) {
return undefined;
}
const [
hash,
abbrHash,
authorDate,
authorName,
authorEmail,
committerDate,
committerName,
committerEmail,
// signatureValidation,
signer,
key,
keyFingerPrint,
...rawLines
] = result.split("\n");
const raw = rawLines.join("\n").trim();
const commit: Commit = {
// deno-lint-ignore no-undef
files: await fileStatusFromCommit(hash, Iterator.from(files)),
hash: { long: hash, short: abbrHash },
author: {
date: new Date(authorDate),
name: authorName,
email: authorEmail,
},
committer: {
date: new Date(committerDate),
name: committerName,
email: committerEmail,
},
};
if (raw.length > 0) {
commit.signature = {
type: raw.startsWith("gpgsm:")
? "x509"
: raw.startsWith("gpg:")
? "gpg"
: "ssh",
signer,
key: { long: keyFingerPrint, short: key },
rawMessage: raw,
};
}
return commit;
}),
)).filter(defined);
const last = gitLogs.sort(({ committer: a }, { committer: b }) =>
b.date.getTime() - a.date.getTime()
)?.[0];
if (last === undefined) return undefined;
const final = gitLogs.filter(({ hash }) => hash.long === last.hash.long);
last.files = final.flatMap(get("files"));
return last;
}
async function fileStatusFromCommit(
hash: string,
files: Iterable<URL>,
): Promise<CommitFile[]> {
const gitDiffTree = new Deno.Command("git", {
args: [
"diff-tree",
"--no-commit-id",
"--name-status",
"-r",
hash,
],
});
const { stdout } = await gitDiffTree.output();
const result = new TextDecoder().decode(stdout).trim().split("\n").filter(
defined,
);
const dir = await gitDir();
return result.map((line) => {
const [status, path] = line.split("\t");
if (
// deno-lint-ignore no-undef
Iterator.from(files).some((file) =>
file.pathname.replace(dir.pathname, "").includes(path)
)
) {
return {
path: new URL(path, dir),
status: status === "A"
? "added"
: status === "D"
? "deleted"
: "modified",
} as const;
}
return undefined;
}).filter(defined);
}
export async function fileCreationCommitDate(
file: URL,
): Promise<Date | undefined> {
const gitDiffTree = new Deno.Command("git", {
args: [
"log",
"--follow",
"--diff-filter=A",
"--format=%cI",
"--",
file.pathname,
],
});
const { stdout } = await gitDiffTree.output();
try {
return new Date(new TextDecoder().decode(stdout).trim());
} catch {
return undefined;
}
}
|