From 62310e6f41b65bc3b91be05f3bab4f350527d63f Mon Sep 17 00:00:00 2001 From: Daniel Hengeveld Date: Thu, 15 Oct 2015 18:10:32 +0200 Subject: [PATCH] isPathModified --- spec/git-repository-async-spec.coffee | 56 ++++++++++++++++++++------- src/git-repository-async.js | 16 ++++++++ 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/spec/git-repository-async-spec.coffee b/spec/git-repository-async-spec.coffee index b80b2940f..180988d83 100644 --- a/spec/git-repository-async-spec.coffee +++ b/spec/git-repository-async-spec.coffee @@ -1,15 +1,19 @@ temp = require 'temp' GitRepositoryAsync = require '../src/git-repository-async' fs = require 'fs-plus' +os = require 'os' path = require 'path' Task = require '../src/task' Project = require '../src/project' +# Clean up when the process exits +temp.track() + copyRepository = -> workingDirPath = temp.mkdirSync('atom-working-dir') fs.copySync(path.join(__dirname, 'fixtures', 'git', 'working-dir'), workingDirPath) fs.renameSync(path.join(workingDirPath, 'git.git'), path.join(workingDirPath, '.git')) - workingDirPath + fs.realpathSync(workingDirPath) openFixture = (fixture)-> GitRepositoryAsync.open(path.join(__dirname, 'fixtures', 'git', fixture)) @@ -82,29 +86,55 @@ fdescribe "GitRepositoryAsync", -> expect(onSuccess.mostRecentCall.args[0]).toBeFalsy() - xdescribe ".isPathModified(path)", -> - [repo, filePath, newPath] = [] + describe ".isPathModified(path)", -> + [repo, filePath, newPath, emptyPath] = [] beforeEach -> workingDirPath = copyRepository() - repo = new GitRepository(workingDirPath) + repo = new GitRepositoryAsync.open(workingDirPath) filePath = path.join(workingDirPath, 'a.txt') newPath = path.join(workingDirPath, 'new-path.txt') + fs.writeFileSync(newPath, "i'm new here") + emptyPath = path.join(workingDirPath, 'empty-path.txt') - xdescribe "when the path is unstaged", -> - it "returns false if the path has not been modified", -> - expect(repo.isPathModified(filePath)).toBeFalsy() + describe "when the path is unstaged", -> + it "resolves false if the path has not been modified", -> + onSuccess = jasmine.createSpy('onSuccess') + waitsForPromise -> + repo.isPathModified(filePath).then(onSuccess) + runs -> + expect(onSuccess.mostRecentCall.args[0]).toBeFalsy() - it "returns true if the path is modified", -> + it "resolves true if the path is modified", -> fs.writeFileSync(filePath, "change") - expect(repo.isPathModified(filePath)).toBeTruthy() + onSuccess = jasmine.createSpy('onSuccess') + waitsForPromise -> + repo.isPathModified(filePath).then(onSuccess) + runs -> + expect(onSuccess.mostRecentCall.args[0]).toBeTruthy() - it "returns true if the path is deleted", -> + + it "resolves true if the path is deleted", -> fs.removeSync(filePath) - expect(repo.isPathModified(filePath)).toBeTruthy() + onSuccess = jasmine.createSpy('onSuccess') + waitsForPromise -> + repo.isPathModified(filePath).then(onSuccess) + runs -> + expect(onSuccess.mostRecentCall.args[0]).toBeTruthy() - it "returns false if the path is new", -> - expect(repo.isPathModified(newPath)).toBeFalsy() + it "resolves false if the path is new", -> + onSuccess = jasmine.createSpy('onSuccess') + waitsForPromise -> + repo.isPathModified(newPath).then(onSuccess) + runs -> + expect(onSuccess.mostRecentCall.args[0]).toBeFalsy() + + it "resolves false if the path is invalid", -> + onSuccess = jasmine.createSpy('onSuccess') + waitsForPromise -> + repo.isPathModified(emptyPath).then(onSuccess) + runs -> + expect(onSuccess.mostRecentCall.args[0]).toBeFalsy() xdescribe ".isPathNew(path)", -> [filePath, newPath] = [] diff --git a/src/git-repository-async.js b/src/git-repository-async.js index e6d5a8f5d..445e88ccb 100644 --- a/src/git-repository-async.js +++ b/src/git-repository-async.js @@ -1,6 +1,7 @@ "use babel"; const Git = require('nodegit') +const path = require('path') module.exports = class GitRepositoryAsync { static open(path) { @@ -35,4 +36,19 @@ module.exports = class GitRepositoryAsync { return Promise.resolve(Git.Ignore.pathIsIgnored(repo, _path)) }) } + + isPathModified(_path) { + // Surely I'm missing a built-in way to do this + var basePath = null + return this.repoPromise.then( (repo) => { + basePath = repo.workdir() + return repo.getStatus() + }).then( (statuses) => { + console.log(statuses.map(function(x){return x.path()})); + ret = statuses.filter((status)=> { + return _path == path.join(basePath, status.path()) && (status.isModified() || status.isDeleted()) + }).length > 0 + return Promise.resolve(ret) + }) + } }