From a608ed496e7312cefbc8d2e585b843cf11428f2a Mon Sep 17 00:00:00 2001 From: Grzegorz Godlewski Date: Sat, 25 May 2024 20:40:26 +0200 Subject: [PATCH] Fix git behind indicator Resolves: #419 --- apps/ui/src/components/GitToolBar.vue | 4 ++-- apps/ui/src/pages/GDocsView.vue | 4 ++-- src/git/GitScanner.ts | 23 ++++++++++++----------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/ui/src/components/GitToolBar.vue b/apps/ui/src/components/GitToolBar.vue index 2ab3ba6c..2c243dcd 100644 --- a/apps/ui/src/components/GitToolBar.vue +++ b/apps/ui/src/components/GitToolBar.vue @@ -9,8 +9,8 @@ {{ gitStats.headAhead }} commits ahead remote - - {{ -gitStats.headAhead }} commits behind remote + + {{ gitStats.headBehind }} commits behind remote diff --git a/apps/ui/src/pages/GDocsView.vue b/apps/ui/src/pages/GDocsView.vue index fec91701..ccca8fed 100644 --- a/apps/ui/src/pages/GDocsView.vue +++ b/apps/ui/src/pages/GDocsView.vue @@ -169,8 +169,8 @@ {{ gitStats.headAhead }} commits ahead remote - - {{ -gitStats.headAhead }} commits behind remote + + {{ gitStats.headBehind }} commits behind remote diff --git a/src/git/GitScanner.ts b/src/git/GitScanner.ts index a7289ae2..cefef44d 100644 --- a/src/git/GitScanner.ts +++ b/src/git/GitScanner.ts @@ -776,27 +776,27 @@ export class GitScanner { } } - async countAhead(remoteBranch: string) { - let retVal = 0; - + async countAheadBehind(remoteBranch: string) { try { - const result = await this.exec(`git log origin/${remoteBranch}..HEAD`, { + const result = await this.exec(`git rev-list --left-right --count HEAD...origin/${remoteBranch}`, { skipLogger: true }); - for (const line of result.stdout.split('\n')) { - if (line.startsWith('commit ')) { - retVal++; - } - } + const firstLine = result.stdout.split('\n')[0]; + + const [ ahead, behind ] = firstLine.split(/\s+/).map(val => parseInt(val)); + + return { + ahead, behind + }; // eslint-disable-next-line no-empty } catch (ignore) {} - return retVal; + return { ahead: 0, behind: 0 }; } async getStats(userConfig: UserConfig) { let initialized = true; - const headAhead = userConfig.remote_branch ? await this.countAhead(userConfig.remote_branch) : 0; + const { ahead: headAhead, behind: headBehind } = userConfig.remote_branch ? await this.countAheadBehind(userConfig.remote_branch) : { ahead: 0, behind: 0 }; let unstaged = 0; try { @@ -834,6 +834,7 @@ export class GitScanner { return { initialized, headAhead, + headBehind, unstaged, remote_branch: userConfig.remote_branch, remote_url: initialized ? await this.getRemoteUrl() : null