Skip to content

Commit

Permalink
Fix removing assets
Browse files Browse the repository at this point in the history
Resolves: #449
  • Loading branch information
ggodlewski committed May 5, 2024
1 parent 4201645 commit 718f0d8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/containers/job/JobManagerContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,7 @@ export class JobManagerContainer extends Container {
for (const fileToRemove of removeFilePaths
.filter(path => path.endsWith('.md'))
.map(path => path.substring(0, path.length - 3) + '.assets')) {

if (!await transformedFileSystem.exists(fileToRemove)) {
continue;
}
removeFileAssetsPaths.push(fileToRemove + '/' + fileToRemove);
removeFileAssetsPaths.push(fileToRemove);
}

filePaths.push(...fileAssetsPaths);
Expand Down
12 changes: 8 additions & 4 deletions src/git/GitScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class GitScanner {
return fs.existsSync(path.join(this.rootPath, '.git'));
}

async changes(): Promise<GitChange[]> {
async changes(opts: { includeAssets: boolean } = { includeAssets: false }): Promise<GitChange[]> {
const retVal: { [path: string]: GitChange & { cnt: number } } = {};

const skipOthers = false;
Expand All @@ -114,7 +114,11 @@ export class GitScanner {
}

try {
const result = await this.exec('git --no-pager diff HEAD --name-status -- \':!**/*.assets/*.png\'', { skipLogger: true });
const cmd = !opts.includeAssets ?
'git --no-pager diff HEAD --name-status -- \':!**/*.assets/*.png\'' :
'git --no-pager diff HEAD --name-status --';

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];
Expand Down Expand Up @@ -149,7 +153,7 @@ export class GitScanner {
.replace(/^"/, '')
.replace(/"$/, '');

if (path.indexOf('.assets/') > -1) {
if (path.indexOf('.assets/') > -1 && !opts.includeAssets) {
const idx = path.indexOf('.assets/');
const mdPath = path.substring(0, idx) + '.md';
addEntry(mdPath, { isModified: true }, 1);
Expand Down Expand Up @@ -184,7 +188,7 @@ export class GitScanner {
const chunk = removedFiles.splice(0, 400);
const rmParam = chunk.map(fileName => `"${sanitize(fileName)}"`).join(' ');
if (rmParam) {
await this.exec(`git rm ${rmParam}`);
await this.exec(`git rm -r ${rmParam}`);
}
}

Expand Down
82 changes: 79 additions & 3 deletions test/git/GitTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {assert} from 'chai';
import winston from 'winston';
import {instrumentLogger} from '../../src/utils/logger/logger';
import {GitScanner} from '../../src/git/GitScanner';
import {createTmpDir} from '../utils';
import fs from 'fs';
import {rmSync, unlinkSync} from 'node:fs';
import path from 'path';
import {execSync} from 'child_process';

import {instrumentLogger} from '../../src/utils/logger/logger.ts';
import {GitScanner} from '../../src/git/GitScanner.ts';
import {createTmpDir} from '../utils.ts';

const COMMITER1 = {
name: 'John', email: '[email protected]'
};
Expand Down Expand Up @@ -609,4 +611,78 @@ describe('GitTest', function () {
}
});

it('test remove assets', async () => {
const localRepoDir: string = createTmpDir();

try {
const scannerLocal = new GitScanner(logger, localRepoDir, COMMITER1.email);
await scannerLocal.initialize();

const commit = async (filePaths = [], removeFilePaths = []) => {
const fileAssetsPaths = [];
for (const filePath of filePaths.filter(path => path.endsWith('.md'))) {
const assetsPath = filePath.substring(0, filePath.length - 3) + '.assets';
if (fs.existsSync(path.join(scannerLocal.rootPath, assetsPath))) {
fileAssetsPaths.push(assetsPath);
}
}
const removeFileAssetsPaths = [];
for (const fileToRemove of removeFilePaths
.filter(filePath => filePath.endsWith('.md'))
.map(filePath => filePath.substring(0, filePath.length - 3) + '.assets')) {

removeFileAssetsPaths.push(fileToRemove);
}

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

await scannerLocal.commit('initial commit', filePaths, removeFilePaths, COMMITER1);
};

fs.writeFileSync(path.join(scannerLocal.rootPath, 'test.md'), 'test');
fs.mkdirSync(path.join(scannerLocal.rootPath, 'test.assets'));

fs.writeFileSync(path.join(scannerLocal.rootPath, 'test.assets', '1.png'), '1');
fs.writeFileSync(path.join(scannerLocal.rootPath, 'test.assets', '2.png'), '2');

{
const changes = await scannerLocal.changes({ includeAssets: true });
assert.equal(changes.length, 4);
assert.ok(!!changes.find(item => item.path === '.gitignore'));
assert.ok(!!changes.find(item => item.path === 'test.md'));
assert.ok(!!changes.find(item => item.path === 'test.assets/1.png'));
assert.ok(!!changes.find(item => item.path === 'test.assets/2.png'));
}

await commit(['.gitignore', 'test.md'], []);

{
const changes = await scannerLocal.changes({ includeAssets: true });
assert.equal(changes.length, 0);
}

unlinkSync(path.join(scannerLocal.rootPath, 'test.md'));
rmSync(path.join(scannerLocal.rootPath, 'test.assets'), { recursive: true, force: true });

{
const changes = await scannerLocal.changes({ includeAssets: true });
assert.equal(changes.length, 3);
assert.ok(!!changes.find(item => item.path === 'test.md'));
assert.ok(!!changes.find(item => item.path === 'test.assets/1.png'));
assert.ok(!!changes.find(item => item.path === 'test.assets/2.png'));
}

await commit([], ['test.md']);

{
const changes = await scannerLocal.changes({ includeAssets: true });
assert.equal(changes.length, 0);
}

} finally {
fs.rmSync(localRepoDir, { recursive: true, force: true });
}
});

});

0 comments on commit 718f0d8

Please sign in to comment.