-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
123 lines (108 loc) · 3.28 KB
/
Copy pathgit.js
File metadata and controls
123 lines (108 loc) · 3.28 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { execSync, exec } from 'child_process'
import { existsSync, mkdirSync } from 'fs'
import { dirname } from 'path'
export function gitSync(repoPath, commit, cloneUrl) {
// Auto-clone if repo doesn't exist but we have a cloneUrl
if (!existsSync(repoPath)) {
if (!cloneUrl) {
console.error(`[git] ✗ Repo doesn't exist and no cloneUrl: ${repoPath}`)
return false
}
console.log(`[git] Auto-cloning ${cloneUrl} to ${repoPath}`)
try {
// Create parent directory if needed
mkdirSync(dirname(repoPath), { recursive: true })
execSync(`git clone ${cloneUrl} ${repoPath}`, {
stdio: 'pipe'
})
console.log(`[git] ✓ Cloned successfully`)
return true
} catch (err) {
console.error(`[git] ✗ Clone failed:`, err.message)
return false
}
}
// If no commit specified (30617 event), check if there are changes first
if (!commit) {
try {
// Fetch to see if there are remote changes
execSync(`git fetch`, {
cwd: repoPath,
stdio: 'pipe'
})
// Check if local is behind remote
const local = execSync(`git rev-parse HEAD`, { cwd: repoPath, stdio: 'pipe' }).toString().trim()
const remote = execSync(`git rev-parse @{u}`, { cwd: repoPath, stdio: 'pipe' }).toString().trim()
if (local === remote) {
console.log(`[git] Already up to date: ${repoPath.split('/').pop()}`)
return 'skipped'
}
console.log(`[git] Pulling latest for ${repoPath}`)
execSync(`git pull`, {
cwd: repoPath,
stdio: 'pipe'
})
console.log(`[git] ✓ Pulled latest`)
return true
} catch (err) {
// If no upstream tracking, try pull anyway
if (err.message.includes('@{u}') || err.message.includes('upstream')) {
try {
execSync(`git pull`, { cwd: repoPath, stdio: 'pipe' })
console.log(`[git] ✓ Pulled latest`)
return true
} catch (pullErr) {
console.error(`[git] ✗ Pull failed:`, pullErr.message)
return false
}
}
console.error(`[git] ✗ Pull failed:`, err.message)
return false
}
}
// Specific commit (30618 event)
console.log(`[git] Syncing ${repoPath} to ${commit.slice(0, 7)}`)
try {
// Fetch latest
execSync(`git fetch --all`, {
cwd: repoPath,
stdio: 'pipe'
})
// Checkout specific commit
execSync(`git checkout ${commit}`, {
cwd: repoPath,
stdio: 'pipe'
})
console.log(`[git] ✓ Synced to ${commit.slice(0, 7)}`)
return true
} catch (err) {
console.error(`[git] ✗ Failed:`, err.message)
return false
}
}
export function getCurrentCommit(repoPath) {
try {
const result = execSync('git rev-parse HEAD', {
cwd: repoPath,
stdio: 'pipe'
})
return result.toString().trim()
} catch {
return null
}
}
export function runPostSync(repoPath, command) {
if (!command) return true
console.log(`[post-sync] Running: ${command}`)
return new Promise((resolve) => {
exec(command, { cwd: repoPath }, (err, stdout, stderr) => {
if (err) {
console.error(`[post-sync] ✗ Failed:`, err.message)
resolve(false)
} else {
console.log(`[post-sync] ✓ Complete`)
resolve(true)
}
})
})
}