Skip to content

Commit

Permalink
Added method to retrieve changed files for each commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AvrAlexandra committed Oct 16, 2024
1 parent b3adb0b commit 5fbfdcb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions __tests__/history.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { analyseHistory } from '../src/commands/history/history';

import {analyseHistory} from '../src/commands/history/history'
jest.setTimeout(30000);
describe('test history analysis for default plugins', () => {

it('test history analysis for javascript and ruby', async () => {
Expand Down
46 changes: 42 additions & 4 deletions src/commands/history/history.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Command } from 'commander'
import git from 'isomorphic-git'

Check failure on line 2 in src/commands/history/history.ts

View workflow job for this annotation

GitHub Actions / Build And Test

Cannot find module 'isomorphic-git' or its corresponding type declarations.
import fs from 'fs'
import { AnalyseOptions } from '../analyse'
import { getPluginsFromNames } from '../../plugins'
import { DepinderProject } from "../../extension-points/extract";
import { Plugin } from "../../extension-points/plugin";
import {AnalyseOptions} from '../analyse'
import {getPluginsFromNames} from '../../plugins'
import {DepinderProject} from "../../extension-points/extract"
import {Plugin} from "../../extension-points/plugin"

export const historyCommand = new Command()
.name('history')
Expand Down Expand Up @@ -37,6 +37,44 @@ export async function analyseHistory(folders: string[], options: AnalyseOptions,
// Function to process each commit and update the map by plugin
async function processCommitForPlugins(commit: any, folder: string, selectedPlugins: Plugin[], projectMap: Map<string, { commit: string, projects: DepinderProject[] }[]>) {
console.log('Commit:', JSON.stringify(commit, null, 2));
const changes = await getChangedFiles(commit, folder);
console.log("Changes: ", changes);
}

async function getChangedFiles(commit: any, folder: string): Promise<string[]> {
const changedFiles: string[] = [];

const currentCommitOid = commit.oid;
const parentCommitOid = commit.commit.parent[0];

try {
const diff = await git.walk({
fs,
dir: folder,
trees: [git.TREE({ ref: currentCommitOid }), git.TREE({ ref: parentCommitOid })],
map: async function (filepath, [head, base]) {

Check failure on line 55 in src/commands/history/history.ts

View workflow job for this annotation

GitHub Actions / Build And Test

Parameter 'filepath' implicitly has an 'any' type.

Check failure on line 55 in src/commands/history/history.ts

View workflow job for this annotation

GitHub Actions / Build And Test

Binding element 'head' implicitly has an 'any' type.

Check failure on line 55 in src/commands/history/history.ts

View workflow job for this annotation

GitHub Actions / Build And Test

Binding element 'base' implicitly has an 'any' type.
if (!head && base) {
// File was deleted
changedFiles.push(filepath);
} else if (head && !base) {
// File was added
changedFiles.push(filepath);
} else if (head && base) {
// File was modified
const headOid = await head.oid();
const baseOid = await base.oid();
if (headOid !== baseOid) {
changedFiles.push(filepath);
}
}
}
});

} catch (error) {
console.error(`Error getting changed files for commit ${currentCommitOid}:`, error);
}

return changedFiles;
}

// Function to fetch commits from a Git repository
Expand Down

0 comments on commit 5fbfdcb

Please sign in to comment.