Skip to content

🎥 Make videos programmatically with React

implement git ref

Jonny Burgerbea05a397be9
diff --git a/packages/cli/src/get-github-repository.ts b/packages/cli/src/get-github-repository.ts
index 525bf83a94..5901b8c539 100644
--- a/packages/cli/src/get-github-repository.ts
+++ b/packages/cli/src/get-github-repository.ts
@@ -37,7 +37,7 @@ const getGitRemotes = (lines: string[]) => {
});
};
-export const getGitRemoteOrigin = (remotionRoot: string) => {
+export const getGitConfig = (remotionRoot: string) => {
const gitFolder = BundlerInternals.findClosestFolderWithItem(
remotionRoot,
'.git',
@@ -51,6 +51,10 @@ export const getGitRemoteOrigin = (remotionRoot: string) => {
return null;
}
+ return gitConfig;
+};
+
+export const getGitRemoteOrigin = (gitConfig: string) => {
const contents = readFileSync(gitConfig, 'utf-8');
const lines = contents.split('\n');
@@ -97,7 +101,12 @@ export const getGitSource = (remotionRoot: string): GitSource | null => {
return null;
}
- const origin = getGitRemoteOrigin(remotionRoot);
+ const gitConfig = getGitConfig(remotionRoot);
+ if (!gitConfig) {
+ return null;
+ }
+
+ const origin = getGitRemoteOrigin(gitConfig);
if (!origin || !origin.url) {
return null;
}
@@ -107,10 +116,14 @@ export const getGitSource = (remotionRoot: string): GitSource | null => {
return null;
}
+ const gitRoot = path.dirname(path.dirname(gitConfig));
+ const relativeFromGitRoot = path.relative(gitRoot, remotionRoot);
+
return {
name: parsed.name,
org: parsed.org,
ref,
type: 'github',
+ relativeFromGitRoot,
};
};
diff --git a/packages/cli/src/test/get-github-repo.test.ts b/packages/cli/src/test/get-github-repo.test.ts
index a8159e2a6e..2a215acbfe 100644
--- a/packages/cli/src/test/get-github-repo.test.ts
+++ b/packages/cli/src/test/get-github-repo.test.ts
@@ -2,13 +2,15 @@ import {expect} from 'bun:test';
import {test} from 'vitest';
import {
getGifRef,
+ getGitConfig,
getGitRemoteOrigin,
getGitSource,
normalizeGitRemoteUrl,
} from '../get-github-repository';
test('Get GitHub repo', () => {
- expect(getGitRemoteOrigin(__dirname)).toEqual({
+ const gitConfig = getGitConfig(__dirname);
+ expect(getGitRemoteOrigin(gitConfig as string)).toEqual({
remote: 'origin',
url: 'https://github.com/remotion-dev/remotion.git',
});
@@ -41,4 +43,5 @@ test('Should get Gif Ref', () => {
test('Should get Git Source', () => {
const git = getGitSource(process.cwd());
expect(git).not.toBeNull();
+ expect(git?.relativeFromGitRoot).toBe('packages/cli');
});
diff --git a/packages/studio-shared/src/git-source.ts b/packages/studio-shared/src/git-source.ts
index d0dc26cb44..eedba699bb 100644
--- a/packages/studio-shared/src/git-source.ts
+++ b/packages/studio-shared/src/git-source.ts
@@ -3,4 +3,5 @@ export type GitSource = {
org: string;
name: string;
ref: string;
+ relativeFromGitRoot: string;
};
diff --git a/packages/studio/src/components/MenuBuildIndicator.tsx b/packages/studio/src/components/MenuBuildIndicator.tsx
index 4b71f677ef..e3344cff09 100644
--- a/packages/studio/src/components/MenuBuildIndicator.tsx
+++ b/packages/studio/src/components/MenuBuildIndicator.tsx
@@ -58,7 +58,11 @@ export const MenuBuildIndicator: React.FC = () => {
{showButton ? <Spacing x={0.5} /> : null}
{window.remotion_projectName}
{showButton ? <Spacing x={0.25} /> : null}
- {showButton ? <OpenEditorButton /> : null}
+ {showButton ? (
+ <OpenEditorButton type="editor" />
+ ) : window.remotion_gitSource ? (
+ <OpenEditorButton type="git" />
+ ) : null}
</div>
);
};
diff --git a/packages/studio/src/components/OpenEditorButton.tsx b/packages/studio/src/components/OpenEditorButton.tsx
index 3bd551bade..e788ef2235 100644
--- a/packages/studio/src/components/OpenEditorButton.tsx
+++ b/packages/studio/src/components/OpenEditorButton.tsx
@@ -1,5 +1,10 @@
+import type {GitSource} from '@remotion/studio-shared';
import {useCallback, useMemo, useState} from 'react';
import {LIGHT_TEXT} from '../helpers/colors';
+import {
+ getGitSourceBranchUrl,
+ getGitSourceName,
+} from '../helpers/get-git-menu-item';
import {openInEditor} from '../helpers/open-in-editor';
import {sendErrorNotification} from './Notifications/NotificationCenter';
@@ -16,35 +21,52 @@ const buttonStyle: React.CSSProperties = {
alignItems: 'center',
};
-export const OpenEditorButton: React.FC<{}> = () => {
+export const OpenEditorButton: React.FC<{
+ type: 'git' | 'editor';
+}> = ({type}) => {
const [hovered, setHovered] = useState<boolean>(false);
const svgFillColor = useMemo(() => {
return hovered ? 'white' : LIGHT_TEXT;
}, [hovered]);
- const handleClick = async () => {
- await openInEditor({
- originalFileName: `${window.remotion_cwd}`,
- originalLineNumber: 1,
- originalColumnNumber: 1,
- originalFunctionName: null,
- originalScriptCode: null,
- })
- .then((res) => res.json())
- .then(({success}) => {
- if (!success) {
- sendErrorNotification(`Could not open ${window.remotion_editorName}`);
- }
+ const handleClick = useCallback(async () => {
+ if (type === 'editor') {
+ await openInEditor({
+ originalFileName: `${window.remotion_cwd}`,
+ originalLineNumber: 1,
+ originalColumnNumber: 1,
+ originalFunctionName: null,
+ originalScriptCode: null,
})
- .catch((err) => {
- // eslint-disable-next-line no-console
- console.error(err);
- sendErrorNotification(`Could not open ${window.remotion_editorName}`);
- });
- };
+ .then((res) => res.json())
+ .then(({success}) => {
+ if (!success) {
+ sendErrorNotification(
+ `Could not open ${window.remotion_editorName}`,
+ );
+ }
+ })
+ .catch((err) => {
+ // eslint-disable-next-line no-console
+ console.error(err);
+ sendErrorNotification(`Could not open ${window.remotion_editorName}`);
+ });
+ }
+
+ if (type === 'git') {
+ if (!window.remotion_gitSource) {
+ throw new Error('No git source');
+ }
- const buttonTooltip = `Open in ${window.remotion_editorName}`;
+ window.open(getGitSourceBranchUrl(window.remotion_gitSource), '_blank');
+ }
+ }, [type]);
+
+ const buttonTooltip =
+ type === 'git'
+ ? `Open ${getGitSourceName(window.remotion_gitSource as GitSource)} Repo`
+ : `Open in ${window.remotion_editorName}`;
const openInEditorSvg = (
<svg viewBox="0 0 512 512" style={svgStyle}>
<path
@@ -53,6 +75,7 @@ export const OpenEditorButton: React.FC<{}> = () => {
/>
</svg>
);
+
const onPointerEnter = useCallback(() => {
setHovered(true);
}, []);
diff --git a/packages/studio/src/components/Timeline/TimelineStack/index.tsx b/packages/studio/src/components/Timeline/TimelineStack/index.tsx
index 9ee503aeab..51527fb547 100644
--- a/packages/studio/src/components/Timeline/TimelineStack/index.tsx
+++ b/packages/studio/src/components/Timeline/TimelineStack/index.tsx
@@ -1,13 +1,22 @@
+import type {GitSource} from '@remotion/studio-shared';
import {SOURCE_MAP_ENDPOINT} from '@remotion/studio-shared';
-import React, {useCallback, useEffect, useMemo, useState} from 'react';
+import React, {
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useState,
+} from 'react';
import type {TSequence} from 'remotion';
import {SourceMapConsumer} from 'source-map';
import type {OriginalPosition} from '../../../error-overlay/react-overlay/utils/get-source-map';
+import {StudioServerConnectionCtx} from '../../../helpers/client-id';
import {
LIGHT_COLOR,
LIGHT_TEXT,
VERY_LIGHT_TEXT,
} from '../../../helpers/colors';
+import {getGitRefUrl} from '../../../helpers/get-git-menu-item';
import {openInEditor} from '../../../helpers/open-in-editor';
import {pushUrl} from '../../../helpers/url-state';
import {useSelectAsset} from '../../InitialCompositionLoader';
@@ -34,6 +43,9 @@ export const TimelineStack: React.FC<{
const [opening, setOpening] = useState(false);
const selectAsset = useSelectAsset();
+ const connectionStatus = useContext(StudioServerConnectionCtx)
+ .previewServerState.type;
+
const assetPath = useMemo(() => {
if (sequence.type !== 'video' && sequence.type !== 'audio') {
return null;
@@ -80,7 +92,22 @@ export const TimelineStack: React.FC<{
}
}, []);
+ const canOpenInEditor =
+ window.remotion_editorName &&
+ connectionStatus === 'connected' &&
+ originalLocation;
+
+ const canOpenInGitHub = window.remotion_gitSource && originalLocation;
+
+ const titleHoverable =
+ (isCompact && (canOpenInEditor || canOpenInGitHub)) || assetPath;
+ const stackHoverable = !isCompact && (canOpenInEditor || canOpenInGitHub);
+
const onClickTitle = useCallback(() => {
+ if (!titleHoverable) {
+ return null;
+ }
+
if (assetPath) {
navigateToAsset(assetPath);
return;
@@ -90,16 +117,44 @@ export const TimelineStack: React.FC<{
return;
}
- openEditor(originalLocation);
- }, [assetPath, navigateToAsset, openEditor, originalLocation]);
+ if (canOpenInEditor) {
+ openEditor(originalLocation);
+ return;
+ }
+
+ if (canOpenInGitHub) {
+ window.open(
+ getGitRefUrl(window.remotion_gitSource as GitSource, originalLocation),
+ '_blank',
+ );
+ }
+ }, [
+ assetPath,
+ canOpenInEditor,
+ canOpenInGitHub,
+ navigateToAsset,
+ openEditor,
+ originalLocation,
+ titleHoverable,
+ ]);
const onClickStack = useCallback(() => {
if (!originalLocation) {
return;
}
- openEditor(originalLocation);
- }, [openEditor, originalLocation]);
+ if (canOpenInEditor) {
+ openEditor(originalLocation);
+ return;
+ }
+
+ if (canOpenInGitHub) {
+ window.open(
+ getGitRefUrl(window.remotion_gitSource as GitSource, originalLocation),
+ '_blank',
+ );
+ }
+ }, [canOpenInEditor, canOpenInGitHub, openEditor, originalLocation]);
useEffect(() => {
if (!sequence.stack) {
@@ -132,20 +187,16 @@ export const TimelineStack: React.FC<{
setTitleHovered(false);
}, []);
- const hoverable =
- (originalLocation && isCompact) ||
- (assetPath && window.remotion_editorName);
-
const style = useMemo((): React.CSSProperties => {
return {
fontSize: 12,
color: opening
? VERY_LIGHT_TEXT
- : stackHovered && hoverable
+ : stackHovered && stackHoverable
? LIGHT_TEXT
: VERY_LIGHT_TEXT,
marginLeft: 10,
- cursor: hoverable ? 'pointer' : undefined,
+ cursor: stackHoverable ? 'pointer' : undefined,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
@@ -154,10 +205,10 @@ export const TimelineStack: React.FC<{
overflow: 'hidden',
flexShrink: 100000,
};
- }, [opening, stackHovered, hoverable]);
+ }, [opening, stackHovered, stackHoverable]);
- const textStyle: React.CSSProperties = useMemo(() => {
- const hoverEffect = titleHovered && hoverable;
+ const titleStyle: React.CSSProperties = useMemo(() => {
+ const hoverEffect = titleHovered && titleHoverable;
return {
fontSize: 12,
whiteSpace: 'nowrap',
@@ -169,7 +220,7 @@ export const TimelineStack: React.FC<{
borderBottom: hoverEffect ? '1px solid #fff' : 'none',
cursor: hoverEffect ? 'pointer' : undefined,
};
- }, [hoverable, isCompact, opening, titleHovered]);
+ }, [titleHoverable, isCompact, opening, titleHovered]);
const text =
sequence.displayName.length > 1000
@@ -186,7 +237,7 @@ export const TimelineStack: React.FC<{
? getOriginalSourceAttribution(originalLocation)
: text || '<Sequence>'
}
- style={textStyle}
+ style={titleStyle}
onClick={onClickTitle}
>
{text || '<Sequence>'}
diff --git a/packages/studio/src/helpers/get-git-menu-item.ts b/packages/studio/src/helpers/get-git-menu-item.ts
index b83c7c0c4b..9a98067f85 100644
--- a/packages/studio/src/helpers/get-git-menu-item.ts
+++ b/packages/studio/src/helpers/get-git-menu-item.ts
@@ -1,5 +1,6 @@
import type {GitSource} from '@remotion/studio-shared';
import type {ComboboxValue} from '../components/NewComposition/ComboBox';
+import type {OriginalPosition} from '../error-overlay/react-overlay/utils/get-source-map';
export const getGitSourceName = (gitSource: GitSource) => {
if (gitSource.type === 'github') {
@@ -9,7 +10,7 @@ export const getGitSourceName = (gitSource: GitSource) => {
throw new Error('Unknown git source type');
};
-export const getGitSourceRepoName = (gitSource: GitSource) => {
+export const getGitSourceBranchUrl = (gitSource: GitSource) => {
if (gitSource.type === 'github') {
return `https://github.com/${gitSource.org}/${gitSource.name}/tree/${gitSource.ref}`;
}
@@ -17,6 +18,21 @@ export const getGitSourceRepoName = (gitSource: GitSource) => {
throw new Error('Unknown git source type');
};
+export const getGitRefUrl = (
+ gitSource: GitSource,
+ originalLocation: OriginalPosition,
+) => {
+ if (gitSource.type === 'github') {
+ return `https://github.com/${gitSource.org}/${gitSource.name}/tree/${
+ gitSource.ref
+ }/${
+ gitSource.relativeFromGitRoot ? `${gitSource.relativeFromGitRoot}/` : ''
+ }${originalLocation.source}#L${originalLocation.line}`;
+ }
+
+ throw new Error('Unknown git source type');
+};
+
export const getGitMenuItem = (): ComboboxValue | null => {
if (!window.remotion_gitSource) {
return null;
@@ -28,7 +44,7 @@ export const getGitMenuItem = (): ComboboxValue | null => {
label: `Open ${getGitSourceName(window.remotion_gitSource)} Repo`,
onClick: () => {
window.open(
- getGitSourceRepoName(window.remotion_gitSource as GitSource),
+ getGitSourceBranchUrl(window.remotion_gitSource as GitSource),
'_blank',
);
},