Skip to content

Commit

Permalink
Show file contents from commit or stash
Browse files Browse the repository at this point in the history
  • Loading branch information
Rutherther committed Sep 9, 2023
1 parent ca9cb48 commit a40c0a1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/commands/diffingCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as VisitAtPoint from './visitAtPointCommands';
import * as Constants from '../common/constants';
import { Section } from '../views/general/sectionHeader';
import { Status } from '../typings/git';
import { MagitChange } from '../models/magitChange';
import { ContextualMagitChange, MagitChange } from '../models/magitChange';
import { Stash } from '../models/stash';
import ViewUtils from '../utils/viewUtils';
import { constants } from 'buffer';
Expand Down Expand Up @@ -104,7 +104,7 @@ async function showStash({ repository }: MenuState) {
}
}

function stashToMagitChanges(nameStatusText: string, diff: string): MagitChange[] {
export function stashToMagitChanges(repository: MagitRepository, contextId: string, nameStatusText: string, diff: string): ContextualMagitChange[] {
const DIFF_PREFIX = 'diff --git';
const filesWithStatus = nameStatusText.split(Constants.LineSplitterRegex).filter(t => t !== '').map(s => s.split('\t'));
const diffs = diff.split(DIFF_PREFIX).filter(r => r !== '');
Expand All @@ -115,11 +115,12 @@ function stashToMagitChanges(nameStatusText: string, diff: string): MagitChange[

return diffs.map((diff, idx) => {
const [status, ...paths] = filesWithStatus[idx];
const uri = Uri.file(paths[paths.length - 1]);
const uri = Uri.parse(repository.gitRepository.rootUri + '/' + paths[paths.length - 1]);
const fileStatus = getStatusFromString(status);
return {
diff,
uri,
contextId,
originalUri: uri,
status: fileStatus,
renameUri: undefined,
Expand Down Expand Up @@ -171,7 +172,7 @@ export async function showStashDetail(repository: MagitRepository, stash: Stash)
const nameStatusText = (await nameStatusTask).stdout;
const stashDiff = (await stashShowTask).stdout;

return ViewUtils.showView(uri, new StashDetailView(uri, stash, stashToMagitChanges(nameStatusText, stashDiff), stashUntrackedFiles));
return ViewUtils.showView(uri, new StashDetailView(uri, stash, stashToMagitChanges(repository, `stash@{${stash.index}}`, nameStatusText, stashDiff), stashUntrackedFiles));
}

async function showCommit({ repository }: MenuState) {
Expand Down
29 changes: 25 additions & 4 deletions src/commands/visitAtPointCommands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, workspace, TextEditorRevealType, Range, Position, Selection, commands } from 'vscode';
import { window, workspace, TextEditorRevealType, Range, Position, Selection, commands, WorkspaceEdit, Uri } from 'vscode';
import { MagitRepository } from '../models/magitRepository';
import { CommitItemView } from '../views/commits/commitSectionView';
import { DocumentView } from '../views/general/documentView';
Expand All @@ -21,6 +21,8 @@ import { PullRequestView } from '../views/forge/pullRequestView';
import { sep } from 'path';
import { ErrorMessageView } from '../views/errorMessageView';
import { processView } from './processCommands';
import { stashToMagitChanges } from './diffingCommands';
import { ContextualMagitChange } from '../models/magitChange';

export async function magitVisitAtPoint(repository: MagitRepository, currentView: DocumentView) {

Expand All @@ -36,8 +38,27 @@ export async function magitVisitAtPoint(repository: MagitRepository, currentView

const change = (selectedView as ChangeView).change;

if (change.hunks?.length) {
return visitHunk(selectedView.subViews.find(v => v instanceof HunkView) as HunkView);
/* Check if is ContextualMagitChange */
if ((change as ContextualMagitChange).contextId !== undefined) {
const contextual = change as ContextualMagitChange;
const content = await gitRun(repository.gitRepository, ['show', `${contextual.contextId}:${change.relativePath}`]);

let path = change.uri.path;
let filename = path.split('/').pop() ?? '';
let extension = filename.split('.', 2).pop() ?? '';
if (extension.length > 0) {
path = path.slice(0, -extension.length - 1);
}

workspace.openTextDocument(Uri.parse(`untitled:${path}.~${contextual.contextId}~.${extension}`))
.then(doc => window.showTextDocument(doc))
.then(editor => {
editor.edit((edit) => {
edit.insert(new Position(0, 0), content.stdout);
});
});
} else if (change.hunks?.length) {
return visitHunk(selectedView.subViews.find(v => v instanceof HunkView) as HunkView);
} else {

// Check if change path is a directory. Reveal directories in file explorer
Expand Down Expand Up @@ -145,5 +166,5 @@ export async function visitCommit(repository: MagitRepository, commitHash: strin
const commit: MagitCommit = { hash: commitHash, message: '', parents: [] };

const uri = CommitDetailView.encodeLocation(repository, commit.hash);
return ViewUtils.showView(uri, new CommitDetailView(uri, commit, result.stdout));
return ViewUtils.showView(uri, new CommitDetailView(uri, commit, header.stdout, stashToMagitChanges(repository, commitHash, nameStatus.stdout, diffs.stdout)));
}
4 changes: 4 additions & 0 deletions src/models/magitChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export interface MagitChange extends Change {
diff?: string;
relativePath?: string;
}

export interface ContextualMagitChange extends MagitChange {
contextId: string
}

0 comments on commit a40c0a1

Please sign in to comment.