Skip to content

Commit

Permalink
Fixes stashes with untracked files
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Sep 11, 2020
1 parent 984cdb8 commit e7c1888
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/git/models/logCommit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class GitLogCommit extends GitCommit {
toFileCommit(file: string | GitFile): GitLogCommit | undefined {
const fileName = typeof file === 'string' ? GitUri.relativeTo(file, this.repoPath) : file.fileName;
const foundFile = this.files.find(f => f.fileName === fileName);
if (foundFile === undefined) return undefined;
if (foundFile == null) return undefined;

let sha;
// If this is a stash commit with an untracked file
Expand Down
21 changes: 20 additions & 1 deletion src/git/models/stashCommit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
import { GitCommitType } from './commit';
import { Container } from '../../container';
import { GitFile } from './file';
import { GitLogCommit } from './logCommit';
import { GitReference } from './models';
import { memoize } from '../../system';
import { gate, memoize } from '../../system';

const stashNumberRegex = /stash@{(\d+)}/;

Expand Down Expand Up @@ -49,6 +50,24 @@ export class GitStashCommit extends GitLogCommit {
return this.stashName;
}

private _untrackedFilesChecked = false;
@gate()
async checkForUntrackedFiles() {
if (!this._untrackedFilesChecked) {
this._untrackedFilesChecked = true;

// Check for any untracked files -- since git doesn't return them via `git stash list` :(
// See https://stackoverflow.com/questions/12681529/
const commit = await Container.git.getCommit(this.repoPath, `${this.stashName}^3`);
if (commit != null && commit.files.length !== 0) {
// Since these files are untracked -- make them look that way
commit.files.forEach(s => (s.status = '?'));

this.files.push(...commit.files);
}
}
}

with(changes: {
type?: GitCommitType;
sha?: string | null;
Expand Down
32 changes: 8 additions & 24 deletions src/views/nodes/stashNode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict';
import * as paths from 'path';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { ViewFilesLayout } from '../../config';
import { Container } from '../../container';
import { CommitFormatter, GitStashCommit, GitStashReference } from '../../git/git';
import { Arrays, Iterables, Strings } from '../../system';
import { ContextValues, FileNode, FolderNode, RepositoryNode, StashFileNode, ViewNode, ViewRefNode } from '../nodes';
import { Arrays, Strings } from '../../system';
import { ViewsWithFiles } from '../viewBase';
import { StashFileNode } from './stashFileNode';
import { ContextValues, ViewNode, ViewRefNode } from './viewNode';
import { RepositoryNode } from './repositoryNode';
import { FileNode, FolderNode } from '../nodes';
import { ViewFilesLayout } from '../../config';

export class StashNode extends ViewRefNode<ViewsWithFiles, GitStashReference> {
static key = ':stash';
Expand All @@ -34,25 +31,12 @@ export class StashNode extends ViewRefNode<ViewsWithFiles, GitStashReference> {
}

async getChildren(): Promise<ViewNode[]> {
let files = this.commit.files;

// Check for any untracked files -- since git doesn't return them via `git stash list` :(
// See https://stackoverflow.com/questions/12681529/
const log = await Container.git.getLog(this.commit.repoPath, {
limit: 1,
ref: `${this.commit.stashName}^3`,
});
if (log != null) {
const commit = Iterables.first(log.commits.values());
if (commit != null && commit.files.length !== 0) {
// Since these files are untracked -- make them look that way
commit.files.forEach(s => (s.status = '?'));
// Ensure we have checked for untracked files
await this.commit.checkForUntrackedFiles();

files = { ...files, ...commit.files };
}
}

let children: FileNode[] = files.map(s => new StashFileNode(this.view, this, s, this.commit.toFileCommit(s)!));
let children: FileNode[] = this.commit.files.map(
s => new StashFileNode(this.view, this, s, this.commit.toFileCommit(s)!),
);

if (this.view.config.files.layout !== ViewFilesLayout.List) {
const hierarchy = Arrays.makeHierarchical(
Expand Down

0 comments on commit e7c1888

Please sign in to comment.