feat: Allow relative git repository (#41)

This commit is contained in:
Eric Tuvesson
2024-06-26 21:11:50 +02:00
committed by GitHub
parent fa282d6169
commit 3fb3668fc3
8 changed files with 103 additions and 38 deletions

View File

@@ -16,6 +16,7 @@ import { projectFromDirectory, unzipIntoDirectory } from '../models/projectmodel
import FileSystem from './filesystem'; import FileSystem from './filesystem';
import { tracker } from './tracker'; import { tracker } from './tracker';
import { guid } from './utils'; import { guid } from './utils';
import { getTopLevelWorkingDirectory } from '@noodl/git/src/core/open';
export interface ProjectItem { export interface ProjectItem {
id: string; id: string;
@@ -267,12 +268,15 @@ export class LocalProjectsModel extends Model {
}); });
} }
isGitProject(project: ProjectModel): boolean { /**
// TODO: check if there's is git in any parent folder too * Check if this project is in a git repository.
*
// Check if the git folder exists. * @param project
const gitPath = filesystem.join(project._retainedProjectDirectory, '.git'); * @returns
return filesystem.exists(gitPath); */
async isGitProject(project: ProjectModel): Promise<boolean> {
const gitPath = await getTopLevelWorkingDirectory(project._retainedProjectDirectory);
return gitPath !== null;
} }
setCurrentGlobalGitAuth(projectId: string) { setCurrentGlobalGitAuth(projectId: string) {

View File

@@ -11,6 +11,14 @@ export interface ArrayDiff<T> {
changed: T[]; changed: T[];
unchanged: T[]; unchanged: T[];
} }
export function createEmptyArrayDiff<T>(): ArrayDiff<T> {
return {
deleted: [],
created: [],
changed: [],
unchanged: [],
}
}
export interface ProjectDiffItem { export interface ProjectDiffItem {
graph: TSFixme; graph: TSFixme;

View File

@@ -199,6 +199,7 @@ function BaseVersionControlPanel() {
export function VersionControlPanel() { export function VersionControlPanel() {
const [git, setGit] = useState<Git>(null); const [git, setGit] = useState<Git>(null);
const [state, setState] = useState<'loading' | 'loaded' | 'not-git'>('loading');
async function createGit() { async function createGit() {
const gitClient = new Git(mergeProject); const gitClient = new Git(mergeProject);
@@ -206,12 +207,18 @@ export function VersionControlPanel() {
setGit(gitClient); setGit(gitClient);
} }
const isGitProject = git === null ? LocalProjectsModel.instance.isGitProject(ProjectModel.instance) : true;
useEffect(() => { useEffect(() => {
if (isGitProject) { LocalProjectsModel.instance
createGit(); .isGitProject(ProjectModel.instance)
} .then(async (isGitProject) => {
}, [isGitProject]); if (isGitProject) {
await createGit();
setState('loaded');
} else {
setState('not-git');
}
});
}, []);
async function setupGit() { async function setupGit() {
const gitClient = new Git(mergeProject); const gitClient = new Git(mergeProject);
@@ -220,7 +227,7 @@ export function VersionControlPanel() {
setGit(gitClient); setGit(gitClient);
} }
if (git === null && !isGitProject) { if (git === null && state === 'not-git') {
return ( return (
<BasePanel isFill title="Version Control"> <BasePanel isFill title="Version Control">
<Box hasXSpacing hasYSpacing> <Box hasXSpacing hasYSpacing>

View File

@@ -6,11 +6,13 @@ import { Commit } from '@noodl/git/src/core/models/snapshot';
import { FileChange } from '@noodl/git/src/core/models/status'; import { FileChange } from '@noodl/git/src/core/models/status';
import { revRange } from '@noodl/git/src/core/rev-list'; import { revRange } from '@noodl/git/src/core/rev-list';
import { ProjectModel } from '@noodl-models/projectmodel';
import { applyPatches } from '@noodl-models/ProjectPatches/applypatches'; import { applyPatches } from '@noodl-models/ProjectPatches/applypatches';
import { mergeProject } from '@noodl-utils/projectmerger'; import { mergeProject } from '@noodl-utils/projectmerger';
import { ProjectDiff, diffProject } from '@noodl-utils/projectmerger.diff'; import { ProjectDiff, diffProject } from '@noodl-utils/projectmerger.diff';
import { useVersionControlContext } from '../context'; import { useVersionControlContext } from '../context';
import { getProjectFilePath } from '../context/DiffUtils';
import { DiffList } from './DiffList'; import { DiffList } from './DiffList';
//Kind: //Kind:
@@ -124,7 +126,10 @@ async function getMergeDiff(repositoryPath: string, commit: Commit, refToDiffTo:
} }
async function getProjectFile(commit: Commit) { async function getProjectFile(commit: Commit) {
const projectContent = JSON.parse(await commit.getFileAsString('project.json')); const projectFilePath = getProjectFilePath(commit.repositoryDir, ProjectModel.instance._retainedProjectDirectory);
const projectContentRaw = await commit.getFileAsString(projectFilePath);
const projectContent = JSON.parse(projectContentRaw);
applyPatches(projectContent); applyPatches(projectContent);
return projectContent; return projectContent;
} }

View File

@@ -61,7 +61,7 @@ export function DiffList({ diff, fileChanges, componentDiffTitle, actions, commi
const [imageDiff, setImageDiff] = useState<IImageDiff>(null); const [imageDiff, setImageDiff] = useState<IImageDiff>(null);
const components = diff?.components ? getChangedComponents(diff.components) : []; const components = diff?.components ? getChangedComponents(diff.components) : [];
const files = (fileChanges || [])?.filter((f) => f.path !== 'project.json') || []; const files = (fileChanges || [])?.filter((f) => !f.path.endsWith('project.json')) || [];
const settings = diff?.settings ? getChangedObjectProperties(diff.settings) : []; const settings = diff?.settings ? getChangedObjectProperties(diff.settings) : [];
const colorStyles = diff?.styles.colors ? getChangedObjectProperties(diff.styles.colors) : []; const colorStyles = diff?.styles.colors ? getChangedObjectProperties(diff.styles.colors) : [];
const textStyles = diff?.styles.text ? getChangedObjectProperties(diff.styles.text) : []; const textStyles = diff?.styles.text ? getChangedObjectProperties(diff.styles.text) : [];

View File

@@ -1,21 +1,24 @@
import path from 'path';
import { getCommit } from '@noodl/git/src/core/logs';
import { FileStatusKind } from '@noodl/git/src/core/models/status';
import { FeedbackType } from '@noodl-constants/FeedbackType';
import { applyPatches } from '@noodl-models/ProjectPatches/applypatches';
import { import {
ProjectDiff, ProjectDiff,
ProjectDiffItem, ProjectDiffItem,
ProjectBasicDiffItem, ProjectBasicDiffItem,
ArrayDiff, ArrayDiff,
diffProject diffProject,
createEmptyArrayDiff
} from '@noodl-utils/projectmerger.diff'; } from '@noodl-utils/projectmerger.diff';
import { IconName } from '@noodl-core-ui/components/common/Icon';
import { ListItemProps } from '@noodl-core-ui/components/layout/ListItem';
import { ProjectModel } from '../../../../models/projectmodel'; import { ProjectModel } from '../../../../models/projectmodel';
import { applyPatches } from '@noodl-models/ProjectPatches/applypatches'; export interface ProjectLocalDiff extends ProjectDiff {
import { FileStatusKind } from '@noodl/git/src/core/models/status';
import { IconName } from '@noodl-core-ui/components/common/Icon';
import { ListItemProps } from '@noodl-core-ui/components/layout/ListItem';
import { FeedbackType } from '@noodl-constants/FeedbackType';
import { getCommit } from '@noodl/git/src/core/logs';
export interface ProjectLocalDiff extends ProjectDiff{
baseProject: TSFixme; //Project model as an object from raw json baseProject: TSFixme; //Project model as an object from raw json
commitShaDiffedTo: string; commitShaDiffedTo: string;
} }
@@ -84,17 +87,49 @@ export function getFileStatusIconProps(status: FileStatusKind): Partial<ListItem
} }
} }
export async function doLocalDiff(repositoryPath: string, headCommitId: string): Promise<ProjectLocalDiff> { export function getProjectFilePath(repositoryPath: string, projectPath: string) {
const baseCommit = await getCommit(repositoryPath, headCommitId); const relativePath = path.relative(repositoryPath, projectPath);
const baseProjectJson = await baseCommit.getFileAsString('project.json'); const projectFilePath = path.join(relativePath, 'project.json').replaceAll('\\', '/');
const baseProject = JSON.parse(baseProjectJson); return projectFilePath;
applyPatches(baseProject); }
const diff = diffProject(baseProject, ProjectModel.instance.toJSON()); export async function doLocalDiff(
repositoryPath: string,
return { projectPath: string,
...diff, headCommitId: string
baseProject, ): Promise<ProjectLocalDiff> {
commitShaDiffedTo: headCommitId const projectFilePath = getProjectFilePath(repositoryPath, projectPath);
};
try {
const baseCommit = await getCommit(projectPath, headCommitId);
const baseProjectJson = await baseCommit.getFileAsString(projectFilePath);
const baseProject = JSON.parse(baseProjectJson);
applyPatches(baseProject);
const diff = diffProject(baseProject, ProjectModel.instance.toJSON());
return {
...diff,
baseProject,
commitShaDiffedTo: headCommitId
};
} catch (error) {
if (error.toString().includes('exists on disk, but not in')) {
console.warn('project.json does not exist in this commit.');
}
// Return empty state
return {
baseProject: {},
commitShaDiffedTo: headCommitId,
components: createEmptyArrayDiff(),
variants: createEmptyArrayDiff(),
settings: createEmptyArrayDiff(),
styles: {
colors: createEmptyArrayDiff(),
text: createEmptyArrayDiff()
},
cloudservices: createEmptyArrayDiff()
};
}
} }

View File

@@ -8,6 +8,7 @@ import { Slot } from '@noodl-core-ui/types/global';
import { doLocalDiff, ProjectLocalDiff } from './DiffUtils'; import { doLocalDiff, ProjectLocalDiff } from './DiffUtils';
import { useVersionControlFetch } from './fetch.context'; import { useVersionControlFetch } from './fetch.context';
import { BranchStatus, IVersionControlContext } from './types'; import { BranchStatus, IVersionControlContext } from './types';
import { ProjectModel } from '@noodl-models/projectmodel';
const VersionControlContext = createContext<IVersionControlContext>({ const VersionControlContext = createContext<IVersionControlContext>({
git: null, git: null,
@@ -57,7 +58,8 @@ export function VersionControlProvider({ git, children }: { git: Git; children:
(async () => { (async () => {
const currentCommitSha = await git.getHeadCommitId(); const currentCommitSha = await git.getHeadCommitId();
if (currentCommitSha) { if (currentCommitSha) {
const diff = await doLocalDiff(git.repositoryPath, currentCommitSha); const projectPath = ProjectModel.instance._retainedProjectDirectory;
const diff = await doLocalDiff(git.repositoryPath, projectPath, currentCommitSha);
setLocalDiff(diff); setLocalDiff(diff);
} }
})(); })();

View File

@@ -22,7 +22,11 @@ export async function open(basePath: string): Promise<string> {
// console.log("VCS error when opening project: " + e); // console.log("VCS error when opening project: " + e);
// } // }
return basePath;
// Find the relative git repository path
const repositoryPath = await getTopLevelWorkingDirectory(basePath);
return repositoryPath;
} }
/** /**