Skip to content

Commit

Permalink
Improves apply/restore for deleted files
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Oct 10, 2023
1 parent 3bce79a commit 0448e1b
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/git/actions/commit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TextDocumentShowOptions } from 'vscode';
import { env, Range, Uri, window } from 'vscode';
import { env, Range, Uri, window, workspace } from 'vscode';
import type { DiffWithCommandArgs } from '../../commands/diffWith';
import type { DiffWithPreviousCommandArgs } from '../../commands/diffWithPrevious';
import type { DiffWithWorkingCommandArgs } from '../../commands/diffWithWorking';
Expand All @@ -22,31 +22,39 @@ import type { GitFile } from '../models/file';
import type { GitRevisionReference } from '../models/reference';
import { getReferenceFromRevision, isUncommitted, isUncommittedStaged } from '../models/reference';

export async function applyChanges(file: string | GitFile, ref1: GitRevisionReference, ref2?: GitRevisionReference) {
export async function applyChanges(file: string | GitFile, rev1: GitRevisionReference, rev2?: GitRevisionReference) {
let create = false;
let ref = ref1.ref;
let ref1 = rev1.ref;
let ref2 = rev2?.ref;
if (typeof file !== 'string') {
// If the file is `?` (untracked), then this must be a stash, so get the ^3 commit to access the untracked file
if (file.status === '?') {
ref = `${ref}^3`;
ref1 = `${ref1}^3`;
create = true;
} else if (file.status === 'A') {
create = true;
} else if (file.status === 'D') {
// If the file is deleted, check to see if it exists, if so, apply the delete, otherwise restore it from the previous commit
const uri = GitUri.fromFile(file, rev1.repoPath);
try {
await workspace.fs.stat(uri);
} catch {
create = true;

ref2 = ref1;
ref1 = `${ref1}^`;
}
}
}

if (create) {
const uri = GitUri.fromFile(file, ref1.repoPath);
await Container.instance.git.applyChangesToWorkingFile(uri, ref, ref2?.ref);
const uri = GitUri.fromFile(file, rev1.repoPath);
await Container.instance.git.applyChangesToWorkingFile(uri, ref1, ref2);
await openFile(uri, { preserveFocus: true, preview: false });
} else {
// Open the working file to ensure undo will work
await openFile(file, ref1, { preserveFocus: true, preview: false });
await Container.instance.git.applyChangesToWorkingFile(
GitUri.fromFile(file, ref1.repoPath, ref),
ref,
ref2?.ref,
);
await openFile(file, rev1, { preserveFocus: true, preview: false });
await Container.instance.git.applyChangesToWorkingFile(GitUri.fromFile(file, rev1.repoPath, ref1), ref1, ref2);
}
}

Expand Down Expand Up @@ -550,7 +558,20 @@ export async function restoreFile(file: string | GitFile, revision: GitRevisionR
ref = revision.ref;
} else {
path = file.path;
ref = file.status === `?` ? `${revision.ref}^3` : file.status === 'D' ? `${revision.ref}^` : revision.ref;
if (file.status === 'D') {
// If the file is deleted, check to see if it exists, if so, restore it from the previous commit, otherwise restore it from the current commit
const uri = GitUri.fromFile(file, revision.repoPath);
try {
await workspace.fs.stat(uri);
ref = `${revision.ref}^`;
} catch {
ref = revision.ref;
}
} else if (file.status === '?') {
ref = `${revision.ref}^3`;
} else {
ref = revision.ref;
}
}

await Container.instance.git.checkout(revision.repoPath, ref, { path: path });
Expand Down

0 comments on commit 0448e1b

Please sign in to comment.