Skip to content

Commit

Permalink
Fix git commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ggodlewski committed Dec 27, 2024
1 parent 8cb7d16 commit 39eec85
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 116 deletions.
22 changes: 4 additions & 18 deletions apps/ui/src/components/GitCommit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,15 @@ export default {
await disableElement(event, async () => {
const filePaths = [];
const removeFilePaths = [];
for (const checkedFileName of checkedFileNames) {
const change = this.gitChanges.find(change => change.path === checkedFileName);
if (!change?.state?.isDeleted) {
filePaths.push(checkedFileName);
} else {
removeFilePaths.push(checkedFileName);
}
filePaths.push(checkedFileName);
}
await this.commitBranch({
branch,
message: this.commitMsg,
filePaths: filePaths,
removeFilePaths
filePaths
});
this.commitMsg = '';
});
Expand All @@ -258,22 +251,15 @@ export default {
await disableElement(event, async () => {
const filePaths = [];
const removeFilePaths = [];
for (const checkedFileName of checkedFileNames) {
const change = this.gitChanges.find(change => change.path === checkedFileName);
if (change?.state?.isDeleted) {
removeFilePaths.push(checkedFileName);
} else {
filePaths.push(checkedFileName);
}
filePaths.push(checkedFileName);
}
try {
await this.commit({
message: this.commitMsg,
filePaths: filePaths,
removeFilePaths
filePaths
});
this.commitMsg = '';
} catch (err) {
Expand Down
6 changes: 2 additions & 4 deletions apps/ui/src/components/GitMixin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
export const GitMixin = {
methods: {
async commit({ message, filePaths, removeFilePaths }) {
async commit({ message, filePaths }) {
const response = await this.authenticatedClient.fetchApi(`/api/git/${this.driveId}/commit`, {
method: 'post',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({
filePaths,
removeFilePaths,
message: message
})
});
Expand All @@ -19,7 +18,7 @@ export const GitMixin = {
window.location.hash = '#drive_logs';
}
},
async commitBranch({ branch, message, filePaths, removeFilePaths }) {
async commitBranch({ branch, message, filePaths }) {
await this.authenticatedClient.fetchApi(`/api/run_action/${this.driveId}/branch`, {
method: 'post',
headers: {
Expand All @@ -28,7 +27,6 @@ export const GitMixin = {
body: JSON.stringify({
branch,
filePaths,
removeFilePaths,
message: message
})
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"main": "src/cli/wikigdrive.ts",
"scripts": {
"test": "ava test/*Test.ts test/**/*.ts",
"test": "ava --timeout=16s -c 8 test/*Test.ts test/**/*.ts",
"test:deno": "deno test test/*Test.ts test/**/*.ts",
"lint": "eslint ./apps/**/*.ts ./apps/**/*.vue ./src/**/*.ts ./test/**/*.ts",
"start": "wikigdrive server 3000",
Expand Down
25 changes: 4 additions & 21 deletions src/containers/job/JobManagerContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,31 +696,14 @@ export class JobManagerContainer extends Container {
}
}

private async gitCommit(driveId: FileId, jobId: string, message: string, filePaths: string[], removeFilePaths: string[], user) {
private async gitCommit(driveId: FileId, jobId: string, message: string, filePaths: string[], user) {
const logger = this.engine.logger.child({ filename: __filename, driveId, jobId });

const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(logger, transformedFileSystem.getRealPath(), '[email protected]');
await gitScanner.initialize();

const fileAssetsPaths = [];
for (const path of filePaths.filter(path => path.endsWith('.md'))) {
const assetsPath = path.substring(0, path.length - 3) + '.assets';
if (await transformedFileSystem.exists(assetsPath)) {
fileAssetsPaths.push(assetsPath);
}
}
const removeFileAssetsPaths = [];
for (const fileToRemove of removeFilePaths
.filter(path => path.endsWith('.md'))
.map(path => path.substring(0, path.length - 3) + '.assets')) {
removeFileAssetsPaths.push(fileToRemove);
}

filePaths.push(...fileAssetsPaths);
removeFilePaths.push(...removeFileAssetsPaths);

await gitScanner.commit(message, filePaths, removeFilePaths, user);
await gitScanner.commit(message, filePaths, user);

await this.schedule(driveId, {
...initJob(),
Expand Down Expand Up @@ -939,8 +922,8 @@ export class JobManagerContainer extends Container {
break;
case 'git_commit':
{
const { message, filePaths, removeFilePaths, user } = JSON.parse(currentJob.payload);
await this.gitCommit(driveId, currentJob.id, message, filePaths, removeFilePaths, user);
const { message, filePaths, user } = JSON.parse(currentJob.payload);
await this.gitCommit(driveId, currentJob.id, message, filePaths, user);
await this.clearGitCache(driveId);
}
break;
Expand Down
6 changes: 1 addition & 5 deletions src/containers/server/routes/GitController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {ContainerEngine} from '../../../ContainerEngine.ts';
interface CommitPost {
message: string;
filePaths: string[];
removeFilePaths: string[];
}

interface CmdPost {
Expand Down Expand Up @@ -73,16 +72,13 @@ export default class GitController extends Controller {
const filePaths: string[] = Array.isArray(body.filePaths)
? body.filePaths
: (body.filePaths ? [body.filePaths] : []);
const removeFilePaths: string[] = Array.isArray(body.removeFilePaths)
? body.removeFilePaths
: (body.removeFilePaths ? [body.removeFilePaths] : []);

await this.jobManagerContainer.schedule(driveId, {
...initJob(),
type: 'git_commit',
title: 'Git Commit',
payload: JSON.stringify({
message, filePaths, removeFilePaths, user
message, filePaths, user
})
});
return { driveId, message };
Expand Down
59 changes: 46 additions & 13 deletions src/git/GitScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ interface SshParams {
privateKeyFile: string;
}

interface Commiter {
name: string;
email: string;
}

function sanitize(txt) {
txt = txt.replace(/[;"|]/g, '');
return txt;
Expand Down Expand Up @@ -130,7 +135,7 @@ export class GitScanner {
const result = await this.exec(cmd, { skipLogger: true });
for (const line of result.stdout.split('\n')) {
const parts = line.split(/\s/);
const path = parts[parts.length - 1];
const path = parts[parts.length - 1].trim();

if (line.match(/^A\s/)) {
addEntry(path, { isNew: true });
Expand All @@ -149,17 +154,21 @@ export class GitScanner {
}

const untrackedResult = await this.exec(
'git -c core.quotepath=off ls-files --modified --deleted --others --exclude-standard',
'git status --short --untracked-files',
{ skipLogger: true }
);

for (const line of untrackedResult.stdout.split('\n')) {
if (!line.trim()) {
continue;
}
const path = line

const [status, path] = line
.replace(/\s+/g, ' ')
.trim()
.replace(/^"/, '')
.replace(/"$/, '');
.replace(/"$/, '')
.split(' ');

if (path.indexOf('.assets/') > -1 && !opts.includeAssets) {
const idx = path.indexOf('.assets/');
Expand All @@ -168,7 +177,14 @@ export class GitScanner {
continue;
}

addEntry(path, { isNew: true });
if (status === 'D') {
addEntry(path, { isDeleted: true });
} else
if (status === 'M') {
addEntry(path, { isModified: true });
} else {
addEntry(path, { isNew: true });
}
}

const retValArr: GitChange[] = Object.values(retVal);
Expand All @@ -178,11 +194,28 @@ export class GitScanner {
return retValArr;
}

async commit(message: string, addedFiles: string[], removedFiles: string[], committer): Promise<string> {
addedFiles = addedFiles.map(fileName => fileName.startsWith('/') ? fileName.substring(1) : fileName)
.filter(fileName => !! fileName);
removedFiles = removedFiles.map(fileName => fileName.startsWith('/') ? fileName.substring(1) : fileName)
.filter(fileName => !! fileName);
async commit(message: string, selectedFiles: string[], committer: Commiter): Promise<string> {
selectedFiles = selectedFiles.map(fileName => fileName.startsWith('/') ? fileName.substring(1) : fileName)
.filter(fileName => !!fileName);

const addedFiles: string[] = [];
const removedFiles: string[] = [];

const changes = await this.changes({ includeAssets: true });
for (const change of changes) {
let mdPath = change.path;
if (mdPath.indexOf('.assets/') > -1) {
mdPath = mdPath.replace(/.assets\/.*/, '.md');
}

if (selectedFiles.includes(mdPath)) {
if (change.state?.isDeleted) {
removedFiles.push(change.path);
} else {
addedFiles.push(change.path);
}
}
}

while (addedFiles.length > 0) {
const chunk = addedFiles.splice(0, 400);
Expand All @@ -197,7 +230,7 @@ export class GitScanner {
const rmParam = chunk.map(fileName => `"${sanitize(fileName)}"`).join(' ');
if (rmParam) {
try {
await this.exec(`git rm -r ${rmParam}`);
await this.exec(`git rm -r --ignore-unmatch ${rmParam}`);
} catch (err) {
if (err.message.indexOf('did not match any files') === -1) {
throw err;
Expand Down Expand Up @@ -803,7 +836,7 @@ export class GitScanner {
}
addedFiles.push(...fileAssetsPaths);

await this.commit('Auto commit for file version change', addedFiles, [], committer);
await this.commit('Auto commit for file version change', addedFiles, committer);
}
}

Expand Down Expand Up @@ -831,7 +864,7 @@ export class GitScanner {
let unstaged = 0;

try {
const untrackedResult = await this.exec('git -c core.quotepath=off ls-files --modified --deleted --others --exclude-standard', { skipLogger: true });
const untrackedResult = await this.exec('git status --short --untracked-files', { skipLogger: true });
for (const line of untrackedResult.stdout.split('\n')) {
if (!line.trim()) {
continue;
Expand Down
Loading

0 comments on commit 39eec85

Please sign in to comment.