Skip to content

Commit c859bd0

Browse files
author
SPRINX0\prochazka
committed
rename file support
1 parent 47ada3a commit c859bd0

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

src/diflow.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
beforeDiflow,
66
checkStateInConfig,
77
createTestCommit,
8+
createTestCommitCore,
89
getTestRepoPath,
910
initTestRepos,
1011
} from './testrepo';
@@ -211,4 +212,37 @@ describe('Git Repository Tests', () => {
211212
expect(await fs.exists(path.join(getTestRepoPath('diff'), 'base-folder/base.txt'))).toBe(false);
212213
expect(await fs.exists(path.join(getTestRepoPath('base'), 'base-folder/base.txt'))).toBe(true);
213214
});
215+
216+
test('Rename files', async () => {
217+
// await execAsync(`git mv only-base.txt only-base-renamed.txt`, { cwd: getTestRepoPath('merged') });
218+
219+
await fs.rename(
220+
path.join(getTestRepoPath('merged'), 'only-base.txt'),
221+
path.join(getTestRepoPath('merged'), 'only-base-renamed.txt')
222+
);
223+
await fs.writeFile(
224+
path.join(getTestRepoPath('merged'), 'only-base-renamed.txt'),
225+
'only-base content\nline 1\nline 2\nline 3'
226+
);
227+
await createTestCommitCore(getTestRepoPath('merged'), 'merged', 'Rename only-base.txt to only-base-renamed.txt');
228+
229+
await beforeDiflow();
230+
231+
const processor = new Processor(getTestRepoPath('config'), path.join(__dirname, 'workrepos'), 'master');
232+
await processor.process();
233+
234+
await afterDiflow();
235+
236+
await checkStateInConfig();
237+
238+
expect(await fs.exists(path.join(getTestRepoPath('merged'), 'only-base-renamed.txt'))).toBe(true);
239+
expect(await fs.exists(path.join(getTestRepoPath('base'), 'only-base-renamed.txt'))).toBe(true);
240+
241+
const newContent = await fs.readFile(path.join(getTestRepoPath('base'), 'only-base-renamed.txt'), 'utf8');
242+
243+
expect(newContent.replaceAll('\r\n', '\n')).toBe('only-base content\nline 1\nline 2\nline 3');
244+
245+
expect(await fs.exists(path.join(getTestRepoPath('merged'), 'only-base.txt'))).toBe(false);
246+
expect(await fs.exists(path.join(getTestRepoPath('base'), 'only-base.txt'))).toBe(false);
247+
});
214248
});

src/processor.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getDiffForCommit,
99
getLastCommitHash,
1010
removeRepoFile,
11+
renameRepoFile,
1112
repoFileExists,
1213
repoHasModifications,
1314
runGitCommand,
@@ -229,7 +230,11 @@ class CommitProcessor {
229230
if (file.action === 'M' || file.action === 'A') {
230231
await copyRepoFile(this.processor.repoPaths.base, this.processor.repoPaths.merged, file.file);
231232
} else if (file.action === 'D') {
232-
await removeRepoFile(this.processor.repoPaths.merged, file.file);
233+
if (!(await repoFileExists(this.processor.repoPaths.diff, file.file))) {
234+
await removeRepoFile(this.processor.repoPaths.merged, file.file);
235+
}
236+
} else if (file.action == 'R') {
237+
await renameRepoFile(this.processor.repoPaths.base, this.processor.repoPaths.merged, file.file, file.newFile!);
233238
}
234239
}
235240

@@ -242,6 +247,8 @@ class CommitProcessor {
242247
} else {
243248
await removeRepoFile(this.processor.repoPaths.merged, file.file);
244249
}
250+
} else if (file.action == 'R') {
251+
await renameRepoFile(this.processor.repoPaths.diff, this.processor.repoPaths.merged, file.file, file.newFile!);
245252
}
246253
}
247254

@@ -305,6 +312,12 @@ class CommitProcessor {
305312
} else if (file.action === 'D') {
306313
await removeRepoFile(this.processor.repoPaths.base, file.file);
307314
await removeRepoFile(this.processor.repoPaths.diff, file.file);
315+
} else if (file.action == 'R') {
316+
if (await repoFileExists(this.processor.repoPaths.diff, file.file)) {
317+
await renameRepoFile(this.processor.repoPaths.merged, this.processor.repoPaths.diff, file.file, file.newFile!);
318+
} else {
319+
await renameRepoFile(this.processor.repoPaths.merged, this.processor.repoPaths.base, file.file, file.newFile!);
320+
}
308321
}
309322
}
310323

src/testrepo.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export async function createTestCommit(
3030
console.log('Creating commit:', repoPath, 'file:', fileName, 'content:', content);
3131
await fs.ensureDir(path.join(repoPath, path.dirname(fileName)));
3232
await fs.writeFile(path.join(repoPath, fileName), content);
33+
return await createTestCommitCore(repoPath, repoid, message);
34+
}
35+
36+
export async function createTestCommitCore(repoPath: string, repoid: string, message?: string) {
3337
await execAsync('git add .', { cwd: repoPath });
3438
if (message) {
3539
await execAsync(`git commit -m "${message}"`, { cwd: repoPath });
@@ -58,6 +62,9 @@ export async function initTestRepos() {
5862
await initTestRepo('merged');
5963

6064
// Setup initial files
65+
await createTestCommit(getTestRepoPath('base'), 'only-base.txt', 'only-base content\nline 1\nline 2', 'base');
66+
await createTestCommit(getTestRepoPath('merged'), 'only-base.txt', 'only-base content\nline 1\nline 2', 'merged');
67+
6168
const baseHash = await createTestCommit(getTestRepoPath('base'), 'file1.txt', 'base content', 'base');
6269
const diffHash = await createTestCommit(getTestRepoPath('diff'), 'file1.txt', 'different content', 'diff');
6370
const mergedHash = await createTestCommit(getTestRepoPath('merged'), 'file1.txt', 'different content', 'merged');

src/tools.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export async function runGitCommand(repoPath: string, cmd: string): Promise<stri
1919
}
2020

2121
export async function getCommits(repoPath: string, branch: string): Promise<Commit[]> {
22-
const log = await runGitCommand(repoPath, `log ${branch} --reverse --pretty=format:"%H@|@%ct@|@%aN@|@%aE@|@%s@|@%ad"`);
22+
const log = await runGitCommand(
23+
repoPath,
24+
`log ${branch} --reverse --pretty=format:"%H@|@%ct@|@%aN@|@%aE@|@%s@|@%ad"`
25+
);
2326
const res = log
2427
.split('\n')
2528
.filter(Boolean)
@@ -64,12 +67,13 @@ export async function getDiffForCommit(repoPath: string, commitHash: string): Pr
6467
const diff = await runGitCommand(repoPath, `show --name-status ${commitHash}`);
6568
return diff
6669
.split('\n')
67-
.filter(x => x.match(/^[AMD]\t/))
70+
.filter(x => x.match(/^(A|M|D|R\d\d\d)\t/))
6871
.map(x => {
69-
const [action, file] = x.split('\t');
72+
const [action, file, newFile] = x.split('\t');
7073
return {
71-
action: action as FileAction,
74+
action: action.substring(0, 1) as FileAction,
7275
file,
76+
newFile,
7377
};
7478
});
7579
}
@@ -82,6 +86,19 @@ export async function copyRepoFile(srcRepo: string, destRepo: string, file: stri
8286
console.log(`Copied ${file} from ${srcRepo} to ${destRepo}`);
8387
}
8488

89+
export async function renameRepoFile(srcRepo: string, destRepo: string, srcFile: string, dstFile: string) {
90+
const oldTargetFile = path.join(destRepo, srcFile);
91+
if (await fs.exists(oldTargetFile)) {
92+
await fs.unlink(oldTargetFile);
93+
}
94+
const newSourceFile = path.join(srcRepo, dstFile);
95+
const newTargetFile = path.join(destRepo, dstFile);
96+
97+
await fs.ensureDir(path.dirname(newTargetFile));
98+
await fs.copyFile(newSourceFile, newTargetFile);
99+
console.log(`Renamed ${srcFile} to ${dstFile} from ${srcRepo} to ${destRepo}`);
100+
}
101+
85102
export async function removeRepoFile(repoPath: string, file: string) {
86103
const filePath = path.join(repoPath, file);
87104
if (await fs.exists(filePath)) {

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export type RepoId = 'base' | 'diff' | 'merged' | 'config';
2-
export type FileAction = 'A' | 'D' | 'M';
2+
export type FileAction = 'A' | 'D' | 'M' | 'R';
33

44
export interface RepoConfig {
55
url: string;
6-
commitTag?: string; // eg. [skip ci] for skipping github pipeline
6+
commitTag?: string; // eg. [skip ci] for skipping github pipeline
77
}
88

99
export interface RepoIdentifier {
@@ -45,6 +45,7 @@ export interface Commit {
4545
export interface ChangeItem {
4646
action: FileAction;
4747
file: string;
48+
newFile?: string;
4849
}
4950

5051
// export interface BranchInfo {

0 commit comments

Comments
 (0)