Add Git.checkoutHead(path)

This commit is contained in:
Kevin Sawicki 2012-11-03 15:09:43 -07:00
parent 67c1cfe3c6
commit 1019154db6
4 changed files with 69 additions and 0 deletions

View File

@ -6,6 +6,7 @@ var $git = {};
native function getPath(); native function getPath();
native function getStatus(path); native function getStatus(path);
native function isIgnored(path); native function isIgnored(path);
native function checkoutHead(path);
function GitRepository(path) { function GitRepository(path) {
var repo = getRepository(path); var repo = getRepository(path);
@ -18,5 +19,6 @@ var $git = {};
GitRepository.prototype.getPath = getPath; GitRepository.prototype.getPath = getPath;
GitRepository.prototype.getStatus = getStatus; GitRepository.prototype.getStatus = getStatus;
GitRepository.prototype.isIgnored = isIgnored; GitRepository.prototype.isIgnored = isIgnored;
GitRepository.prototype.checkoutHead = checkoutHead;
this.GitRepository = GitRepository; this.GitRepository = GitRepository;
})(); })();

View File

@ -74,6 +74,31 @@ public:
} }
} }
CefRefPtr<CefV8Value> CheckoutHead(const char *path) {
if (!exists) {
return CefV8Value::CreateBool(false);
}
char *copiedPath = (char *)malloc(sizeof(char) * (strlen(path) + 1));
strcpy(copiedPath, path);
git_checkout_opts options;
memset(&options, 0, sizeof(options));
options.checkout_strategy = GIT_CHECKOUT_OVERWRITE_MODIFIED;
git_strarray paths;
paths.count = 1;
paths.strings = &copiedPath;
options.paths = paths;
int result = git_checkout_head(repo, &options);
free(copiedPath);
if (result == GIT_OK) {
return CefV8Value::CreateBool(true);
}
else {
return CefV8Value::CreateBool(false);
}
}
IMPLEMENT_REFCOUNTING(GitRepository); IMPLEMENT_REFCOUNTING(GitRepository);
}; };
@ -119,6 +144,12 @@ bool Git::Execute(const CefString& name,
return true; return true;
} }
if (name == "checkoutHead") {
GitRepository *userData = (GitRepository *)object->GetUserData().get();
retval = userData->CheckoutHead(arguments[0]->GetStringValue().ToString().c_str());
return true;
}
return false; return false;
} }

View File

@ -88,3 +88,36 @@ describe "Git", ->
it "returns false if the path isn't new", -> it "returns false if the path isn't new", ->
expect(repo.isPathNew(path)).toBeFalsy() expect(repo.isPathNew(path)).toBeFalsy()
describe ".checkoutHead(path)", ->
[repo, path1, path2, originalPath1Text, originalPath2Text] = []
beforeEach ->
repo = new Git(require.resolve('fixtures/git/working-dir'))
path1 = require.resolve('fixtures/git/working-dir/file.txt')
originalPath1Text = fs.read(path1)
path2 = require.resolve('fixtures/git/working-dir/other.txt')
originalPath2Text = fs.read(path2)
afterEach ->
fs.write(path1, originalPath1Text)
fs.write(path2, originalPath2Text)
it "no longer reports a path as modified after checkout", ->
expect(repo.isPathModified(path1)).toBeFalsy()
fs.write(path1, '')
expect(repo.isPathModified(path1)).toBeTruthy()
expect(repo.checkoutHead(path1)).toBeTruthy()
expect(repo.isPathModified(path1)).toBeFalsy()
it "restores the contents of the path to the original text", ->
fs.write(path1, '')
expect(repo.checkoutHead(path1)).toBeTruthy()
expect(fs.read(path1)).toBe(originalPath1Text)
it "only restores the path specified", ->
fs.write(path2, 'path 2 is edited')
expect(repo.isPathModified(path2)).toBeTruthy()
expect(repo.checkoutHead(path1)).toBeTruthy()
expect(fs.read(path2)).toBe('path 2 is edited')
expect(repo.isPathModified(path2)).toBeTruthy()

View File

@ -55,3 +55,6 @@ class Git
return head.substring(13) if head.indexOf('refs/remotes/') is 0 return head.substring(13) if head.indexOf('refs/remotes/') is 0
return head.substring(0, 7) if head.match(/[a-fA-F0-9]{40}/) return head.substring(0, 7) if head.match(/[a-fA-F0-9]{40}/)
return head return head
checkoutHead: (path) ->
@repo.checkoutHead(@relativize(path))