Skip to content

Commit 9f6d29c

Browse files
Skip sync when already up to date
1 parent 00e83f0 commit 9f6d29c

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/daemon.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,13 @@ async function handleManualSync(repoId, configDir, res) {
286286

287287
try {
288288
const synced = gitSync(repo.path, null, repo.cloneUrl)
289-
if (synced) {
289+
if (synced === 'skipped') {
290+
if (status.repos[repoId]) {
291+
status.repos[repoId].status = 'synced'
292+
updateStatus(configDir)
293+
}
294+
res.end(JSON.stringify({ ok: true, skipped: true }))
295+
} else if (synced) {
290296
if (status.repos[repoId]) {
291297
status.repos[repoId].lastSync = new Date().toISOString()
292298
status.repos[repoId].syncCount++
@@ -457,13 +463,15 @@ async function handleEvent(event, config, configDir) {
457463

458464
// Handle 30617 (repo announcement) - simpler, just trigger sync
459465
if (kind === 30617) {
460-
console.log('[event] Repository announcement, triggering sync')
461-
462466
// Extract clone URL from event if present
463467
const cloneTag = event.tags.find(t => t[0] === 'clone')
464468
const cloneUrl = cloneTag ? cloneTag[1] : repo.cloneUrl
465469

466470
const synced = gitSync(repo.path, null, cloneUrl)
471+
if (synced === 'skipped') {
472+
// Already up to date, don't update stats
473+
return
474+
}
467475
if (synced) {
468476
if (status.repos[repoId]) {
469477
status.repos[repoId].lastSync = new Date().toISOString()

src/git.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,43 @@ export function gitSync(repoPath, commit, cloneUrl) {
2626
}
2727
}
2828

29-
// If no commit specified (30617 event), just pull latest
29+
// If no commit specified (30617 event), check if there are changes first
3030
if (!commit) {
31-
console.log(`[git] Pulling latest for ${repoPath}`)
3231
try {
32+
// Fetch to see if there are remote changes
33+
execSync(`git fetch`, {
34+
cwd: repoPath,
35+
stdio: 'pipe'
36+
})
37+
38+
// Check if local is behind remote
39+
const local = execSync(`git rev-parse HEAD`, { cwd: repoPath, stdio: 'pipe' }).toString().trim()
40+
const remote = execSync(`git rev-parse @{u}`, { cwd: repoPath, stdio: 'pipe' }).toString().trim()
41+
42+
if (local === remote) {
43+
console.log(`[git] Already up to date: ${repoPath.split('/').pop()}`)
44+
return 'skipped'
45+
}
46+
47+
console.log(`[git] Pulling latest for ${repoPath}`)
3348
execSync(`git pull`, {
3449
cwd: repoPath,
3550
stdio: 'pipe'
3651
})
3752
console.log(`[git] ✓ Pulled latest`)
3853
return true
3954
} catch (err) {
55+
// If no upstream tracking, try pull anyway
56+
if (err.message.includes('@{u}') || err.message.includes('upstream')) {
57+
try {
58+
execSync(`git pull`, { cwd: repoPath, stdio: 'pipe' })
59+
console.log(`[git] ✓ Pulled latest`)
60+
return true
61+
} catch (pullErr) {
62+
console.error(`[git] ✗ Pull failed:`, pullErr.message)
63+
return false
64+
}
65+
}
4066
console.error(`[git] ✗ Pull failed:`, err.message)
4167
return false
4268
}

0 commit comments

Comments
 (0)