-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathapi.ts
More file actions
208 lines (170 loc) · 6.04 KB
/
api.ts
File metadata and controls
208 lines (170 loc) · 6.04 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import 'server-only';
import { sew } from '@/actions';
import { notFound, unexpectedError } from '@/lib/serviceError';
import { withOptionalAuthV2 } from '@/withAuthV2';
import { createLogger, getRepoPath } from '@sourcebot/shared';
import { simpleGit } from 'simple-git';
import { FileTreeItem } from './types';
import { buildFileTree, isPathValid, normalizePath } from './utils';
import { compareFileTreeItems } from './utils';
const logger = createLogger('file-tree');
/**
* Returns a file tree spanning the union of all provided paths for the given
* repo/revision, including intermediate directories needed to connect them
* into a single tree.
*/
export const getTree = async (params: { repoName: string, revisionName: string, paths: string[] }) => sew(() =>
withOptionalAuthV2(async ({ org, prisma }) => {
const { repoName, revisionName, paths } = params;
const repo = await prisma.repo.findFirst({
where: {
name: repoName,
orgId: org.id,
},
});
if (!repo) {
return notFound();
}
const { path: repoPath } = getRepoPath(repo);
const git = simpleGit().cwd(repoPath);
if (!paths.every(path => isPathValid(path))) {
return notFound();
}
const normalizedPaths = paths.map(path => normalizePath(path));
// Verify that the revision is not empty
try{
await git.raw(["rev-parse","--verify",revisionName])
}catch(_error){
return {tree:{}}
}
let result: string = '';
try {
const command = [
// Disable quoting of non-ASCII characters in paths
'-c', 'core.quotePath=false',
'ls-tree',
revisionName,
// format as output as {type},{path}
'--format=%(objecttype),%(path)',
// include tree nodes
'-t',
'--',
'.',
...normalizedPaths,
];
result = await git.raw(command);
} catch (error) {
logger.error('git ls-tree failed.', { error });
return unexpectedError('git ls-tree command failed.');
}
const lines = result.split('\n').filter(line => line.trim());
const flatList = lines.map(line => {
const [type, path] = line.split(',');
return {
type,
path,
}
});
const tree = buildFileTree(flatList);
return {
tree,
}
}));
/**
* Returns the contents of a folder at a given path in a given repository,
* at a given revision.
*/
export const getFolderContents = async (params: { repoName: string, revisionName: string, path: string }) => sew(() =>
withOptionalAuthV2(async ({ org, prisma }) => {
const { repoName, revisionName, path } = params;
const repo = await prisma.repo.findFirst({
where: {
name: repoName,
orgId: org.id,
},
});
if (!repo) {
return notFound();
}
const { path: repoPath } = getRepoPath(repo);
const git = simpleGit().cwd(repoPath);
if (!isPathValid(path)) {
return notFound();
}
const normalizedPath = normalizePath(path);
// Verify that the revision is not empty
try{
await git.raw(["rev-parse","--verify",revisionName])
} catch(_error){
return [];
}
let result: string;
try {
result = await git.raw([
// Disable quoting of non-ASCII characters in paths
'-c', 'core.quotePath=false',
'ls-tree',
revisionName,
// format as output as {type},{path}
'--format=%(objecttype),%(path)',
...(normalizedPath.length === 0 ? [] : [normalizedPath]),
]);
} catch (error) {
logger.error('git ls-tree failed.', { error });
return unexpectedError('git ls-tree command failed.');
}
const lines = result.split('\n').filter(line => line.trim());
const contents: FileTreeItem[] = lines.map(line => {
const [type, path] = line.split(',');
const name = path.split('/').pop() ?? '';
return {
type,
path,
name,
}
});
// Sort the contents in place, first by type (trees before blobs), then by name.
contents.sort(compareFileTreeItems);
return contents;
}));
export const getFiles = async (params: { repoName: string, revisionName: string }) => sew(() =>
withOptionalAuthV2(async ({ org, prisma }) => {
const { repoName, revisionName } = params;
const repo = await prisma.repo.findFirst({
where: {
name: repoName,
orgId: org.id,
},
});
if (!repo) {
return notFound();
}
const { path: repoPath } = getRepoPath(repo);
const git = simpleGit().cwd(repoPath);
let result: string;
try {
result = await git.raw([
// Disable quoting of non-ASCII characters in paths
'-c', 'core.quotePath=false',
'ls-tree',
revisionName,
// recursive
'-r',
// only return the names of the files
'--name-only',
]);
} catch (error) {
logger.error('git ls-tree failed.', { error });
return unexpectedError('git ls-tree command failed.');
}
const paths = result.split('\n').filter(line => line.trim());
const files: FileTreeItem[] = paths.map(path => {
const name = path.split('/').pop() ?? '';
return {
type: 'blob',
path,
name,
}
});
return files;
}));