From 1019154db66bda5d9cb8ce697226f63195005a96 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Sat, 3 Nov 2012 15:09:43 -0700 Subject: [PATCH] Add Git.checkoutHead(path) --- native/v8_extensions/git.js | 2 ++ native/v8_extensions/git.mm | 31 +++++++++++++++++++++++++++++++ spec/app/git-spec.coffee | 33 +++++++++++++++++++++++++++++++++ src/app/git.coffee | 3 +++ 4 files changed, 69 insertions(+) diff --git a/native/v8_extensions/git.js b/native/v8_extensions/git.js index 71ae3f038..b240204ff 100644 --- a/native/v8_extensions/git.js +++ b/native/v8_extensions/git.js @@ -6,6 +6,7 @@ var $git = {}; native function getPath(); native function getStatus(path); native function isIgnored(path); + native function checkoutHead(path); function GitRepository(path) { var repo = getRepository(path); @@ -18,5 +19,6 @@ var $git = {}; GitRepository.prototype.getPath = getPath; GitRepository.prototype.getStatus = getStatus; GitRepository.prototype.isIgnored = isIgnored; + GitRepository.prototype.checkoutHead = checkoutHead; this.GitRepository = GitRepository; })(); diff --git a/native/v8_extensions/git.mm b/native/v8_extensions/git.mm index ab8f82b71..3f677454b 100644 --- a/native/v8_extensions/git.mm +++ b/native/v8_extensions/git.mm @@ -74,6 +74,31 @@ public: } } + CefRefPtr 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); }; @@ -119,6 +144,12 @@ bool Git::Execute(const CefString& name, return true; } + if (name == "checkoutHead") { + GitRepository *userData = (GitRepository *)object->GetUserData().get(); + retval = userData->CheckoutHead(arguments[0]->GetStringValue().ToString().c_str()); + return true; + } + return false; } diff --git a/spec/app/git-spec.coffee b/spec/app/git-spec.coffee index 0eb1f7fb2..455c6391e 100644 --- a/spec/app/git-spec.coffee +++ b/spec/app/git-spec.coffee @@ -88,3 +88,36 @@ describe "Git", -> it "returns false if the path isn't new", -> 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() diff --git a/src/app/git.coffee b/src/app/git.coffee index 41f3dd7db..6d29f1b53 100644 --- a/src/app/git.coffee +++ b/src/app/git.coffee @@ -55,3 +55,6 @@ class Git 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 + + checkoutHead: (path) -> + @repo.checkoutHead(@relativize(path))