Show current branch in status bar

This commit is contained in:
Kevin Sawicki 2012-10-25 10:09:42 -07:00 committed by Corey Johnson
parent c425b58093
commit 14302c491d
4 changed files with 81 additions and 11 deletions

View File

@ -1,13 +1,19 @@
var $git = {};
(function() {
native function isRepository(path);
$git.isRepository = isRepository;
native function getRepositoryPath(path);
$git.getRepositoryPath = getRepositoryPath;
native function getRepository(path);
$git.getRepository = getRepository;
native function getHead();
native function getCurrentBranch(repository);
$git.getCurrentBranch = getCurrentBranch;
function GitRepository(path) {
var repo = getRepository(path);
repo.constructor = GitRepository;
repo.__proto__ = GitRepository.prototype;
return repo;
}
GitRepository.prototype.getHead = getHead;
this.GitRepository = GitRepository;
})();

View File

@ -4,6 +4,36 @@
#import <Cocoa/Cocoa.h>
namespace v8_extensions {
class GitRepository : public CefBase {
private:
bool exists;
git_repository *repo;
public:
GitRepository(CefRefPtr<CefV8Value> path) {
const char *repoPath = path->GetStringValue().ToString().c_str();
exists = git_repository_open(&repo, repoPath) == GIT_OK;
}
~GitRepository() {
if (repo)
git_repository_free(repo);
}
CefRefPtr<CefV8Value> GetHead() {
if (!exists)
return CefV8Value::CreateNull();
git_reference *head;
if (git_repository_head(&head, repo) == GIT_OK)
return CefV8Value::CreateString(git_reference_name(head));
else
return CefV8Value::CreateNull();
}
IMPLEMENT_REFCOUNTING(GitRepository);
};
Git::Git() : CefV8Handler() {
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"v8_extensions/git.js"];
@ -16,15 +46,29 @@ bool Git::Execute(const CefString& name,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
if (name == "isRepository") {
if (name == "getRepositoryPath") {
const char *path = arguments[0]->GetStringValue().ToString().c_str();
int length = strlen(path);
char repoPath[length];
bool isRepository = git_repository_discover(repoPath, length, path, 0, "") == GIT_OK;
retval = CefV8Value::CreateBool(isRepository);
char repoPath[GIT_PATH_MAX];
if (git_repository_discover(repoPath, length, path, 0, "") == GIT_OK)
retval = CefV8Value::CreateString(repoPath);
else
retval = CefV8Value::CreateNull();
return true;
}
if (name == "getRepository") {
CefRefPtr<CefBase> userData = new GitRepository(arguments[0]);
retval = CefV8Value::CreateObject(NULL);
retval->SetUserData(userData);
return true;
}
if (name == "getHead") {
GitRepository *userData = (GitRepository *)object->GetUserData().get();
retval = userData->GetHead();
return true;
}
return false;
}

16
src/app/git.coffee Normal file
View File

@ -0,0 +1,16 @@
module.exports =
class Git
@open: (path) ->
repoPath = $git.getRepositoryPath(path)
new Git(repoPath) if repoPath
constructor: (@repoPath) ->
@repo = new GitRepository(@repoPath)
getHead: ->
head = @repo.getHead()
return '' unless head
return head.substring(11) if head.indexOf('refs/heads/') is 0
return head.substring(10) if head.indexOf('refs/tags/') is 0
return head.substring(13) if head.indexOf('refs/remotes/') is 0

View File

@ -1,5 +1,6 @@
{View} = require 'space-pen'
_ = require 'underscore'
Git = require 'git'
module.exports =
class StatusBar extends View
@ -50,10 +51,13 @@ class StatusBar extends View
updatePathText: ->
path = @editor.getPath()
if path
@head = Git.open(path)?.getHead()
@currentPath.text(@rootView.project.relativize(path))
else
@currentPath.text('untitled')
updateCursorPositionText: ->
{ row, column } = @editor.getCursorBufferPosition()
@cursorPosition.text("#{row + 1},#{column + 1}")
cursorText = "#{row + 1},#{column + 1}"
cursorText = "(#{@head}) #{cursorText}" if @head
@cursorPosition.text(cursorText)