Skip to content

Commit 001b9c1

Browse files
committed
fix: fetches latest changes before checking sync state
1 parent 6db3126 commit 001b9c1

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-sync-js",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "JS implementation for Git-Sync, a handy script that backup your notes in a git repo to the remote git services.",
55
"homepage": "https://github.com/linonetwo/git-sync-js",
66
"bugs": {

src/forcePull.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export async function forcePull(options: IForcePullOptions) {
7474
await credentialOn(dir, remoteUrl, gitUserName, accessToken, remoteName);
7575
try {
7676
logProgress(GitStep.StartFetchingFromGithubRemote);
77-
await fetchRemote(dir, defaultGitInfo.remote, defaultGitInfo.branch, logger);
77+
logDebug(`Fetching from remote ${remoteName} branch ${defaultBranchName}`, GitStep.StartFetchingFromGithubRemote);
78+
await fetchRemote(dir, remoteName, defaultBranchName, logger);
7879
const syncState = await getSyncState(dir, defaultBranchName, remoteName, logger);
7980
logDebug(`syncState in dir ${dir} is ${syncState}`, GitStep.StartFetchingFromGithubRemote);
8081
if (syncState === 'equal') {
@@ -83,7 +84,7 @@ export async function forcePull(options: IForcePullOptions) {
8384
return;
8485
}
8586
logProgress(GitStep.StartResettingLocalToRemote);
86-
await hardResetLocalToRemote(dir, branch, remoteName);
87+
await hardResetLocalToRemote(dir, defaultBranchName, remoteName);
8788
logProgress(GitStep.FinishForcePull);
8889
} catch (error) {
8990
if (error instanceof CantForcePullError) {

test/forcePull.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,23 @@ describe('forcePull', () => {
3131
await forcePull(getForcePullOptions());
3232
expect(await getSyncState(dir, defaultGitInfo.branch, defaultGitInfo.remote)).toBe<SyncState>('equal');
3333
});
34+
35+
test('fetches latest changes before checking sync state', async () => {
36+
// This test verifies that forcePull fetches the latest remote state
37+
// before checking if local is equal to remote
38+
39+
// First fetch to know the remote state
40+
await fetchRemote(dir, defaultGitInfo.remote, defaultGitInfo.branch);
41+
42+
// Check initial state (could be behind or diverged depending on local commits)
43+
const initialSyncState = await getSyncState(dir, defaultGitInfo.branch, defaultGitInfo.remote);
44+
expect(['behind', 'diverged']).toContain(initialSyncState);
45+
46+
// forcePull should fetch the latest and then reset
47+
await forcePull(getForcePullOptions());
48+
49+
// After forcePull, should be equal to remote
50+
const finalSyncState = await getSyncState(dir, defaultGitInfo.branch, defaultGitInfo.remote);
51+
expect(finalSyncState).toBe<SyncState>('equal');
52+
});
3453
});

0 commit comments

Comments
 (0)