Skip to content

Commit 39c0b6a

Browse files
committed
feat: check git state equal will skip force pull
1 parent a89da27 commit 39c0b6a

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

src/commitAndSync.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,15 @@ export async function commitAndSync(options: ICommitAndSyncOptions): Promise<voi
7272
});
7373

7474
// preflight check
75-
const repoStartingState = await getGitRepositoryState(dir, logger);
76-
if (repoStartingState.length === 0 || repoStartingState === '|DIRTY') {
77-
logProgress(GitStep.PrepareSync);
78-
logDebug(`${dir} repoStartingState: ${repoStartingState}, ${gitUserName} <${email ?? defaultGitInfo.email}>`, GitStep.PrepareSync);
79-
} else if (repoStartingState === 'NOGIT') {
80-
throw new CantSyncGitNotInitializedError(dir);
81-
} else {
82-
// we may be in middle of a rebase, try fix that
83-
await continueRebase(dir, gitUserName, email ?? defaultGitInfo.email, logger, repoStartingState);
84-
}
75+
await syncPreflightCheck({
76+
dir,
77+
logger,
78+
logProgress,
79+
logDebug,
80+
defaultGitInfo,
81+
userInfo,
82+
});
83+
8584
if (await haveLocalChanges(dir)) {
8685
logProgress(GitStep.HaveThingsToCommit);
8786
logDebug(commitMessage, GitStep.HaveThingsToCommit);
@@ -189,3 +188,32 @@ export async function commitAndSync(options: ICommitAndSyncOptions): Promise<voi
189188
await credentialOff(dir, remoteUrl);
190189
}
191190
}
191+
192+
/**
193+
* Check for git repo state, if it is not clean, try fix it. If not init will throw error.
194+
* This method is used by commitAndSync and forcePull before they doing anything.
195+
*/
196+
export async function syncPreflightCheck(configs: {
197+
/** defaultGitInfo from ICommitAndSyncOptions */
198+
defaultGitInfo?: typeof defaultDefaultGitInfo;
199+
dir: string;
200+
logDebug?: (message: string, step: GitStep) => unknown;
201+
logProgress?: (step: GitStep) => unknown;
202+
logger?: ILogger;
203+
/** userInfo from ICommitAndSyncOptions */
204+
userInfo?: IGitUserInfos;
205+
}) {
206+
const { dir, logger, logProgress, logDebug, defaultGitInfo = defaultDefaultGitInfo, userInfo } = configs;
207+
const { gitUserName, email } = userInfo ?? defaultGitInfo;
208+
209+
const repoStartingState = await getGitRepositoryState(dir, logger);
210+
if (repoStartingState.length === 0 || repoStartingState === '|DIRTY') {
211+
logProgress?.(GitStep.PrepareSync);
212+
logDebug?.(`${dir} repoStartingState: ${repoStartingState}, ${gitUserName} <${email ?? defaultGitInfo.email}>`, GitStep.PrepareSync);
213+
} else if (repoStartingState === 'NOGIT') {
214+
throw new CantSyncGitNotInitializedError(dir);
215+
} else {
216+
// we may be in middle of a rebase, try fix that
217+
await continueRebase(dir, gitUserName, email ?? defaultGitInfo.email, logger, repoStartingState);
218+
}
219+
}

src/forcePull.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { GitProcess } from 'dugite';
2+
import { syncPreflightCheck } from './commitAndSync';
23
import { credentialOff, credentialOn } from './credential';
34
import { defaultGitInfo as defaultDefaultGitInfo } from './defaultGitInfo';
45
import { CantForcePullError, SyncParameterMissingError } from './errors';
5-
import { getDefaultBranchName, getRemoteName } from './inspect';
6+
import { getDefaultBranchName, getRemoteName, getSyncState } from './inspect';
67
import { GitStep, IGitUserInfos, ILogger } from './interface';
78
import { fetchRemote } from './sync';
89

@@ -58,12 +59,28 @@ export async function forcePull(options: IForcePullOptions) {
5859
});
5960

6061
logProgress(GitStep.StartForcePull);
61-
logDebug(`Successfully Running git init for force pull in dir ${dir}`, GitStep.StartForcePull);
62+
logDebug(`Do preflight Check before force pull in dir ${dir}`, GitStep.StartForcePull);
63+
// preflight check
64+
await syncPreflightCheck({
65+
dir,
66+
logger,
67+
logProgress,
68+
logDebug,
69+
defaultGitInfo,
70+
userInfo,
71+
});
6272
logProgress(GitStep.StartConfiguringGithubRemoteRepository);
6373
await credentialOn(dir, remoteUrl, gitUserName, accessToken, remoteName);
6474
try {
6575
logProgress(GitStep.StartFetchingFromGithubRemote);
6676
await fetchRemote(dir, defaultGitInfo.remote, defaultGitInfo.branch);
77+
const syncState = await getSyncState(dir, defaultBranchName, remoteName, logger);
78+
logDebug(`syncState in dir ${dir} is ${syncState}`, GitStep.StartFetchingFromGithubRemote);
79+
if (syncState === 'equal') {
80+
// if there is no new commit in remote (and nothing messy in local), we don't need to pull.
81+
logProgress(GitStep.SkipForcePull);
82+
return;
83+
}
6784
logProgress(GitStep.StartResettingLocalToRemote);
6885
await hardResetLocalToRemote(dir, branch, remoteName);
6986
logProgress(GitStep.FinishForcePull);

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export enum GitStep {
6060
RebaseConflictNeedsResolve = 'RebaseConflictNeedsResolve',
6161
RebaseResultChecking = 'RebaseResultChecking',
6262
RebaseSucceed = 'RebaseSucceed',
63+
SkipForcePull = 'SkipForcePull',
6364
StartBackupToGitRemote = 'StartBackupToGitRemote',
6465
StartConfiguringGithubRemoteRepository = 'StartConfiguringGithubRemoteRepository',
6566
StartFetchingFromGithubRemote = 'StartFetchingFromGithubRemote',

0 commit comments

Comments
 (0)