gctm
Version:
š Git Commit Time Machine - Comprehensive Git history manipulation tool with AI-powered commit message generation. Change commit dates, edit content, manage sensitive data, and generate creative commit messages using OpenAI, Anthropic, Google Gemini, and
160 lines (135 loc) ⢠4.92 kB
JavaScript
/**
* Git Commit Time Machine - Basic Usage Example
*/
const GitCommitTimeMachine = require('../src/index');
const fs = require('fs-extra');
const path = require('path');
async function basicUsageExample() {
console.log('š Git Commit Time Machine - Basic Usage Example\n');
// Create Git Commit Time Machine instance
const gctm = new GitCommitTimeMachine({
repoPath: process.cwd() // Use repository in current directory
});
try {
// 1. Create backup
console.log('1ļøā£ Creating backup...');
const backupResult = await gctm.backupManager.createBackup({
description: 'Backup before basic usage example',
includeUncommitted: true
});
if (backupResult.success) {
console.log(`ā
Backup created: ${backupResult.backupId}`);
} else {
console.error('ā Failed to create backup:', backupResult.error);
return;
}
// 2. Get last 5 commits
console.log('\n2ļøā£ Getting last 5 commits...');
const commits = await gctm.gitProcessor.getCommits({ limit: 5 });
if (commits.length === 0) {
console.log('ā No commits found to process');
return;
}
console.log(`ā
Found ${commits.length} commits`);
commits.forEach((commit, index) => {
console.log(` ${index + 1}. ${commit.shortHash} - ${commit.message} (${commit.date})`);
});
// 3. Redate commits (to past dates)
console.log('\n3ļøā£ Redating commit timestamps...');
const redateResult = await gctm.redateCommits({
startDate: '2023-01-01',
endDate: '2023-01-05',
preserveOrder: true,
createBackup: false // Already created backup
});
if (redateResult.success) {
console.log(`ā
${redateResult.processed} commits successfully redated`);
} else {
console.error('ā Redating failed:', redateResult.error);
}
// 4. Create example file with sensitive data
console.log('\n4ļøā£ Creating example file...');
const exampleFile = 'example-config.js';
const exampleContent = `
// Example Configuration File
const config = {
database: {
host: "localhost",
port: 5432,
user: "admin",
password: "supersecretpassword123",
apiKey: "sk-1234567890abcdef"
},
api: {
endpoint: "https://api.example.com",
key: "ak_test_abcdef123456",
secret: "secret_key_xyz789"
},
email: {
from: "admin@example.com",
smtp: {
host: "smtp.example.com",
user: "noreply@example.com",
pass: "emailpassword456"
}
}
};
module.exports = config;
`;
await fs.writeFile(exampleFile, exampleContent, 'utf8');
console.log(`ā
${exampleFile} file created`);
// 5. Detect sensitive data
console.log('\n5ļøā£ Detecting sensitive data...');
const sensitiveData = gctm.contentEditor.detectSensitiveData(exampleContent);
console.log('Detected sensitive data:');
Object.entries(sensitiveData).forEach(([type, items]) => {
console.log(` ${type}: ${items.length} items`);
items.forEach(item => {
console.log(` - ${item.substring(0, 50)}${item.length > 50 ? '...' : ''}`);
});
});
// 6. Sanitize sensitive data
console.log('\n6ļøā£ Sanitizing sensitive data...');
const sanitizeResult = await gctm.contentEditor.sanitizeFile(exampleFile, {
hideEmails: true,
hideApiKeys: true,
emailReplacement: '***EMAIL***',
apiKeyReplacement: '***API_KEY***'
});
if (sanitizeResult.success && sanitizeResult.changes) {
console.log(`ā
File sanitized: ${sanitizeResult.sanitizedTypes.join(', ')}`);
// Show sanitized content
const cleanedContent = await fs.readFile(exampleFile, 'utf8');
console.log('\nSanitized file content (first 10 lines):');
const lines = cleanedContent.split('\n').slice(0, 10);
lines.forEach((line, index) => {
console.log(` ${index + 1}: ${line}`);
});
}
// 7. List backups
console.log('\n7ļøā£ Listing available backups...');
const backups = await gctm.listBackups();
if (backups.length > 0) {
console.log(`ā
Found ${backups.length} backups:`);
backups.forEach((backup, index) => {
const date = new Date(backup.createdAt).toLocaleDateString();
console.log(` ${index + 1}. ${backup.id} - ${backup.description || 'No description'} (${date})`);
});
} else {
console.log('ā¹ļø No available backups found');
}
// 8. Cleanup
console.log('\n8ļøā£ Performing cleanup...');
await fs.remove(exampleFile);
console.log(`ā
${exampleFile} file deleted`);
console.log('\nš Example completed successfully!');
} catch (error) {
console.error('\nā Error occurred while running example:', error.message);
console.error(error.stack);
}
}
// Run the example
if (require.main === module) {
basicUsageExample();
}
module.exports = basicUsageExample;