Skip to content

Commit

Permalink
Fixes applying changes for adds and untracked files
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 11, 2018
1 parent 0e3ecee commit 1ae014e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Fixes [#588](https://github.com/eamodio/vscode-gitlens/issues/588) — Output channel doesn't exist with `gitlens.outputLevel` default
- Fixes an issue where comparing a file with its staged revision doesn't show any content
- Fixes an issue where the workspace folder added by the _Explore Repository from Here_ command (`gitlens.views.exploreRepoRevision`) would fail to load in certain cases
- Fixes an issue where applying changes of an added file or an untracked file (in a stash) would fail

## [9.0.3] - 2018-12-06

Expand Down
51 changes: 42 additions & 9 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export * from './models/models';
export * from './parsers/parsers';
export * from './remotes/provider';

// This is a root sha of all git repo's if using sha1
const rootSha = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';

const defaultBlameParams = ['blame', '--root', '--incremental'];

// Using %x00 codes because some shells seem to try to expand things if not
Expand Down Expand Up @@ -52,7 +55,7 @@ const stashFormat = [
const defaultStashParams = ['stash', 'list', '--name-status', '-M', `--format=${stashFormat}`];

const GitErrors = {
badRevision: /bad revision \'.*?\'/i,
badRevision: /bad revision \'(.*?)\'/i,
notAValidObjectName: /Not a valid object name/i
};

Expand Down Expand Up @@ -477,22 +480,52 @@ export class Git {
return data.length === 0 ? undefined : data.trim();
}

static diff(repoPath: string, fileName: string, ref1?: string, ref2?: string, options: { encoding?: string } = {}) {
const params = ['diff', '--diff-filter=M', '-M', '--no-ext-diff', '--minimal'];
static async diff(
repoPath: string,
fileName: string,
ref1?: string,
ref2?: string,
options: { encoding?: string; filter?: string } = {}
): Promise<string> {
const params = ['diff', '-M', '--no-ext-diff', '--minimal'];
if (options.filter) {
params.push(`--diff-filter=${options.filter}`);
}

if (ref1) {
// <sha>^3 signals an untracked file in a stash and if we are trying to find its parent, use the root sha
if (ref1.endsWith('^3^')) {
ref1 = rootSha;
}
params.push(Git.isStagedUncommitted(ref1) ? '--staged' : ref1);
}
if (ref2) {
params.push(Git.isStagedUncommitted(ref2) ? '--staged' : ref2);
}

const encoding: BufferEncoding = options.encoding === 'utf8' ? 'utf8' : 'binary';
return git<string>(
{ cwd: repoPath, configs: ['-c', 'color.diff=false'], encoding: encoding },
...params,
'--',
fileName
);

try {
return await git<string>(
{ cwd: repoPath, configs: ['-c', 'color.diff=false'], encoding: encoding },
...params,
'--',
fileName
);
}
catch (ex) {
const match = GitErrors.badRevision.exec(ex.message);
if (match !== null) {
const [, ref] = match;

// If the bad ref is trying to find a parent ref, assume we hit to the last commit, so try again using the root sha
if (ref === ref1 && ref.endsWith('^')) {
return Git.diff(repoPath, fileName, rootSha, ref2, options);
}
}

throw ex;
}
}

static diff_nameStatus(repoPath: string, ref1?: string, ref2?: string, options: { filter?: string } = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ export class GitService implements Disposable {
const [file, root] = Git.splitPath(fileName, repoPath, false);

try {
const data = await Git.diff(root, file, ref1, ref2, options);
const data = await Git.diff(root, file, ref1, ref2, { ...options, filter: 'M' });
const diff = GitDiffParser.parse(data);
return diff;
}
Expand Down

0 comments on commit 1ae014e

Please sign in to comment.