Skip to content

Commit 34e41eb

Browse files
author
SPRINX0\prochazka
committed
new file identifiers
1 parent 4609cf8 commit 34e41eb

File tree

4 files changed

+88
-26
lines changed

4 files changed

+88
-26
lines changed

src/diflow.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ describe('Git Repository Tests', () => {
193193
});
194194

195195
test('Adding file to merged', async () => {
196-
await createTestCommit(getTestRepoPath('merged'), 'outer.txt', 'outer', 'merged');
197-
await createTestCommit(getTestRepoPath('merged'), 'folder/inner.txt', 'inner', 'merged');
196+
await createTestCommit(getTestRepoPath('merged'), 'normal-folder/diff.txt', 'diff content', 'merged');
197+
await createTestCommit(getTestRepoPath('merged'), 'base-folder/base.txt', 'base content', 'merged');
198198

199199
await beforeDiflow();
200200

@@ -205,10 +205,10 @@ describe('Git Repository Tests', () => {
205205

206206
await checkStateInConfig();
207207

208-
expect(await fs.exists(path.join(getTestRepoPath('diff'), 'outer.txt'))).toBe(true);
209-
expect(await fs.exists(path.join(getTestRepoPath('base'), 'outer.txt'))).toBe(false);
208+
expect(await fs.exists(path.join(getTestRepoPath('diff'), 'normal-folder/diff.txt'))).toBe(true);
209+
expect(await fs.exists(path.join(getTestRepoPath('base'), 'normal-folder/diff.txt'))).toBe(false);
210210

211-
expect(await fs.exists(path.join(getTestRepoPath('diff'), 'folder/inner.txt'))).toBe(false);
212-
expect(await fs.exists(path.join(getTestRepoPath('base'), 'folder/inner.txt'))).toBe(true);
211+
expect(await fs.exists(path.join(getTestRepoPath('diff'), 'base-folder/base.txt'))).toBe(false);
212+
expect(await fs.exists(path.join(getTestRepoPath('base'), 'base-folder/base.txt'))).toBe(true);
213213
});
214214
});

src/processor.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
repoHasModifications,
1313
runGitCommand,
1414
} from './tools';
15-
import { ChangeItem, Config, RepoId, State } from './types';
15+
import { ChangeItem, Config, RepoId, RepoIdentifier, State } from './types';
1616
import { minimatch } from 'minimatch';
1717
import { rimraf } from 'rimraf';
1818

@@ -245,13 +245,56 @@ class CommitProcessor {
245245
}
246246
}
247247

248+
async matchIdentifiers(repoPath: string, file: string, identifiers?: RepoIdentifier[]) {
249+
if (!identifiers) {
250+
return false;
251+
}
252+
253+
let content: string | null = null;
254+
for (const identifier of identifiers) {
255+
if (identifier.content) {
256+
if (content == null) {
257+
content = await fs.readFile(path.join(repoPath, file), 'utf8');
258+
}
259+
if (content.includes(identifier.content)) {
260+
return true;
261+
}
262+
}
263+
if (identifier.name) {
264+
if (minimatch(file, identifier.name, { partial: true })) {
265+
return true;
266+
}
267+
}
268+
}
269+
return false;
270+
}
271+
248272
async processMergedFile(file: ChangeItem) {
249273
if (file.action === 'A') {
250-
if (await fs.exists(path.join(this.processor.repoPaths.diff, path.dirname(file.file)))) {
251-
// if exists folder, copy to diff
252-
await copyRepoFile(this.processor.repoPaths.merged, this.processor.repoPaths.diff, file.file);
253-
} else {
274+
let target = this.processor.config?.newFilesTargetDefault ?? 'diff';
275+
if (
276+
await this.matchIdentifiers(
277+
this.processor.repoPaths.diff,
278+
file.file,
279+
this.processor.config?.repos.base.identifiers
280+
)
281+
) {
282+
target = 'base';
283+
}
284+
if (
285+
await this.matchIdentifiers(
286+
this.processor.repoPaths.diff,
287+
file.file,
288+
this.processor.config?.repos.diff.identifiers
289+
)
290+
) {
291+
target = 'diff';
292+
}
293+
294+
if (target === 'base') {
254295
await copyRepoFile(this.processor.repoPaths.merged, this.processor.repoPaths.base, file.file);
296+
} else {
297+
await copyRepoFile(this.processor.repoPaths.merged, this.processor.repoPaths.diff, file.file);
255298
}
256299
} else if (file.action === 'M') {
257300
if (await repoFileExists(this.processor.repoPaths.diff, file.file)) {

src/testrepo.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs-extra';
22
import * as path from 'path';
33
import { rimraf } from 'rimraf';
44
import { execAsync, getCommits, getHeadCommitInRepo, sleep } from './tools';
5-
import { State } from './types';
5+
import { Config, State } from './types';
66
import _ from 'lodash';
77

88
export function getTestRepoPath(repo: string) {
@@ -62,20 +62,28 @@ export async function initTestRepos() {
6262
const diffHash = await createTestCommit(getTestRepoPath('diff'), 'file1.txt', 'different content', 'diff');
6363
const mergedHash = await createTestCommit(getTestRepoPath('merged'), 'file1.txt', 'different content', 'merged');
6464

65-
// Create config.json in config repo
66-
const configContent = JSON.stringify(
67-
{
68-
branches: ['master'],
69-
repos: {
70-
base: { url: getTestRepoPath('base') },
71-
diff: { url: getTestRepoPath('diff') },
72-
merged: { url: getTestRepoPath('merged') },
65+
const config: Config = {
66+
repos: {
67+
base: {
68+
url: getTestRepoPath('base'),
69+
identifiers: [
70+
{
71+
name: 'base-folder/**',
72+
},
73+
],
74+
},
75+
diff: {
76+
url: getTestRepoPath('diff'),
77+
},
78+
merged: {
79+
url: getTestRepoPath('merged'),
7380
},
74-
ignorePaths: ['.github/**'],
7581
},
76-
null,
77-
2
78-
);
82+
ignorePaths: ['.github/**'],
83+
};
84+
85+
// Create config.json in config repo
86+
const configContent = JSON.stringify(config, null, 2);
7987
await createTestCommit(getTestRepoPath('config'), 'config.json', configContent, 'config');
8088

8189
// Create state.json in config repo

src/types.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@ export interface RepoConfig {
55
url: string;
66
}
77

8+
export interface RepoIdentifier {
9+
content?: string;
10+
name?: string;
11+
// applyOnExisting?: boolean;
12+
}
13+
14+
export interface SourceRepoConfig extends RepoConfig {
15+
identifiers?: RepoIdentifier[];
16+
}
17+
818
export interface Config {
919
repos: {
10-
base: RepoConfig;
11-
diff: RepoConfig;
20+
base: SourceRepoConfig;
21+
diff: SourceRepoConfig;
1222
merged: RepoConfig;
1323
};
1424
ignorePaths: string[];
1525
syncCommitPrefix?: string;
26+
newFilesTargetDefault?: 'base' | 'diff'; // default: diff
1627
}
1728

1829
export interface State {

0 commit comments

Comments
 (0)