All files / app/actions diff.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93          5x                     5x         5x           5x                                                                 5x                                                                
import { ICommit } from 'app/interfaces';
import { ActionTypes } from 'app/actions/ActionTypes';
import { isVscode, vscode } from 'app/actions/vscode';
import { debug } from 'app/utilities/logger';
 
const requestDiff = (
  path: string,
  leftCommit: ICommit,
  rightCommit: ICommit
) => ({
  leftCommit,
  rightCommit,
  selectedPath: path,
  type: ActionTypes.REQUEST_DIFF,
});
 
const requestDeferredDiff = (path: string) => ({
  selectedPath: path,
  type: ActionTypes.REQUEST_DEFERRED_DIFF,
});
 
const _receiveDiff = (path: string, diff: any) => ({
  diff,
  selectedPath: path,
  type: ActionTypes.RECEIVE_DIFF,
});
 
export const receiveDiff = (path: string, diff: any) => (
  dispatch: any,
  getState: any
) => {
  // We fetch the diff initially with left and right commit
  // of null which returns the diff of uncommited changes.
  // If there are are no uncommited changes, show the changes
  // made by the last commit.
  const { diffLeftCommit, diffRightCommit, isFetching, commits } = getState();
  const areDefaults = !diffLeftCommit && !diffRightCommit;
  const isUnchangedFile =
    !diff.isDirectory &&
    ((!diff.leftFileContents && !diff.rightFileContents) ||
      diff.leftFileContents === diff.rightFileContents);
  const isUnchangedDirectory =
    diff.isDirectory &&
    (!diff.modifiedFiles || diff.modifiedFiles.length === 0);
  const areCommits = commits && commits.length > 1;
  if (areDefaults && (isUnchangedFile || isUnchangedDirectory)) {
    if (commits && commits.length > 1) {
      dispatch(fetchDiff(path, commits[1], commits[0]));
    } else if (isFetching) {
      // this will cause the next receive commits to call a fetchDiff
      // with the two most recent commits
      dispatch(requestDeferredDiff(path));
    } else {
      dispatch(_receiveDiff(path, diff));
    }
  } else {
    dispatch(_receiveDiff(path, diff));
  }
};
 
export const fetchDiff = (
  path: string,
  leftCommit?: ICommit,
  rightCommit?: ICommit
) => (dispatch: any, getState: any): void => {
  // set state vars first for isDiffFetching
  dispatch(requestDiff(path, leftCommit, rightCommit));
 
  if (isVscode) {
    debug('sending diff request to vscode ', path, leftCommit, rightCommit);
    // see actions/vscode.ts for response handling that comes as a window event
    vscode.postMessage({
      path,
      leftCommit: (leftCommit && leftCommit.id) || null,
      rightCommit: (rightCommit && rightCommit.id) || null,
      command: 'diff',
    });
  } else {
    const pathParam =
      path && path.trim().length > 0 ? `?path=${path}` : '?path=.';
    const leftCommitParam = leftCommit ? `&leftCommit=${leftCommit.id}` : '';
    const rightCommitParam = rightCommit
      ? `&rightCommit=${rightCommit.id}`
      : '';
 
    // TODO : replace this with serviceBaseUrl when it is in
    const url = `http://localhost:11966/git-temporal/diff${pathParam}${leftCommitParam}${rightCommitParam}`;
    fetch(url)
      .then(response => response.json())
      .then(diff => dispatch(receiveDiff(path, diff)));
  }
};