Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Revert "Merge pull request #487 from atom/ku-discard-lines"
Browse files Browse the repository at this point in the history
Due to bug in discard logic #509

This reverts commit b11e36b, reversing
changes made to 8e09a04.
  • Loading branch information
kuychaco committed Feb 11, 2017
1 parent c83e888 commit 4da74c3
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 932 deletions.
26 changes: 0 additions & 26 deletions lib/controllers/file-patch-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,18 @@ export default class FilePatchController {
);
} else {
// NOTE: Outer div is required for etch to render elements correctly
const filePath = this.props.filePatch.getPath();
const hasUndoHistory = this.props.repository ? this.hasUndoHistory() : false;
return (
<div className="github-PaneView pane-item">
<FilePatchView
ref="filePatchView"
commandRegistry={this.props.commandRegistry}
attemptLineStageOperation={this.attemptLineStageOperation}
didSurfaceFile={this.didSurfaceFile}
didDiveIntoCorrespondingFilePatch={this.diveIntoCorrespondingFilePatch}
attemptHunkStageOperation={this.attemptHunkStageOperation}
hunks={hunks}
filePath={filePath}
stagingStatus={this.props.stagingStatus}
isPartiallyStaged={this.props.isPartiallyStaged}
registerHunkView={this.props.registerHunkView}
openCurrentFile={this.openCurrentFile}
discardLines={this.props.discardLines}
undoLastDiscard={this.undoLastDiscard}
hasUndoHistory={hasUndoHistory}
/>
</div>
);
Expand Down Expand Up @@ -172,14 +164,6 @@ export default class FilePatchController {
}
}

@autobind
diveIntoCorrespondingFilePatch() {
const filePath = this.props.filePatch.getPath();
const stagingStatus = this.props.stagingStatus === 'staged' ? 'unstaged' : 'staged';
this.props.quietlySelectItem(filePath, stagingStatus);
return this.props.didDiveIntoFilePath(filePath, stagingStatus, {amending: this.props.isAmending});
}

didUpdateFilePatch() {
// FilePatch was mutated so all we need to do is re-render
return etch.update(this);
Expand All @@ -203,14 +187,4 @@ export default class FilePatchController {
textEditor.setCursorBufferPosition(position);
return textEditor;
}

@autobind
undoLastDiscard() {
return this.props.undoLastDiscard(this.props.filePatch.getPath());
}

@autobind
hasUndoHistory() {
return this.props.repository.hasUndoHistory(this.props.filePatch.getPath());
}
}
108 changes: 2 additions & 106 deletions lib/controllers/git-controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import {CompositeDisposable, Disposable, File, TextBuffer} from 'atom';
import {CompositeDisposable, Disposable, File} from 'atom';

import React from 'react';
import {autobind} from 'core-decorators';
Expand All @@ -18,14 +18,11 @@ import GitPanelController from './git-panel-controller';
import StatusBarTileController from './status-bar-tile-controller';
import ModelObserver from '../models/model-observer';
import ModelStateRegistry from '../models/model-state-registry';
import discardChangesInBuffer from '../discard-changes-in-buffer';
import {CannotRestoreError} from '../models/file-discard-history';

const nullFilePatchState = {
filePath: null,
filePatch: null,
stagingStatus: null,
partiallyStaged: null,
};

export default class GitController extends React.Component {
Expand Down Expand Up @@ -185,15 +182,9 @@ export default class GitController extends React.Component {
commandRegistry={this.props.commandRegistry}
filePatch={this.state.filePatch}
stagingStatus={this.state.stagingStatus}
isAmending={this.state.amending}
isPartiallyStaged={this.state.partiallyStaged}
onRepoRefresh={this.onRepoRefresh}
didSurfaceFile={this.surfaceFromFileAtPath}
didDiveIntoFilePath={this.diveIntoFilePatchForPath}
quietlySelectItem={this.quietlySelectItem}
openFiles={this.openFiles}
discardLines={this.discardLines}
undoLastDiscard={this.undoLastDiscard}
/>
</EtchWrapper>
</PaneItem>
Expand All @@ -220,10 +211,9 @@ export default class GitController extends React.Component {

const staged = stagingStatus === 'staged';
const filePatch = await repository.getFilePatchForPath(filePath, {staged, amending: staged && amending});
const partiallyStaged = await repository.isPartiallyStaged(filePath);
return new Promise(resolve => {
if (filePatch) {
this.setState({filePath, filePatch, stagingStatus, partiallyStaged}, () => {
this.setState({filePath, filePatch, stagingStatus}, () => {
// TODO: can be better done w/ a prop?
if (activate && this.filePatchControllerPane) {
this.filePatchControllerPane.activate();
Expand Down Expand Up @@ -346,98 +336,4 @@ export default class GitController extends React.Component {
handleChangeTab(activeTab) {
this.setState({activeTab});
}

@autobind
async discardLines(lines) {
const relFilePath = this.state.filePatch.getPath();
const absfilePath = path.join(this.props.repository.getWorkingDirectoryPath(), relFilePath);
let buffer, disposable;
const isSafe = async () => {
const editor = this.props.workspace.getTextEditors().find(e => e.getPath() === absfilePath);
if (editor) {
buffer = editor.getBuffer();
if (buffer.isModified()) {
this.props.notificationManager.addError('Cannot discard lines.', {description: 'You have unsaved changes.'});
return false;
}
} else {
buffer = new TextBuffer({filePath: absfilePath, load: true});
await new Promise(resolve => {
disposable = buffer.onDidReload(() => {
disposable.dispose();
resolve();
});
});
}
return true;
};
const snapshots = await this.props.repository.storeBeforeAndAfterBlobs(relFilePath, isSafe, () => {
this.discardChangesInBuffer(buffer, this.state.filePatch, lines);
});
if (disposable) { disposable.dispose(); }
return snapshots;
}

discardChangesInBuffer(buffer, filePatch, lines) {
discardChangesInBuffer(buffer, filePatch, lines);
}

@autobind
async undoLastDiscard(filePath) {
const relFilePath = this.state.filePatch.getPath();
const absfilePath = path.join(this.props.repository.getWorkingDirectoryPath(), relFilePath);
const isSafe = () => {
const editor = this.props.workspace.getTextEditors().find(e => e.getPath() === absfilePath);
if (editor && editor.getBuffer().isModified()) {
this.notifyInabilityToUndo(relFilePath, 'You have unsaved changes.');
return false;
}
return true;
};
try {
await this.props.repository.attemptToRestoreBlob(filePath, isSafe);
} catch (e) {
if (e instanceof CannotRestoreError) {
this.notifyInabilityToUndo(relFilePath, 'Contents have been modified since last discard.');
} else if (e.stdErr.match(/fatal: Not a valid object name/)) {
this.notifyInabilityToUndo(relFilePath, 'Discard history has expired.');
this.props.repository.clearDiscardHistoryForPath(filePath);
} else {
// eslint-disable-next-line no-console
console.error(e);
}
}
}

notifyInabilityToUndo(filePath, description) {
const openPreDiscardVersion = () => this.openFileBeforeLastDiscard(filePath);
this.props.notificationManager.addError(
'Cannot undo last discard.',
{
description: `${description} Would you like to open pre-discard version of "${filePath}" in new buffer?`,
buttons: [{
text: 'Open in new buffer',
onDidClick: openPreDiscardVersion,
dismissable: true,
}],
},
);
}

async openFileBeforeLastDiscard(filePath) {
const {beforeSha} = await this.props.repository.getLastHistorySnapshotsForPath(filePath);
const contents = await this.props.repository.getBlobContents(beforeSha);
const editor = await this.props.workspace.open();
editor.setText(contents);
return editor;
}

@autobind
quietlySelectItem(filePath, stagingStatus) {
if (this.gitPanelController) {
return this.gitPanelController.getWrappedComponent().quietlySelectItem(filePath, stagingStatus);
} else {
return null;
}
}
}
5 changes: 0 additions & 5 deletions lib/controllers/git-panel-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,4 @@ export default class GitPanelController {
isFocused() {
return this.refs.gitPanel.isFocused();
}

@autobind
quietlySelectItem(filePath, stagingStatus) {
return this.refs.gitPanel.quitelySelectItem(filePath, stagingStatus);
}
}
35 changes: 0 additions & 35 deletions lib/discard-changes-in-buffer.js

This file was deleted.

40 changes: 2 additions & 38 deletions lib/git-shell-out-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {parse as parseDiff} from 'what-the-diff';

import GitPromptServer from './git-prompt-server';
import AsyncQueue from './async-queue';
import {readFile, writeFile, fsStat, deleteFileOrFolder} from './helpers';
import {readFile, fsStat, deleteFileOrFolder} from './helpers';

const LINE_ENDING_REGEX = /\r?\n/;

Expand Down Expand Up @@ -36,7 +36,7 @@ export default class GitShellOutStrategy {
}

// Execute a command and read the output using the embedded Git environment
exec(args, stdin = null, useGitPromptServer = false, redirectFilePath = null) {
exec(args, stdin = null, useGitPromptServer = false) {
/* eslint-disable no-console */
const subscriptions = new CompositeDisposable();
return this.commandQueue.push(async () => {
Expand Down Expand Up @@ -99,9 +99,6 @@ export default class GitShellOutStrategy {
err.command = formattedArgs;
return Promise.reject(err);
}
if (redirectFilePath) {
return writeFile(redirectFilePath, stdout);
}
return stdout;
});
});
Expand Down Expand Up @@ -271,19 +268,6 @@ export default class GitShellOutStrategy {
return rawDiffs[0];
}

async isPartiallyStaged(filePath) {
const args = ['status', '--short', '--', filePath];
const output = await this.exec(args);
const results = output.trim().split(LINE_ENDING_REGEX);
if (results.length === 2) {
return true;
} else if (results.length === 1) {
return ['MM', 'AM', 'MD'].includes(results[0].slice(0, 2));
} else {
throw new Error(`Unexpected output for ${args.join(' ')}: ${output}`);
}
}

/**
* Miscellaneous getters
*/
Expand Down Expand Up @@ -461,26 +445,6 @@ export default class GitShellOutStrategy {
return [];
}
}

async createBlob({filePath, stdin} = {}) {
let output;
if (filePath) {
output = await this.exec(['hash-object', '-w', filePath]);
} else if (stdin) {
output = await this.exec(['hash-object', '-w', '--stdin'], stdin);
} else {
throw new Error('Must supply file path or stdin');
}
return output.trim();
}

async restoreBlob(filePath, sha) {
await this.exec(['cat-file', '-p', sha], null, null, path.join(this.workingDir, filePath));
}

async getBlobContents(sha) {
return await this.exec(['cat-file', '-p', sha]);
}
}

function buildAddedFilePatch(filePath, contents, stats) {
Expand Down
8 changes: 0 additions & 8 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ export function readFile(absoluteFilePath, encoding = 'utf8') {
});
}

export function writeFile(absoluteFilePath, contents) {
return new Promise((resolve, reject) => {
fs.writeFile(absoluteFilePath, contents, err => {
if (err) { return reject(err); } else { return resolve(); }
});
});
}

export function deleteFileOrFolder(path) {
return new Promise((resolve, reject) => {
fs.remove(path, err => {
Expand Down
Loading

0 comments on commit 4da74c3

Please sign in to comment.