From 5fbfdcb1db1acaaeb750f4b734f4ffaf5481383a Mon Sep 17 00:00:00 2001 From: AvrAlexandra Date: Wed, 16 Oct 2024 18:27:45 +0300 Subject: [PATCH] Added method to retrieve changed files for each commit --- __tests__/history.integration.test.ts | 4 +-- src/commands/history/history.ts | 46 ++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/__tests__/history.integration.test.ts b/__tests__/history.integration.test.ts index 8af0a7f..f9b2228 100644 --- a/__tests__/history.integration.test.ts +++ b/__tests__/history.integration.test.ts @@ -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 () => { diff --git a/src/commands/history/history.ts b/src/commands/history/history.ts index a363ad3..1111f97 100644 --- a/src/commands/history/history.ts +++ b/src/commands/history/history.ts @@ -1,10 +1,10 @@ import { Command } from 'commander' import git from 'isomorphic-git' 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') @@ -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) { 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 { + 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]) { + 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