Merge pull request #8862 from phord/dot-git-files

🐛 Teach Atom to read .git files
This commit is contained in:
Max Brunsfeld
2015-10-01 09:32:49 -07:00
2 changed files with 32 additions and 3 deletions

View File

@@ -11,7 +11,7 @@ describe "GitRepositoryProvider", ->
it "returns a Promise that resolves to a GitRepository", ->
waitsForPromise ->
provider = new GitRepositoryProvider atom.project
directory = new Directory path.join(__dirname, 'fixtures/git/master.git')
directory = new Directory path.join(__dirname, 'fixtures', 'git', 'master.git')
provider.repositoryForDirectory(directory).then (result) ->
expect(result).toBeInstanceOf GitRepository
expect(provider.pathToRepository[result.getPath()]).toBeTruthy()
@@ -24,11 +24,11 @@ describe "GitRepositoryProvider", ->
secondRepo = null
waitsForPromise ->
directory = new Directory path.join(__dirname, 'fixtures/git/master.git')
directory = new Directory path.join(__dirname, 'fixtures', 'git', 'master.git')
provider.repositoryForDirectory(directory).then (result) -> firstRepo = result
waitsForPromise ->
directory = new Directory path.join(__dirname, 'fixtures/git/master.git/objects')
directory = new Directory path.join(__dirname, 'fixtures', 'git', 'master.git', 'objects')
provider.repositoryForDirectory(directory).then (result) -> secondRepo = result
runs ->
@@ -56,6 +56,21 @@ describe "GitRepositoryProvider", ->
provider.repositoryForDirectory(directory).then (result) ->
expect(result).toBe null
describe "when specified a Directory with a valid gitfile-linked repository", ->
it "returns a Promise that resolves to a GitRepository", ->
waitsForPromise ->
provider = new GitRepositoryProvider atom.project
gitDirPath = path.join(__dirname, 'fixtures', 'git', 'master.git')
workDirPath = temp.mkdirSync('git-workdir')
fs.writeFileSync(path.join(workDirPath, '.git'), 'gitdir: ' + gitDirPath+'\n')
directory = new Directory workDirPath
provider.repositoryForDirectory(directory).then (result) ->
expect(result).toBeInstanceOf GitRepository
expect(provider.pathToRepository[result.getPath()]).toBeTruthy()
expect(result.statusTask).toBeTruthy()
expect(result.getType()).toBe 'git'
describe "when specified a Directory without existsSync()", ->
directory = null
provider = null

View File

@@ -1,6 +1,17 @@
fs = require 'fs'
{Directory} = require 'pathwatcher'
GitRepository = require './git-repository'
# Returns the .gitdir path in the agnostic Git symlink .git file given, or
# null if the path is not a valid gitfile.
#
# * `gitFile` {String} path of gitfile to parse
gitFileRegex = RegExp "^gitdir: (.+)"
pathFromGitFile = (gitFile) ->
try
gitFileBuff = fs.readFileSync(gitFile, 'utf8')
return gitFileBuff?.match(gitFileRegex)[1]
# Checks whether a valid `.git` directory is contained within the given
# directory or one of its ancestors. If so, a Directory that corresponds to the
# `.git` folder will be returned. Otherwise, returns `null`.
@@ -11,6 +22,9 @@ findGitDirectorySync = (directory) ->
# can return cached values rather than always returning new objects:
# getParent(), getFile(), getSubdirectory().
gitDir = directory.getSubdirectory('.git')
gitDirPath = pathFromGitFile(gitDir.getPath?())
if gitDirPath
gitDir = new Directory(directory.resolve(gitDirPath))
if gitDir.existsSync?() and isValidGitDirectorySync gitDir
gitDir
else if directory.isRoot()