Skip to content

Commit

Permalink
feat: show last commit time in status bar
Browse files Browse the repository at this point in the history
close #334
  • Loading branch information
Vinzent03 committed Mar 20, 2023
1 parent 94f62a0 commit 4525fef
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export abstract class GitManager {

abstract push(): Promise<number>;

abstract getUnpushedCommits(): Promise<number>;

abstract canPush(): Promise<boolean>;

abstract checkRequirements(): Promise<
Expand Down
16 changes: 15 additions & 1 deletion src/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ export class IsomorphicGit extends GitManager {
progressNotice?.hide();

const upstreamCommit = await this.resolveRef("HEAD");
this.plugin.lastUpdate = Date.now();
const changedFiles = await this.getFileChangesCount(
localCommit,
upstreamCommit
Expand Down Expand Up @@ -507,6 +506,21 @@ export class IsomorphicGit extends GitManager {
}
}

async getUnpushedCommits(): Promise<number> {
const status = await this.branchInfo();
const trackingBranch = status.tracking;
const currentBranch = status.current;
const localCommit = await this.resolveRef(currentBranch!);
const upstreamCommit = await this.resolveRef(trackingBranch!);

const changedFiles = await this.getFileChangesCount(
localCommit,
upstreamCommit
);

return changedFiles.length;
}

async canPush(): Promise<boolean> {
const status = await this.branchInfo();
const trackingBranch = status.tracking;
Expand Down
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default class ObsidianGit extends Plugin {
timeoutIDBackup?: number;
timeoutIDPush?: number;
timeoutIDPull?: number;
lastUpdate: number;
lastPulledFiles: FileStatusResult[];
gitReady = false;
promiseQueue: PromiseQueue = new PromiseQueue();
Expand Down Expand Up @@ -839,7 +838,6 @@ export default class ObsidianGit extends Plugin {
}

dispatchEvent(new CustomEvent("git-refresh"));
this.lastUpdate = Date.now();
this.setState(PluginState.idle);
}

Expand Down Expand Up @@ -1079,7 +1077,6 @@ export default class ObsidianGit extends Plugin {
console.log("Pushing....");
const pushedFiles = await this.gitManager.push();
console.log("Pushed!", pushedFiles);
this.lastUpdate = Date.now();
if (pushedFiles > 0) {
this.displayMessage(
`Pushed ${pushedFiles} ${
Expand Down
14 changes: 14 additions & 0 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ export class SimpleGit extends GitManager {
return remoteChangedFiles;
}

async getUnpushedCommits(): Promise<number> {
const status = await this.git.status();
const trackingBranch = status.tracking!;
const currentBranch = status.current!;
const remoteChangedFiles = (
await this.git.diffSummary(
[currentBranch, trackingBranch, "--"],
(err) => this.onError(err)
)
).changed;

return remoteChangedFiles;
}

async canPush(): Promise<boolean> {
// allow pushing in submodules even if the root has no changes.
if (this.plugin.settings.updateSubmodules === true) {
Expand Down
23 changes: 20 additions & 3 deletions src/statusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface StatusBarMessage {
export class StatusBar {
private messages: StatusBarMessage[] = [];
private currentMessage: StatusBarMessage | null;
private lastCommitTimestamp?: Date;
private unPushedCommits?: number;
public lastMessageTimestamp: number | null;
private base = "obsidian-git-statusbar-";
private iconEl: HTMLElement;
Expand All @@ -20,6 +22,8 @@ export class StatusBar {
private readonly plugin: ObsidianGit
) {
this.statusBarEl.setAttribute("aria-label-position", "top");

addEventListener("git-refresh", this.refreshCommitTimestamp.bind(this));
}

public displayMessage(message: string, timeout: number) {
Expand Down Expand Up @@ -63,9 +67,10 @@ export class StatusBar {
this.textEl.style.marginLeft = "5px";
this.iconEl.style.float = "left";
}

switch (this.plugin.state) {
case PluginState.idle:
this.displayFromNow(this.plugin.lastUpdate);
this.displayFromNow();
break;
case PluginState.status:
this.statusBarEl.ariaLabel = "Checking repository status...";
Expand Down Expand Up @@ -105,13 +110,18 @@ export class StatusBar {
}
}

private displayFromNow(timestamp: number): void {
private displayFromNow(): void {
const timestamp = this.lastCommitTimestamp;
if (timestamp) {
const moment = (window as any).moment;
const fromNow = moment(timestamp).fromNow();
this.statusBarEl.ariaLabel = `${
this.plugin.offlineMode ? "Offline: " : ""
}Last Git update: ${fromNow}`;
}Last Commit: ${fromNow}`;

if (this.unPushedCommits ?? 0 > 0) {
this.statusBarEl.ariaLabel += `\n(${this.unPushedCommits} unpushed commits)`;
}
} else {
this.statusBarEl.ariaLabel = this.plugin.offlineMode
? "Git is offline"
Expand All @@ -133,4 +143,11 @@ export class StatusBar {
}
this.statusBarEl.addClass(this.base + "idle");
}

private async refreshCommitTimestamp() {
this.lastCommitTimestamp =
await this.plugin.gitManager.getLastCommitTime();
this.unPushedCommits =
await this.plugin.gitManager.getUnpushedCommits();
}
}

0 comments on commit 4525fef

Please sign in to comment.