Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Add Repository.relativize(path)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsawicki committed Apr 9, 2013
1 parent f47baad commit 3f17fea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ Returns `true` if the path is a submodule, false otherwise.
Reread the index to update any values that have changed since the last time the
index was read.

### Repository.relativize(path)

Relativize the given path to the repository's working directory.

`path` - The string path to relativize.

Returns a repository-relative path if the given path is prefixed with the
repository's working directory path.

### Repository.release()

Release the repository and close all file handles it has open. No other methods
Expand Down
13 changes: 13 additions & 0 deletions spec/git-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,16 @@ describe "git", ->
expect(diffs[0].oldLines).toBe 1
expect(diffs[0].newStart).toBe 0
expect(diffs[0].newLines).toBe 0

describe '.relativize(path)', ->
it 'relativizes the given path to the working directory of the repository', ->
repo = git.open(__dirname)
workingDirectory = repo.getWorkingDirectory()

expect(repo.relativize(path.join(workingDirectory, 'a.txt'))).toBe 'a.txt'
expect(repo.relativize(path.join(workingDirectory, 'a/b/c.txt'))).toBe 'a/b/c.txt'
expect(repo.relativize('a.txt')).toBe 'a.txt'
expect(repo.relativize('/not/in/working/dir')).toBe '/not/in/working/dir'
expect(repo.relativize(null)).toBe null
expect(repo.relativize()).toBeUndefined()
expect(repo.relativize('')).toBe ''
10 changes: 10 additions & 0 deletions src/git.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ Repository.prototype.getAheadBehindCount = ->
counts.behind = @getCommitCount(upstreamCommit, mergeBase)
counts

Repository.prototype.relativize = (path) ->
return path unless path?
return path unless path[0] is '/'

workingDirectory = @getWorkingDirectory()
if workingDirectory and path.indexOf("#{workingDirectory}/") is 0
path.substring(workingDirectory.length + 1)
else
path

exports.open = (path) ->
repository = new Repository(path)
if repository.exists()
Expand Down

0 comments on commit 3f17fea

Please sign in to comment.