Skip to content

Commit

Permalink
Updates last fetched in commits view periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 15, 2020
1 parent db0eeef commit 8b62d3a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
36 changes: 31 additions & 5 deletions src/git/models/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Logger } from '../../logger';
import { Messages } from '../../messages';
import { GitBranchReference, GitReference, GitTagReference } from './models';
import { RemoteProviderFactory, RemoteProviders, RichRemoteProvider } from '../remotes/factory';
import { Arrays, debug, Functions, gate, Iterables, log, logName } from '../../system';
import { Arrays, Dates, debug, Functions, gate, Iterables, log, logName } from '../../system';
import { runGitCommandInTerminal } from '../../terminal';

const ignoreGitRegex = /\.git(?:\/|\\|$)/;
Expand Down Expand Up @@ -88,6 +88,21 @@ export interface RepositoryFileSystemChangeEvent {

@logName<Repository>((r, name) => `${name}(${r.id})`)
export class Repository implements Disposable {
static formatLastFetched(lastFetched: number): string {
return Date.now() - lastFetched < Dates.MillisecondsPerDay
? Dates.getFormatter(new Date(lastFetched)).fromNow()
: Dates.getFormatter(new Date(lastFetched)).format(
Container.config.defaultDateShortFormat ?? 'MMM D, YYYY',
);
}

static getLastFetchedUpdateInterval(lastFetched: number): number {
const timeDiff = Date.now() - lastFetched;
return timeDiff < Dates.MillisecondsPerDay
? (timeDiff < Dates.MillisecondsPerHour ? Dates.MillisecondsPerMinute : Dates.MillisecondsPerHour) / 2
: 0;
}

static sort(repositories: Repository[]) {
return repositories.sort((a, b) => (a.starred ? -1 : 1) - (b.starred ? -1 : 1) || a.index - b.index);
}
Expand Down Expand Up @@ -229,6 +244,8 @@ export class Repository implements Disposable {

@debug()
private onRepositoryChanged(uri: Uri | undefined) {
this._lastFetched = undefined;

if (uri == null) {
this.fireChange(RepositoryChange.Unknown);

Expand Down Expand Up @@ -454,16 +471,25 @@ export class Repository implements Disposable {
return Container.git.getContributors(this.path);
}

private _lastFetched: number | undefined;
@gate()
async getLastFetched(): Promise<number> {
const hasRemotes = await this.hasRemotes();
if (!hasRemotes || Container.vsls.isMaybeGuest) return 0;
if (this._lastFetched == null) {
const hasRemotes = await this.hasRemotes();
if (!hasRemotes || Container.vsls.isMaybeGuest) return 0;
}

try {
const stat = await workspace.fs.stat(Uri.file(paths.join(this.path, '.git/FETCH_HEAD')));
return stat.mtime;
// If the file is empty, assume the fetch failed, and don't update the timestamp
if (stat.size > 0) {
this._lastFetched = stat.mtime;
}
} catch {
return 0;
this._lastFetched = undefined;
}

return this._lastFetched ?? 0;
}

getRemotes(_options: { sort?: boolean } = {}): Promise<GitRemote[]> {
Expand Down
33 changes: 26 additions & 7 deletions src/views/commitsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CancellationToken,
commands,
ConfigurationChangeEvent,
Disposable,
ProgressLocation,
TreeItem,
TreeItemCollapsibleState,
Expand All @@ -16,6 +17,7 @@ import {
GitReference,
GitRemote,
GitRevisionReference,
Repository,
RepositoryChange,
RepositoryChangeEvent,
} from '../git/git';
Expand All @@ -29,7 +31,7 @@ import {
unknownGitUri,
ViewNode,
} from './nodes';
import { Dates, debug, gate, Strings } from '../system';
import { Dates, debug, Functions, gate, Strings } from '../system';
import { ViewBase } from './viewBase';

export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, BranchNode> {
Expand Down Expand Up @@ -87,9 +89,7 @@ export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, Bra

const status = branch?.getTrackingStatus();
item.description = `${status ? `${status} ${GlyphChars.Dot} ` : ''}${branch.name}${
lastFetched
? ` ${GlyphChars.Dot} Last fetched ${Dates.getFormatter(new Date(lastFetched)).fromNow()}`
: ''
lastFetched ? ` ${GlyphChars.Dot} Last fetched ${Repository.formatLastFetched(lastFetched)}` : ''
}`;

let providerName;
Expand Down Expand Up @@ -134,6 +134,27 @@ export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, Bra
await this.ensureSubscription();
}

@debug()
protected async subscribe() {
const lastFetched = (await this.repo?.getLastFetched()) ?? 0;

const interval = Repository.getLastFetchedUpdateInterval(lastFetched);
if (lastFetched !== 0 && interval > 0) {
return Disposable.from(
await super.subscribe(),
Functions.interval(() => {
// Check if the interval should change, and if so, reset it
if (interval !== Repository.getLastFetchedUpdateInterval(lastFetched)) {
void this.resetSubscription();
}
void this.view.triggerNodeChange(this);
}, interval),
);
}

return super.subscribe();
}

protected changed(e: RepositoryChangeEvent) {
return (
e.changed(RepositoryChange.Config) ||
Expand Down Expand Up @@ -179,9 +200,7 @@ export class CommitsViewNode extends ViewNode<CommitsView> {

const status = branch.getTrackingStatus();
this.view.description = `${status ? `${status} ${GlyphChars.Dot} ` : ''}${branch.name}${
lastFetched
? ` ${GlyphChars.Dot} Last fetched ${Dates.getFormatter(new Date(lastFetched)).fromNow()}`
: ''
lastFetched ? ` ${GlyphChars.Dot} Last fetched ${Repository.formatLastFetched(lastFetched)}` : ''
}`;
} else {
this.view.description = undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/views/contributorsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class ContributorsRepositoryNode extends RepositoryFolderNode<Contributor
}

@debug()
protected subscribe() {
protected async subscribe() {
return Disposable.from(
super.subscribe(),
await super.subscribe(),
Avatars.onDidFetch(e => this.child?.updateAvatar(e.email)),
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/views/nodes/viewNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ export abstract class SubscribeableViewNode<TView extends View = View> extends V
this.subscription = Promise.resolve(this.subscribe());
await this.subscription;
}

@gate()
@debug()
async resetSubscription() {
await this.unsubscribe();
await this.ensureSubscription();
}
}

export abstract class RepositoryFolderNode<
Expand Down Expand Up @@ -375,7 +382,7 @@ export abstract class RepositoryFolderNode<
}

@debug()
protected subscribe() {
protected subscribe(): Disposable | Promise<Disposable> {
return this.repo.onDidChange(this.onRepositoryChanged, this);
}

Expand Down

0 comments on commit 8b62d3a

Please sign in to comment.