-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-fix.js
More file actions
104 lines (85 loc) · 3.34 KB
/
git-fix.js
File metadata and controls
104 lines (85 loc) · 3.34 KB
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
import { exec } from 'child_process';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import util from 'util';
const execPromise = util.promisify(exec);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Main function to run the git fix operations
async function fixGitPushIssues() {
try {
console.log('Starting Git fix process...');
// Step 1: Check for and remove lock files
console.log('\nStep 1: Checking for lock files...');
const gitDir = path.join(process.cwd(), '.git');
const lockFiles = await findLockFiles(gitDir);
if (lockFiles.length > 0) {
console.log(`Found ${lockFiles.length} lock files. Removing them...`);
for (const lockFile of lockFiles) {
fs.unlinkSync(lockFile);
console.log(`Removed: ${lockFile}`);
}
} else {
console.log('No lock files found.');
}
// Step 2: Pull changes
console.log('\nStep 2: Pulling remote changes...');
try {
const { stdout: pullOutput } = await execPromise('git pull --no-rebase');
console.log(pullOutput);
} catch (pullError) {
console.log('Pull encountered conflicts. This is normal if there are merge conflicts.');
console.log(pullError.stdout || pullError.message);
}
// Step 3: Check for merge conflicts
console.log('\nStep 3: Checking for merge conflicts...');
const { stdout: statusOutput } = await execPromise('git status');
if (statusOutput.includes('Unmerged paths') || statusOutput.includes('fix conflicts')) {
console.log('Merge conflicts detected. Please resolve them manually in the files listed below:');
const conflictedFiles = statusOutput
.split('\n')
.filter(line => line.includes('both modified:'))
.map(line => line.trim().replace('both modified:', '').trim());
console.log('\nConflicted files:');
conflictedFiles.forEach(file => console.log(`- ${file}`));
console.log('\nAfter resolving conflicts, run these commands:');
console.log('1. git add <resolved-file-paths>');
console.log('2. git commit -m "Resolve merge conflicts"');
console.log('3. git push');
} else {
console.log('No merge conflicts detected.');
// Step 4: Try to push changes
console.log('\nStep 4: Pushing changes...');
try {
const { stdout: pushOutput } = await execPromise('git push');
console.log('Push successful!');
console.log(pushOutput);
} catch (pushError) {
console.log('Push failed. You may need additional steps:');
console.log(pushError.stdout || pushError.message);
}
}
} catch (error) {
console.error('Error during Git fix process:', error.message);
}
}
// Helper function to find lock files
async function findLockFiles(dir) {
const lockFiles = [];
function scanDirectory(currentDir) {
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
scanDirectory(fullPath);
} else if (entry.name.endsWith('.lock')) {
lockFiles.push(fullPath);
}
}
}
scanDirectory(dir);
return lockFiles;
}
// Run the fix process
fixGitPushIssues();