forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepositoryCommitPage.tsx
281 lines (262 loc) · 13.1 KB
/
RepositoryCommitPage.tsx
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { createHoverifier, HoveredToken, Hoverifier, HoverState } from '@sourcegraph/codeintellify'
import { LoadingSpinner } from '@sourcegraph/react-loading-spinner'
import { isEqual } from 'lodash'
import * as React from 'react'
import { RouteComponentProps } from 'react-router'
import { merge, Observable, of, Subject, Subscription } from 'rxjs'
import { catchError, distinctUntilChanged, filter, map, switchMap, tap, withLatestFrom } from 'rxjs/operators'
import { ActionItemAction } from '../../../../shared/src/actions/ActionItem'
import { HoverMerged } from '../../../../shared/src/api/client/types/hover'
import { ExtensionsControllerProps } from '../../../../shared/src/extensions/controller'
import { gql } from '../../../../shared/src/graphql/graphql'
import * as GQL from '../../../../shared/src/graphql/schema'
import { getHoverActions } from '../../../../shared/src/hover/actions'
import { HoverContext } from '../../../../shared/src/hover/HoverOverlay'
import { getModeFromPath } from '../../../../shared/src/languages'
import { PlatformContextProps } from '../../../../shared/src/platform/context'
import { asError, createAggregateError, ErrorLike, isErrorLike } from '../../../../shared/src/util/errors'
import { memoizeObservable } from '../../../../shared/src/util/memoizeObservable'
import { property, isDefined } from '../../../../shared/src/util/types'
import { FileSpec, ModeSpec, UIPositionSpec, RepoSpec, ResolvedRevSpec, RevSpec } from '../../../../shared/src/util/url'
import { getHover } from '../../backend/features'
import { queryGraphQL } from '../../backend/graphql'
import { PageTitle } from '../../components/PageTitle'
import { WebHoverOverlay } from '../../components/shared'
import { eventLogger, EventLoggerProps } from '../../tracking/eventLogger'
import { GitCommitNode } from '../commits/GitCommitNode'
import { gitCommitFragment } from '../commits/RepositoryCommitsPage'
import { FileDiffConnection } from '../../components/diff/FileDiffConnection'
import { FileDiffNode } from '../../components/diff/FileDiffNode'
import { queryRepositoryComparisonFileDiffs } from '../compare/RepositoryCompareDiffPage'
import { ThemeProps } from '../../../../shared/src/theme'
import { ErrorAlert } from '../../components/alerts'
const queryCommit = memoizeObservable(
(args: { repo: GQL.ID; revspec: string }): Observable<GQL.IGitCommit> =>
queryGraphQL(
gql`
query RepositoryCommit($repo: ID!, $revspec: String!) {
node(id: $repo) {
... on Repository {
commit(rev: $revspec) {
__typename # necessary so that isErrorLike(x) is false when x: GQL.IGitCommit
...GitCommitFields
}
}
}
}
${gitCommitFragment}
`,
args
).pipe(
map(({ data, errors }) => {
if (!data || !data.node) {
throw createAggregateError(errors)
}
const repo = data.node as GQL.IRepository
if (!repo.commit) {
throw createAggregateError(errors || [new Error('Commit not found')])
}
return repo.commit
})
),
args => `${args.repo}:${args.revspec}`
)
interface Props
extends RouteComponentProps<{ revspec: string }>,
EventLoggerProps,
PlatformContextProps,
ExtensionsControllerProps,
ThemeProps {
repo: GQL.IRepository
onDidUpdateExternalLinks: (externalLinks: GQL.IExternalLink[] | undefined) => void
}
interface State extends HoverState<HoverContext, HoverMerged, ActionItemAction> {
/** The commit, undefined while loading, or an error. */
commitOrError?: GQL.IGitCommit | ErrorLike
}
/** Displays a commit. */
export class RepositoryCommitPage extends React.Component<Props, State> {
private componentUpdates = new Subject<Props>()
/** Emits whenever the ref callback for the hover element is called */
private hoverOverlayElements = new Subject<HTMLElement | null>()
private nextOverlayElement = (element: HTMLElement | null): void => this.hoverOverlayElements.next(element)
/** Emits whenever the ref callback for the hover element is called */
private repositoryCommitPageElements = new Subject<HTMLElement | null>()
private nextRepositoryCommitPageElement = (element: HTMLElement | null): void =>
this.repositoryCommitPageElements.next(element)
/** Emits when the close button was clicked */
private closeButtonClicks = new Subject<MouseEvent>()
private nextCloseButtonClick = (event: MouseEvent): void => this.closeButtonClicks.next(event)
private subscriptions = new Subscription()
private hoverifier: Hoverifier<RepoSpec & RevSpec & FileSpec & ResolvedRevSpec, HoverMerged, ActionItemAction>
constructor(props: Props) {
super(props)
this.hoverifier = createHoverifier<
RepoSpec & RevSpec & FileSpec & ResolvedRevSpec,
HoverMerged,
ActionItemAction
>({
closeButtonClicks: this.closeButtonClicks,
hoverOverlayElements: this.hoverOverlayElements,
hoverOverlayRerenders: this.componentUpdates.pipe(
withLatestFrom(this.hoverOverlayElements, this.repositoryCommitPageElements),
map(([, hoverOverlayElement, repositoryCommitPageElement]) => ({
hoverOverlayElement,
// The root component element is guaranteed to be rendered after a componentDidUpdate
relativeElement: repositoryCommitPageElement!,
})),
// Can't reposition HoverOverlay if it wasn't rendered
filter(property('hoverOverlayElement', isDefined))
),
getHover: hoveredToken => getHover(this.getLSPTextDocumentPositionParams(hoveredToken), this.props),
getActions: context => getHoverActions(this.props, context),
pinningEnabled: true,
})
this.subscriptions.add(this.hoverifier)
this.state = this.hoverifier.hoverState
this.subscriptions.add(
this.hoverifier.hoverStateUpdates.subscribe(update => {
this.setState(update)
})
)
}
private getLSPTextDocumentPositionParams(
hoveredToken: HoveredToken & RepoSpec & RevSpec & FileSpec & ResolvedRevSpec
): RepoSpec & RevSpec & ResolvedRevSpec & FileSpec & UIPositionSpec & ModeSpec {
return {
repoName: hoveredToken.repoName,
rev: hoveredToken.rev,
filePath: hoveredToken.filePath,
commitID: hoveredToken.commitID,
position: hoveredToken,
mode: getModeFromPath(hoveredToken.filePath || ''),
}
}
public componentDidMount(): void {
eventLogger.logViewEvent('RepositoryCommit')
this.subscriptions.add(
this.componentUpdates
.pipe(
distinctUntilChanged(
(a, b) => a.repo.id === b.repo.id && a.match.params.revspec === b.match.params.revspec
),
switchMap(({ repo, match }) =>
merge(
of({ commitOrError: undefined }),
queryCommit({ repo: repo.id, revspec: match.params.revspec }).pipe(
catchError(error => [asError(error)]),
map(c => ({ commitOrError: c })),
tap(({ commitOrError }: { commitOrError: GQL.IGitCommit | ErrorLike }) => {
if (isErrorLike(commitOrError)) {
this.props.onDidUpdateExternalLinks(undefined)
} else {
this.props.onDidUpdateExternalLinks(commitOrError.externalURLs)
}
})
)
)
)
)
.subscribe(
stateUpdate => this.setState(stateUpdate),
error => console.error(error)
)
)
this.componentUpdates.next(this.props)
}
public shouldComponentUpdate(nextProps: Readonly<Props>, nextState: Readonly<State>): boolean {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState)
}
public componentDidUpdate(): void {
this.componentUpdates.next(this.props)
}
public componentWillUnmount(): void {
this.props.onDidUpdateExternalLinks(undefined)
this.subscriptions.unsubscribe()
}
public render(): JSX.Element | null {
return (
<div className="repository-commit-page container mt-3" ref={this.nextRepositoryCommitPageElement}>
<PageTitle
title={
this.state.commitOrError && !isErrorLike(this.state.commitOrError)
? this.state.commitOrError.subject
: `Commit ${this.props.match.params.revspec}`
}
/>
{this.state.commitOrError === undefined ? (
<LoadingSpinner className="icon-inline mt-2" />
) : isErrorLike(this.state.commitOrError) ? (
<ErrorAlert className="mt-2" error={this.state.commitOrError} />
) : (
<>
<div className="card repository-commit-page__card">
<div className="card-body">
<GitCommitNode
node={this.state.commitOrError}
expandCommitMessageBody={true}
showSHAAndParentsRow={true}
/>
</div>
</div>
<div className="mb-3" />
<FileDiffConnection
listClassName="list-group list-group-flush"
noun="changed file"
pluralNoun="changed files"
queryConnection={this.queryDiffs}
nodeComponent={FileDiffNode}
nodeComponentProps={{
...this.props,
extensionInfo: {
base: {
repoName: this.props.repo.name,
repoID: this.props.repo.id,
rev: commitParentOrEmpty(this.state.commitOrError),
commitID: commitParentOrEmpty(this.state.commitOrError),
},
head: {
repoName: this.props.repo.name,
repoID: this.props.repo.id,
rev: this.state.commitOrError.oid,
commitID: this.state.commitOrError.oid,
},
hoverifier: this.hoverifier,
extensionsController: this.props.extensionsController,
},
lineNumbers: true,
}}
updateOnChange={`${this.props.repo.id}:${this.state.commitOrError.oid}`}
defaultFirst={25}
hideSearch={true}
noSummaryIfAllNodesVisible={true}
history={this.props.history}
location={this.props.location}
/>
</>
)}
{this.state.hoverOverlayProps && (
<WebHoverOverlay
{...this.props}
{...this.state.hoverOverlayProps}
telemetryService={this.props.telemetryService}
hoverRef={this.nextOverlayElement}
onCloseButtonClick={this.nextCloseButtonClick}
/>
)}
</div>
)
}
private queryDiffs = (args: { first?: number }): Observable<GQL.IFileDiffConnection> =>
queryRepositoryComparisonFileDiffs({
...args,
repo: this.props.repo.id,
base: commitParentOrEmpty(this.state.commitOrError as GQL.IGitCommit),
head: (this.state.commitOrError as GQL.IGitCommit).oid,
})
}
function commitParentOrEmpty(commit: GQL.IGitCommit): string {
// 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is `git hash-object -t tree /dev/null`, which is used as the base
// when computing the `git diff` of the root commit.
return commit.parents.length > 0 ? commit.parents[0].oid : '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
}