Add Git.isPathNew(path)

Renamed other method to Git.isPathModified
This commit is contained in:
Corey Johnson & Kevin Sawicki
2012-11-02 14:54:12 -07:00
committed by Corey Johnson
parent c9f1064d6b
commit be533d4342
6 changed files with 49 additions and 32 deletions

View File

@@ -62,15 +62,15 @@ public:
CefRefPtr<CefV8Value> GetStatus(const char *path) {
if (!exists) {
return CefV8Value::CreateInt(-1);
return CefV8Value::CreateInt(0);
}
unsigned int status;
unsigned int status = 0;
if (git_status_file(&status, repo, path) == GIT_OK) {
return CefV8Value::CreateInt(status);
}
else {
return CefV8Value::CreateInt(-1);
return CefV8Value::CreateInt(0);
}
}

View File

@@ -33,24 +33,23 @@ describe "Git", ->
repo = new Git(require.resolve('fixtures/git/master.git'))
expect(repo.getShortHead()).toBe 'master'
describe ".isIgnored(path)", ->
describe ".isPathIgnored(path)", ->
it "returns true for an ignored path", ->
repo = new Git(require.resolve('fixtures/git/ignore.git'))
expect(repo.isIgnored('a.txt')).toBeTruthy()
expect(repo.isPathIgnored('a.txt')).toBeTruthy()
it "returns false for a non-ignored path", ->
repo = new Git(require.resolve('fixtures/git/ignore.git'))
expect(repo.isIgnored('b.txt')).toBeFalsy()
expect(repo.isPathIgnored('b.txt')).toBeFalsy()
describe ".isModified(path)", ->
[repo, path, originalPathText, newPath] = []
describe ".isPathModified(path)", ->
[repo, path, newPath, originalPathText] = []
beforeEach ->
repo = new Git(require.resolve('fixtures/git/working-dir'))
path = require.resolve('fixtures/git/working-dir/file.txt')
originalPathText = fs.read(path)
newPath = fs.join(require.resolve('fixtures/git/working-dir'), 'new-path.txt')
fs.write(newPath, "i'm new here")
originalPathText = fs.read(path)
afterEach ->
fs.write(path, originalPathText)
@@ -58,15 +57,34 @@ describe "Git", ->
describe "when the path is unstaged", ->
it "returns false if the path has not been modified", ->
expect(repo.isModified(path)).toBeFalsy()
it "returns true if the path is new", ->
expect(repo.isModified(newPath)).toBeTruthy()
expect(repo.isPathModified(path)).toBeFalsy()
it "returns true if the path is modified", ->
fs.write(path, "change")
expect(repo.isModified(path)).toBeTruthy()
expect(repo.isPathModified(path)).toBeTruthy()
it "returns true if the path is deleted", ->
fs.remove(path)
expect(repo.isModified(path)).toBeTruthy()
expect(repo.isPathModified(path)).toBeTruthy()
it "returns false if the path is new", ->
expect(repo.isPathModified(newPath)).toBeFalsy()
describe ".isPathNew(path)", ->
[repo, path, newPath] = []
beforeEach ->
repo = new Git(require.resolve('fixtures/git/working-dir'))
path = require.resolve('fixtures/git/working-dir/file.txt')
newPath = fs.join(require.resolve('fixtures/git/working-dir'), 'new-path.txt')
fs.write(newPath, "i'm new here")
afterEach ->
fs.remove(newPath) if fs.exists(newPath)
describe "when the path is unstaged", ->
it "returns true if the path is new", ->
expect(repo.isPathNew(newPath)).toBeTruthy()
it "returns false if the path isn't new", ->
expect(repo.isPathNew(path)).toBeFalsy()

View File

@@ -1,11 +1,6 @@
module.exports =
class Git
@isPathIgnored: (path) ->
return false unless path
repo = new Git(path)
repo.isIgnored(repo.relativize(path))
statusFlags:
index_new: 1 << 0
index_modified: 1 << 1
@@ -31,23 +26,27 @@ class Git
getHead: ->
@repo.getHead() or ''
isIgnored: (path) ->
path and @repo.isIgnored(path)
getPathStatus: (path) ->
pathStatus = @repo.getStatus(@relativize(path))
isModified: (path) ->
statusFlags = @repo.getStatus(@relativize(path))
modifiedFlags = @statusFlags.working_dir_new |
@statusFlags.working_dir_modified |
isPathIgnored: (path) ->
@repo.isIgnored(@relativize(path))
isPathModified: (path) ->
modifiedFlags = @statusFlags.working_dir_modified |
@statusFlags.working_dir_delete |
@statusFlags.working_dir_typechange
(@getPathStatus(path) & modifiedFlags) > 0
(statusFlags & modifiedFlags) > 0
isPathNew: (path) ->
(@getPathStatus(path) & @statusFlags.working_dir_new) > 0
relativize: (path) ->
return path unless path
workingDirectory = @getWorkingDirectory()
if workingDirectory and path.indexOf(workingDirectory) is 0
path.substring(workingDirectory.length)
else
path
getShortHead: ->
head = @getHead()

View File

@@ -72,7 +72,7 @@ class StatusBar extends View
updateStatusText: ->
if path = @editor.getPath()
modified = new Git(path).isModified(path)
modified = new Git(path).isPathModified(path)
if modified
@gitStatusIcon.show()

View File

@@ -19,7 +19,7 @@ class DirectoryView extends View
initialize: ({@directory, isExpanded} = {}) ->
@expand() if isExpanded
@disclosureArrow.on 'click', => @toggleExpansion()
@directoryName.addClass('ignored') if Git.isPathIgnored(@directory.getPath())
@directoryName.addClass('ignored') if new Git(@directory.getPath()).isPathIgnored(@directory.getPath())
getPath: ->
@directory.path

View File

@@ -10,7 +10,7 @@ class FileView extends View
file: null
initialize: (@file) ->
@addClass('ignored') if Git.isPathIgnored(@file.getPath())
@addClass('ignored') if new Git(@file.getPath()).isPathIgnored(@file.getPath())
getPath: ->
@file.path