<commit_before>import * as React from 'react'
import { Button } from '../lib/button'
import { Dispatcher } from '../../lib/dispatcher'
import { Branch } from '../../models/branch'
import { Repository } from '../../models/repository'
import { getAheadBehind } from '../../lib/git'
import { ButtonGroup } from '../lib/button-group'
import { Dialog, DialogContent, DialogFooter } from '../dialog'
import { BranchList } from '../branches/branch-list'

interface IMergeProps {
  readonly dispatcher: Dispatcher
  readonly repository: Repository

  readonly defaultBranch: Branch | null
  readonly currentBranch: Branch | null
  readonly allBranches: ReadonlyArray<Branch>
  readonly recentBranches: ReadonlyArray<Branch>

  readonly onDismissed: () => void
}

interface IMergeState {
  /** The currently selected branch. */
  readonly selectedBranch: Branch | null

  /** The number of commits that would be brought in by the merge. */
  readonly commitCount: number
}

/** A component for merging a branch into the current branch. */
export class Merge extends React.Component<IMergeProps, IMergeState> {
  public constructor(props: IMergeProps) {
    super(props)

    const currentBranch = props.currentBranch
    const defaultBranch = props.defaultBranch

    this.state = {
      // Select the default branch unless that's currently checked out
      selectedBranch: currentBranch === defaultBranch ? null : defaultBranch,
      commitCount: 0,
    }
  }

  public componentDidMount() {
    const branch = this.state.selectedBranch
    if (!branch) { return }

    this.updateCommitCount(branch)
  }

  private onFilterKeyDown = (filter: string, event: React.KeyboardEvent<HTMLInputElement>) => {
    if (event.key === 'Escape') {
      if (filter.length === 0) {
        this.props.onDismissed()
        event.preventDefault()
      }
    }
  }

  private onItemClick = (item: Branch) => {
  }

  private onSelectionChanged = (selectedBranch: Branch | null) => {
    if (selectedBranch) {
      this.setState({ selectedBranch })
      this.updateCommitCount(selectedBranch)
    } else {
      this.setState({ selectedBranch, commitCount: 0 })
    }
  }

  public render() {
    const selectedBranch = this.state.selectedBranch
    // const selectedValue = selectedBranch ? selectedBranch.name : null
    const disabled = !selectedBranch
    const countPlural = this.state.commitCount === 1 ? 'commit' : 'commits'
    return (
      <Dialog
        id='merge'
        title={__DARWIN__ ? 'Merge Branch' : 'Merge branch'}
        onDismissed={this.props.onDismissed}
        onSubmit={this.merge}
      >
        <DialogContent>
          <BranchList
            allBranches={this.props.allBranches}
            currentBranch={this.props.currentBranch}
            defaultBranch={this.props.defaultBranch}
            recentBranches={this.props.recentBranches}
            onFilterKeyDown={this.onFilterKeyDown}
            onItemClick={this.onItemClick}
            selectedBranch={this.state.selectedBranch}
            onSelectionChanged={this.onSelectionChanged}
          />
        </DialogContent>
        <DialogFooter>
          <ButtonGroup>
            <Button type='submit' disabled={disabled}>Merge</Button>
            <Button onClick={this.cancel}>Cancel</Button>
          </ButtonGroup>
          <p>This will bring in {this.state.commitCount} {countPlural}.</p>
        </DialogFooter>
      </Dialog>
    )
  }

  private async updateCommitCount(branch: Branch) {
    const range = `...${branch.name}`
    const aheadBehind = await getAheadBehind(this.props.repository, range)
    const commitCount = aheadBehind ? aheadBehind.behind : 0

    // The branch changed while we were waiting on the result of
    // `getAheadBehind`.
    if (this.state.selectedBranch !== branch) { return }

    this.setState({ ...this.state, commitCount })
  }

  private merge = () => {
    const branch = this.state.selectedBranch
    if (!branch) { return }

    this.props.dispatcher.mergeBranch(this.props.repository, branch.name)
    this.props.dispatcher.closePopup()
  }

  private cancel = () => {
    this.props.dispatcher.closePopup()
  }

  // private onBranchChange = (event: React.FormEvent<HTMLSelectElement>) => {
  //   const index = event.currentTarget.selectedIndex
  //   const branch = this.props.branches[index]

  //   this.setState({ ...this.state, selectedBranch: branch })

  //   this.updateCommitCount(branch)
  // }
}<commit_msg>Ditch the cancel button :/<commit_after>import * as React from 'react'
import { Button } from '../lib/button'
import { Dispatcher } from '../../lib/dispatcher'
import { Branch } from '../../models/branch'
import { Repository } from '../../models/repository'
import { getAheadBehind } from '../../lib/git'
import { ButtonGroup } from '../lib/button-group'
import { Dialog, DialogContent, DialogFooter } from '../dialog'
import { BranchList } from '../branches/branch-list'

interface IMergeProps {
  readonly dispatcher: Dispatcher
  readonly repository: Repository

  readonly defaultBranch: Branch | null
  readonly currentBranch: Branch | null
  readonly allBranches: ReadonlyArray<Branch>
  readonly recentBranches: ReadonlyArray<Branch>

  readonly onDismissed: () => void
}

interface IMergeState {
  /** The currently selected branch. */
  readonly selectedBranch: Branch | null

  /** The number of commits that would be brought in by the merge. */
  readonly commitCount: number
}

/** A component for merging a branch into the current branch. */
export class Merge extends React.Component<IMergeProps, IMergeState> {
  public constructor(props: IMergeProps) {
    super(props)

    const currentBranch = props.currentBranch
    const defaultBranch = props.defaultBranch

    this.state = {
      // Select the default branch unless that's currently checked out
      selectedBranch: currentBranch === defaultBranch ? null : defaultBranch,
      commitCount: 0,
    }
  }

  public componentDidMount() {
    const branch = this.state.selectedBranch
    if (!branch) { return }

    this.updateCommitCount(branch)
  }

  private onFilterKeyDown = (filter: string, event: React.KeyboardEvent<HTMLInputElement>) => {
    if (event.key === 'Escape') {
      if (filter.length === 0) {
        this.props.onDismissed()
        event.preventDefault()
      }
    }
  }

  private onItemClick = (item: Branch) => {
  }

  private onSelectionChanged = (selectedBranch: Branch | null) => {
    if (selectedBranch) {
      this.setState({ selectedBranch })
      this.updateCommitCount(selectedBranch)
    } else {
      this.setState({ selectedBranch, commitCount: 0 })
    }
  }

  public render() {
    const selectedBranch = this.state.selectedBranch
    // const selectedValue = selectedBranch ? selectedBranch.name : null
    const disabled = !selectedBranch
    const countPlural = this.state.commitCount === 1 ? 'commit' : 'commits'
    return (
      <Dialog
        id='merge'
        title={__DARWIN__ ? 'Merge Branch' : 'Merge branch'}
        onDismissed={this.props.onDismissed}
        onSubmit={this.merge}
      >
        <DialogContent>
          <BranchList
            allBranches={this.props.allBranches}
            currentBranch={this.props.currentBranch}
            defaultBranch={this.props.defaultBranch}
            recentBranches={this.props.recentBranches}
            onFilterKeyDown={this.onFilterKeyDown}
            onItemClick={this.onItemClick}
            selectedBranch={this.state.selectedBranch}
            onSelectionChanged={this.onSelectionChanged}
          />
        </DialogContent>
        <DialogFooter>
          <ButtonGroup>
            <Button type='submit' disabled={disabled}>
              Merge <strong>{this.state.selectedBranch ? this.state.selectedBranch.name : ''}</strong> into <strong>{this.props.currentBranch ? this.props.currentBranch.name : ''}</strong>
            </Button>
          </ButtonGroup>
          <p>This will bring in {this.state.commitCount} {countPlural}.</p>
        </DialogFooter>
      </Dialog>
    )
  }

  private async updateCommitCount(branch: Branch) {
    const range = `...${branch.name}`
    const aheadBehind = await getAheadBehind(this.props.repository, range)
    const commitCount = aheadBehind ? aheadBehind.behind : 0

    // The branch changed while we were waiting on the result of
    // `getAheadBehind`.
    if (this.state.selectedBranch !== branch) { return }

    this.setState({ ...this.state, commitCount })
  }

  private merge = () => {
    const branch = this.state.selectedBranch
    if (!branch) { return }

    this.props.dispatcher.mergeBranch(this.props.repository, branch.name)
    this.props.dispatcher.closePopup()
  }

  // private onBranchChange = (event: React.FormEvent<HTMLSelectElement>) => {
  //   const index = event.currentTarget.selectedIndex
  //   const branch = this.props.branches[index]

  //   this.setState({ ...this.state, selectedBranch: branch })

  //   this.updateCommitCount(branch)
  // }
}
{'fraction_non_alphanumeric': 0.11931502279550761, 'fraction_numerical': 0.0011119759813188034, 'mean_word_length': 2.9430074528715475, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '798422', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2562, 'n_words': 598}