-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
758f3c2
commit b1d78c1
Showing
10 changed files
with
417 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Extension, extensions } from 'vscode'; | ||
import * as Git from './git'; | ||
|
||
export default class GitProvider { | ||
branch?: string; | ||
|
||
/** | ||
* Listen to branch changes | ||
*/ | ||
onDidChangeBranch?: (branch?: string) => void; | ||
|
||
/** | ||
* Listen to git initialization | ||
*/ | ||
onDidInitialize?: (branch?: string) => void; | ||
|
||
/** | ||
* Access to the native git extension | ||
*/ | ||
private git: Git.API; | ||
|
||
constructor() { | ||
const gitExtension: Extension<Git.GitExtension> | undefined = extensions.getExtension('vscode.git'); | ||
|
||
if (!gitExtension) { | ||
throw new Error('Could not found the vscode.git extension'); | ||
} | ||
|
||
this.git = gitExtension.exports.getAPI(1); | ||
|
||
if (this.git.state === 'initialized') { | ||
this.branch = this.getBranch(); | ||
this.listenToBranchChange(); | ||
if (this.onDidInitialize) { | ||
this.onDidInitialize(this.branch); | ||
} | ||
} else { | ||
this.git.onDidChangeState((state) => { | ||
if (state === 'initialized') { | ||
this.branch = this.getBranch(); | ||
this.listenToBranchChange(); | ||
if (this.onDidInitialize) { | ||
this.onDidInitialize(this.branch); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
setBranch(branch: string): void { | ||
this.git.repositories[0].checkout(branch); | ||
} | ||
|
||
private listenToBranchChange() { | ||
this.git.repositories[0].state.onDidChange(() => { | ||
const newBranch = this.getBranch(); | ||
if (newBranch !== this.branch) { | ||
this.branch = newBranch; | ||
if (this.onDidChangeBranch) { | ||
this.onDidChangeBranch(this.branch); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private getBranch(): string | undefined { | ||
if (this.git && this.git.repositories[0] && this.git.repositories[0].state.HEAD) { | ||
return this.git.repositories[0].state.HEAD.name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.