Skip to content

Commit 791e2ab

Browse files
author
tung-hpzbook
committed
optimize git sync avoid empty commits
1 parent cfdec4f commit 791e2ab

4 files changed

Lines changed: 40 additions & 31 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<p align="center"><img src="https://feathernote.deno.dev/icons/ios/128.png" alt="FeatherNote Logo" width="128"></p>
1+
<a href="/"><p align="center"><img src="https://feathernote.deno.dev/icons/ios/128.png" alt="FeatherNote Logo" width="128"></p></a>
22

33
# FeatherNote: featherweight, offline-first PWA Note with Markdown, S3 Sync, and Share Target API.
44

src/git.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -307,31 +307,39 @@ window.deleteNoteFromGit = async (noteIdOrPath, creds) => {
307307
} catch (e) {
308308
return { ids: [], updatedAt: '1970-01-01T00:00:00.000Z' };
309309
}
310-
}; window.finishGitSync = async (creds) => { if (!creds.repoUrl) return;
310+
}; window.finishGitSync = async (creds) => {
311+
if (!creds.repoUrl) return;
311312

312-
try {
313-
const config = getGitConfig(creds);
314-
const remoteRef = creds.branch || 'main';
315-
316-
// 1. Commit
317313
try {
318-
const sha = await git.commit({
319-
...config,
320-
message: `Sync from FeatherNote: ${new Date().toISOString()}`,
321-
});
322-
console.log('GIT: Committed:', sha);
323-
} catch (e) {
324-
if (e.message && (e.message.includes('nothing to commit') || e.message.includes('no changes'))) {
325-
console.log('GIT: Nothing to commit.');
326-
return; // Nothing to push either
327-
}
328-
throw e; // Rethrow other errors
329-
}
330-
331-
// 2. Push with Fallbacks
332-
const pushOptions = { ...config, url: creds.repoUrl, ref: remoteRef };
314+
const config = getGitConfig(creds);
315+
const remoteRef = creds.branch || 'main';
333316

334-
try {
317+
// 0. Check for staged changes to avoid empty commits
318+
const matrix = await git.statusMatrix({ fs, dir: GIT_DIR });
319+
const hasStagedChanges = matrix.some(row => row[1] !== row[3]);
320+
321+
// 1. Commit if changes exist
322+
if (hasStagedChanges) {
323+
try {
324+
const sha = await git.commit({
325+
...config,
326+
message: `Sync from FeatherNote: ${new Date().toISOString()}`,
327+
});
328+
console.log('GIT: Committed:', sha);
329+
} catch (e) {
330+
if (e.message && (e.message.includes('nothing to commit') || e.message.includes('no changes'))) {
331+
console.log('GIT: Nothing to commit.');
332+
} else {
333+
throw e;
334+
}
335+
}
336+
} else {
337+
console.log('GIT: Nothing to commit (staged index matches HEAD).');
338+
}
339+
340+
// 2. Push with Fallbacks (always try, to push any existing local commits or merges)
341+
const pushOptions = { ...config, url: creds.repoUrl, ref: remoteRef };
342+
try {
335343
// Attempt 1: Standard Push
336344
console.log('GIT: Pushing...');
337345
await git.push(pushOptions);

src/helpers.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ async function syncDeletedNoteIds({deletedNoteIds, credentials, gitCredentials,
420420
console.log('Deleted notes list is already in sync. No upload needed.');
421421
}
422422

423-
return finalIdArray;
423+
return { finalIdArray, hasChanged };
424424
}
425425

426426
async function synchronize({notes, deletedNoteIds, isSilent, credentials, nostrPrivateKey, nostrRelays, gdriveStore, lastSync, gitCredentials}) {
@@ -438,7 +438,7 @@ async function synchronize({notes, deletedNoteIds, isSilent, credentials, nostrP
438438
};
439439

440440
// --- Step 0: Sync Deleted IDs ---
441-
const effectiveDeletedNoteIds = await syncDeletedNoteIds({deletedNoteIds, credentials, gitCredentials, gdriveStore});
441+
const { finalIdArray: effectiveDeletedNoteIds, hasChanged: deletedListChanged } = await syncDeletedNoteIds({deletedNoteIds, credentials, gitCredentials, gdriveStore});
442442

443443
// --- Step 1: Get remote state FIRST ---
444444
const { mergedNotes: remoteNoteMetadata, s3Ids, gitIds, nostrIds, gdriveIds, gdriveMap } = await listNotes({credentials, nostrPrivateKey, nostrRelays, lastSync, gitCredentials, gdriveStore});
@@ -509,9 +509,10 @@ async function synchronize({notes, deletedNoteIds, isSilent, credentials, nostrP
509509
});
510510
const successfulDeletedCount = successfulDeletedIds.length;
511511

512-
// Commit and Push Git if we made changes (Uploads or Deletes)
513-
if (gitCredentials?.repoUrl && (successfulUploadedCount > 0 || successfulDeletedCount > 0)) {
512+
// Finish Git Sync (Commit and Push) if configured
513+
if (gitCredentials?.repoUrl) {
514514
if (typeof window.finishGitSync === 'function') {
515+
// This will check for staged changes, commit if necessary, and push
515516
await window.finishGitSync(gitCredentials);
516517
}
517518
}

src/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
<path fill-rule="evenodd" stroke="#A2D2FF" stroke-width="48.35px" stroke-linecap="butt" stroke-linejoin="miter" fill="none" d="M351.175,24.175 L763.175,24.175 C943.772,24.175 1090.175,170.578 1090.175,351.175 L1090.175,763.175 C1090.175,943.772 943.772,1090.175 763.175,1090.175 L351.175,1090.175 C170.578,1090.175 24.175,943.772 24.175,763.175 L24.175,351.175 C24.175,170.578 170.578,24.175 351.175,24.175 Z"/>
4747
</svg>
4848

49-
<span class="font-bold text-lg font-sans hidden md:inline-block" style="color:#A2D2FF;"
49+
<div class="font-bold text-lg font-sans hidden md:inline-block" style="color:#A2D2FF;"
5050
x-show="!syncStatusMessage && !isSyncing">
51-
FeatherNote
52-
</span>
51+
<span>FeatherNote</span><span class="text-xs font-semibold" x-text="' #' + appVersion"></span>
52+
</div>
5353
</a>
5454
<div class="flex items-center gap-4">
5555
<!-- SyncStatus -->
@@ -226,7 +226,7 @@ <h3 class="text-lg font-bold mb-4">Confirm Delete</h3>
226226

227227
<!-- Settings Dialog -->
228228
<div x-show="settingsDialogIsOpen"
229-
class="fixed inset-0 pt-12 md:pt-8 z-10 w-full h-full flex items-center justify-center"
229+
class="fixed inset-0 pt-12 z-10 w-full h-full flex items-center justify-center"
230230
style="display: none; background-color: rgba(0,0,0,0.5);"
231231
>
232232
<div class="bg-white p-6 border w-full max-w-4xl max-h-full overflow-y-auto"

0 commit comments

Comments
 (0)