Skip to content

Commit

Permalink
Fix git behind indicator
Browse files Browse the repository at this point in the history
Resolves: #419
  • Loading branch information
ggodlewski committed May 25, 2024
1 parent 0470d02 commit d785ddd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions apps/ui/src/components/GitToolBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<span class="badge" v-if="gitStats.headAhead > 0">
{{ gitStats.headAhead }} commits ahead remote
</span>
<span class="badge" v-if="gitStats.headAhead < 0">
{{ -gitStats.headAhead }} commits behind remote
<span class="badge" v-if="gitStats.headBehind > 0">
{{ gitStats.headBehind }} commits behind remote
</span>
</ToolButton>

Expand Down
4 changes: 2 additions & 2 deletions apps/ui/src/pages/GDocsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
<span class="badge" v-if="gitStats.headAhead > 0">
{{ gitStats.headAhead }} commits ahead remote
</span>
<span class="badge" v-if="gitStats.headAhead < 0">
{{ -gitStats.headAhead }} commits behind remote
<span class="badge" v-if="gitStats.headBehind > 0">
{{ gitStats.headBehind }} commits behind remote
</span>
</ToolButton>

Expand Down
23 changes: 12 additions & 11 deletions src/git/GitScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -834,6 +834,7 @@ export class GitScanner {
return {
initialized,
headAhead,
headBehind,
unstaged,
remote_branch: userConfig.remote_branch,
remote_url: initialized ? await this.getRemoteUrl() : null
Expand Down

0 comments on commit d785ddd

Please sign in to comment.