cloc-graph
Version:
Track lines of code over time by language with visualization
66 lines (65 loc) • 2.71 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeGitRepo = initializeGitRepo;
exports.getCommits = getCommits;
/**
* Git repository service
*/
const simple_git_1 = require("simple-git");
const sampling_1 = require("../utils/sampling");
/**
* Initialize Git repository and verify it exists
*
* @param repoPath - Path to the Git repository
* @returns SimpleGit instance
* @throws Error if the path is not a Git repository
*/
function initializeGitRepo(repoPath) {
return __awaiter(this, void 0, void 0, function* () {
const git = (0, simple_git_1.simpleGit)();
try {
const isRepo = yield git.checkIsRepo();
if (!isRepo) {
throw new Error(`${repoPath} is not a git repository`);
}
return git;
}
catch (e) {
throw new Error(`Error checking repository: ${e}`);
}
});
}
/**
* Get commits from a Git repository with optional sampling
*
* @param git - SimpleGit instance
* @param options - Sampling options (step, max samples, smart sampling)
* @returns Array of commits
*/
function getCommits(git, options) {
return __awaiter(this, void 0, void 0, function* () {
// Get all commits in chronological order (oldest first)
const commits = yield git.log(['--reverse']);
// Create a properly typed copy of the commit objects
const allCommits = [...commits.all].map(commit => ({
hash: commit.hash,
date: commit.date,
// Include any other properties you need
}));
// Apply smart sampling if enabled and needed
if (options.smartSampling && allCommits.length > options.maxSamples) {
console.log(`Repository has ${allCommits.length} commits. Using smart sampling to select ${options.maxSamples} representative commits...`);
return (0, sampling_1.smartSampleCommits)(allCommits, options.maxSamples);
}
return allCommits;
});
}