-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
109 lines (94 loc) · 3.86 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { ipcRenderer } = require('electron');
const { exec } = require('child_process');
function openDialog() {
ipcRenderer.send('open-dialog');
}
ipcRenderer.on('selected-directory', (event, path) => {
document.getElementById('repoPath').value = path;
});
function execGitCommand(command, repoPath) {
return new Promise((resolve, reject) => {
exec(command, { cwd: repoPath }, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command '${command}': ${error}`);
return reject(stderr);
}
resolve(stdout.trim());
});
});
}
async function findBranchesForFile(filePath, repoPath) {
try {
const commitCommand = `git log --pretty=format:'%H' -- "${filePath}"`;
const commitHashes = await execGitCommand(commitCommand, repoPath);
if (!commitHashes) {
console.log(`No commits found for file ${filePath}`);
return [];
}
const branchesSet = new Set();
for (let commitHash of commitHashes.split('\n')) {
const branchesCommand = `git branch --contains ${commitHash}`;
const branches = await execGitCommand(branchesCommand, repoPath);
branches.split('\n').map(branch => branchesSet.add(branch.trim()));
}
return Array.from(branchesSet);
} catch (error) {
console.error(`Error finding branches for file '${filePath}': ${error}`);
return [];
}
}
async function displayLfsFilesWithBranchPresence(repoPath) {
const filesContainer = document.getElementById('lfs-files');
filesContainer.innerHTML = '';
try {
const lfsFilesOutput = await execGitCommand('git lfs ls-files -a -s', repoPath);
const files = lfsFilesOutput.split('\n');
for (let file of files) {
if (!file) continue;
const [oid, size, filePath] = file.split(/\s+/);
const branches = await findBranchesForFile(filePath, repoPath);
const fileItem = document.createElement('li');
fileItem.textContent = `${filePath} (Size: ${formatBytes(parseInt(size, 10))}) - Branches: ${branches.join(', ') || 'No branch information'}`;
filesContainer.appendChild(fileItem);
}
} catch (error) {
console.error("Error displaying LFS files:", error);
}
}
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
function onShowLfsFilesClick() {
const repoPath = document.getElementById('repoPath').value;
if (!repoPath) {
alert('Please enter a valid repository path.');
return;
}
displayLfsFilesWithBranchPresence(repoPath);
}
// Modify the pruneLfsObjects function signature to accept only the isDryRun parameter
async function pruneLfsObjects(isDryRun) {
const repoPath = document.getElementById('repoPath').value; // Dynamically get the repoPath
if (!repoPath) {
alert('Please enter a valid repository path.');
return;
}
try {
let pruneCommand = 'git lfs prune --verify-remote';
if (isDryRun) {
pruneCommand += ' --dry-run';
}
const result = await execGitCommand(pruneCommand, repoPath);
console.log(result); // Outputs the result of the prune operation
// Display feedback based on the dry run or actual execution
alert(`Cleanup process ${(isDryRun ? 'simulation' : 'execution')} completed. Check console for details.`);
} catch (error) {
console.error("Error pruning LFS objects:", error);
alert("Error during cleanup process. Check console for details.");
}
}