Skip to content

Fix git file changes #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/components/FileTree/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const state = observable({
get root () {
return this.nodes.get(ROOT_PATH)
},
get gitStatus () {
return this.nodes.values().filter(node => !node.isDir && node.gitStatus !== 'CLEAN')
},
toJS () {
return stateToJS(this)
}
Expand Down
60 changes: 55 additions & 5 deletions app/components/FileTree/subscribeToFileChange.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
/* @flow weak */
import config from '../../config'
import api from '../../backendAPI'
import store, { dispatch } from '../../store'
import store, { getState, dispatch } from '../../store'
import mobxStore from '../../mobxStore'
import * as FileTreeActions from './actions'
import * as GitActions from '../Git/actions'
import * as TabActions from 'commons/Tab/actions'

function handleGitFiles (node) {
const path = node.path
const pathArray = path.split('/.git/refs/heads/')
if (pathArray.length > 1) {
const branchName = pathArray[1]
const gitState = getState().GitState
const current = gitState.branches.current
if (branchName === current) {
const history = gitState.history
const focusedNodes = Object.values(getState().FileTreeState.nodes).filter(node => node.isFocused)
const historyPath = focusedNodes[0] ? focusedNodes[0].path : '/'
dispatch(GitActions.fetchHistory({
path: historyPath,
size: history.size,
page: 0,
reset: true
}))
api.gitStatus().then(({ files, clean }) => {
const gitStatus = mobxStore.FileTreeState.gitStatus
const result = gitStatus.map(node => {
const file = files.find(file => ('/' + file.name) === node.path)
return {
...node,
gitStatus: file ? file.status : 'CLEAN',
}
})
dispatch(
FileTreeActions.loadNodeData(
result
)
)
})
}
return true
}
return false
}

export default function subscribeToFileChange () {
return api.websocketConnectedPromise.then(client =>
client.subscribe(`/topic/ws/${config.spaceKey}/change`, frame => {
return api.websocketConnectedPromise.then(client => {
client.subscribe(`/topic/ws/${config.spaceKey}/change`, (frame) => {
const data = JSON.parse(frame.body)
const node = data.fileInfo
switch (data.changeType) {
case 'create':
if (handleGitFiles(node)) {
break
}
dispatch(FileTreeActions.loadNodeData([node]))
break
case 'modify':
if (handleGitFiles(node)) {
break
}
dispatch(FileTreeActions.loadNodeData([node]))
var tabsToUpdate = mobxStore.EditorTabState.tabs.values().filter(tab => tab.path === node.path)
const tabsToUpdate = mobxStore.EditorTabState.tabs.values().filter(tab => tab.path === node.path)
if (tabsToUpdate.length) {
api.readFile(node.path).then(({ content }) => {
dispatch(TabActions.updateTabByPath({
Expand All @@ -32,5 +77,10 @@ export default function subscribeToFileChange () {
break
}
})
)

client.subscribe(`/topic/git/${config.spaceKey}/checkout`, (frame) => {
const data = JSON.parse(frame.body)
dispatch(GitActions.updateCurrentBranch({ name: data.branch }))
})
})
}
4 changes: 2 additions & 2 deletions app/components/Git/GitHistoryView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class History extends Component {
})
this.fitHistoryTable()
}
if (nextProps.focusedNode !== this.props.focusedNode) {
if (nextProps.focusedNode && nextProps.focusedNode.path !== this.state.path) {
this.state.path = nextProps.focusedNode ? nextProps.focusedNode.path : '/'
this.fetchHistory({ reset: true })
}
Expand Down Expand Up @@ -257,7 +257,7 @@ class History extends Component {
this.setState({
page: this.state.page + 1
})
this.fetchHistory()
this.fetchHistory({ reset: false })
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/components/Git/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export function checkoutBranch (branch, remoteBranch) {
return dispatch => {
api.gitCheckout(branch, remoteBranch).then(data => {
if (data.status === 'OK') {
dispatch(createAction(GIT_CHECKOUT)({ branch }))
// 完全由 ws 里的 checkout 事件来改变显示
// dispatch(createAction(GIT_CHECKOUT)({ branch }))
dispatch(notify({message: `Check out ${branch}`}))
} else if (data.status === 'CONFLICTS') {
dispatch(createAction(GIT_CHECKOUT_FAILED)({ branch }))
Expand Down