From 4add7b62136131852799fca79c365338ac81021b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 27 Feb 2013 13:07:57 -0800 Subject: [PATCH] Support getting status of entire repository New Git.getAllStatuses() method returns all non-ignored status entries in the repository. --- native/v8_extensions/git.mm | 27 ++++++++++++++++++++++++++- spec/app/git-spec.coffee | 22 ++++++++++++++++++++++ src/app/git.coffee | 3 +++ src/stdlib/git-repository.coffee | 1 + 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/native/v8_extensions/git.mm b/native/v8_extensions/git.mm index 4c7bbb056..3a8c13885 100644 --- a/native/v8_extensions/git.mm +++ b/native/v8_extensions/git.mm @@ -8,6 +8,14 @@ namespace v8_extensions { private: git_repository *repo; + static int CollectStatus(const char *path, unsigned int status, void *payload) { + if ((status & GIT_STATUS_IGNORED) == 0) { + std::map *statuses = (std::map *) payload; + statuses->insert(std::pair(path, status)); + } + return 0; + } + public: GitRepository(const char *pathInRepo) { if (git_repository_open_ext(&repo, pathInRepo, 0, NULL) != GIT_OK) { @@ -55,6 +63,17 @@ namespace v8_extensions { return CefV8Value::CreateNull(); } + CefRefPtr GetStatuses() { + std::map statuses; + git_status_foreach(repo, CollectStatus, &statuses); + std::map::iterator iter = statuses.begin(); + CefRefPtr v8Statuses = CefV8Value::CreateObject(NULL); + for (; iter != statuses.end(); ++iter) { + v8Statuses->SetValue(iter->first, CefV8Value::CreateInt(iter->second), V8_PROPERTY_ATTRIBUTE_NONE); + } + return v8Statuses; + } + CefRefPtr IsIgnored(const char *path) { int ignored; if (git_ignore_path_is_ignored(&ignored, repo, path) == GIT_OK) { @@ -190,7 +209,7 @@ namespace v8_extensions { void Git::CreateContextBinding(CefRefPtr context) { const char* methodNames[] = { "getRepository", "getHead", "getPath", "isIgnored", "getStatus", "checkoutHead", - "getDiffStats", "isSubmodule", "refreshIndex", "destroy" + "getDiffStats", "isSubmodule", "refreshIndex", "destroy", "getStatuses" }; CefRefPtr nativeObject = CefV8Value::CreateObject(NULL); @@ -277,6 +296,12 @@ namespace v8_extensions { return true; } + if (name == "getStatuses") { + GitRepository *userData = (GitRepository *)object->GetUserData().get(); + retval = userData->GetStatuses(); + return true; + } + return false; } } diff --git a/spec/app/git-spec.coffee b/spec/app/git-spec.coffee index 522d38d8e..892d3cbcd 100644 --- a/spec/app/git-spec.coffee +++ b/spec/app/git-spec.coffee @@ -147,3 +147,25 @@ describe "Git", -> expect(repo.getDiffStats(path)).toEqual {added: 0, deleted: 0} fs.write(path, "#{originalPathText} edited line") expect(repo.getDiffStats(path)).toEqual {added: 1, deleted: 1} + + describe ".getStatuses()", -> + [newPath, modifiedPath, cleanPath, originalModifiedPathText] = [] + + beforeEach -> + repo = new Git(require.resolve('fixtures/git/working-dir')) + modifiedPath = fixturesProject.resolve('git/working-dir/file.txt') + originalModifiedPathText = fs.read(modifiedPath) + newPath = fixturesProject.resolve('git/working-dir/untracked.txt') + cleanPath = fixturesProject.resolve('git/working-dir/other.txt') + fs.write(newPath, '') + + afterEach -> + fs.write(modifiedPath, originalModifiedPathText) + fs.remove(newPath) if fs.exists(newPath) + + it "returns status information for all new and modified files", -> + fs.write(modifiedPath, 'making this path modified') + statuses = repo.getAllStatuses() + expect(statuses[cleanPath]).toBeUndefined() + expect(repo.isStatusNew(statuses[repo.relativize(newPath)])).toBeTruthy() + expect(repo.isStatusModified(statuses[repo.relativize(modifiedPath)])).toBeTruthy() diff --git a/src/app/git.coffee b/src/app/git.coffee index 1c9fffc0a..292d4fc69 100644 --- a/src/app/git.coffee +++ b/src/app/git.coffee @@ -54,6 +54,9 @@ class Git getPathStatus: (path) -> pathStatus = @getRepo().getStatus(@relativize(path)) + getAllStatuses: (path) -> + @getRepo().getStatuses() + isPathIgnored: (path) -> @getRepo().isIgnored(@relativize(path)) diff --git a/src/stdlib/git-repository.coffee b/src/stdlib/git-repository.coffee index 7a52403b4..2386ba868 100644 --- a/src/stdlib/git-repository.coffee +++ b/src/stdlib/git-repository.coffee @@ -10,6 +10,7 @@ class GitRepository getHead: $git.getHead getPath: $git.getPath getStatus: $git.getStatus + getStatuses: $git.getStatuses isIgnored: $git.isIgnored checkoutHead: $git.checkoutHead getDiffStats: $git.getDiffStats