Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repo.checkoutFile and repo.reset #31

Merged
merged 20 commits into from
Jul 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ node_js:
- "0.8"
- "0.10"
- "0.11"

install:
- "npm install -g npm"
- "npm install"
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ Commit some changes.
### `Repo#checkout(treeish, callback)`
`git checkout <treeish>`

### `Repo#checkoutFile([files, options, ]callback)`
Checkout some files.

* `files` - File(s) to checkout. Pass `'.'` or nothing to checkout all files.
* `options` -
- `force` - `Boolean`
* `callback` - Receives `(err)`.

### `Repo#sync([[remote, ]branch, ]callback)`
Sync the current branch with the remote, keeping all local changes intact.

Expand All @@ -194,6 +202,17 @@ The following steps are carried out: `stash`, `pull`, `push`, `stash pop`. If th
* `branch` - `String` (defaults to `master`).
* `callback` - Receives `(err)`.

### `Repo#reset([treeish, options, ]callback)`
Checkout files.

* `treeish` - The git object to reset to. Defaults to HEAD.
* `options` -
- `soft` - `Boolean`
- `mixed` - `Boolean` __default__
- `hard` - `Boolean`
- `merge` - `Boolean`
- `keep` - `Boolean`
* `callback` - Receives `(err)`.

## Commit
### `Commit#id`
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"main": "./lib/index",
"scripts": {
"test": "mocha --compilers coffee:'./node_modules/coffee-script/lib/coffee-script/coffee-script'",
"test": "mocha --compilers coffee:'./node_modules/coffee-script/lib/coffee-script/register'",
"prepublish": "coffee -o lib -c src"
},
"repository": {
Expand All @@ -28,11 +28,11 @@
"underscore": "1.x.x"
},
"devDependencies": {
"should": "~2.0.1",
"mocha": "1.x.x",
"should": "~4.0.4",
"mocha": "~1.20.1",
"sinon": "~1.7.3",
"coffee-script": "1.6.x",
"rimraf": "2.0.x"
"coffee-script": "~1.7.1",
"fs-extra": "~0.9.1"
},
"engines": {
"node": "> 0.4.1"
Expand Down
6 changes: 3 additions & 3 deletions src/actor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module.exports = class Actor
constructor: (@name, @email) ->
if @email
@hash = crypto.createHash("md5").update(@email, "ascii").digest("hex")

# Public: Get a string representation of the Actor.
toString: ->
"#{@name} <#{@email}>"

# Public: Parse an Actor from a "bla <[email protected]>" string.
#
#
# Returns Actor.
@from_string: (str) ->
if /<.+>/.test str
Expand Down
10 changes: 5 additions & 5 deletions src/blob.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ path = require 'path'
module.exports = class Blob
constructor: (@repo, attrs) ->
{@id, @name, @mode} = attrs

# Public: Get the blob contents.
#
#
# callback - Receives `(err, data)`.
#
# Warning, this only returns files less than 200k, the standard buffer size for
#
# Warning, this only returns files less than 200k, the standard buffer size for
# node's exec(). If you need to get bigger files, you should use dataStream() to
# get a stream for the file's data
#
data: (callback) ->
@repo.git "cat-file", {p: true}, @id
, (err, stdout, stderr) ->
return callback err, stdout

# Public: Get the blob contents as a stream
#
# returns - [dataStream, errstream]
Expand Down
52 changes: 26 additions & 26 deletions src/commit.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ Tree = require './tree'
module.exports = class Commit
constructor: (@repo, @id, parents, tree, @author, @authored_date, @committer, @committed_date, @gpgsig, @message) ->
# Public: Get the commit's Tree.
#
#
# Returns Tree.
@tree = _.memoize => (new Tree @repo, tree)

# Public: Get the Commit's parent Commits.
#
#
# Returns an Array of Commits.
@parents = _.memoize =>
_.map parents, (parent) =>
new Commit @repo, parent


toJSON: ->
{@id, @author, @authored_date, @committer, @committed_date, @message}


# Public: Find the matching commits.
#
#
# callback - Receives `(err, commits)`
#
#
@find_all: (repo, ref, options, callback) ->
options = _.extend {pretty: "raw"}, options
repo.git "rev-list", options, ref
, (err, stdout, stderr) =>
return callback err if err
return callback null, @parse_commits(repo, stdout)


@find: (repo, id, callback) ->
options = {pretty: "raw", "max-count": 1}
repo.git "rev-list", options, id
, (err, stdout, stderr) =>
return callback err if err
return callback null, @parse_commits(repo, stdout)[0]


@find_commits: (repo, ids, callback) ->
commits = []
next = (i) ->
Expand All @@ -53,10 +53,10 @@ module.exports = class Commit
else
callback null, commits
next 0


# Internal: Parse the commits from `git rev-list`
#
#
# Return Commit[]
@parse_commits: (repo, text) ->
commits = []
Expand All @@ -65,17 +65,17 @@ module.exports = class Commit
id = _.last lines.shift().split(" ")
break if !id
tree = _.last lines.shift().split(" ")

parents = []
while /^parent/.test lines[0]
parents.push _.last lines.shift().split(" ")

author_line = lines.shift()
[author, authored_date] = @actor author_line

committer_line = lines.shift()
[committer, committed_date] = @actor committer_line

gpgsig = []
if /^gpgsig/.test lines[0]
gpgsig.push lines.shift().replace /^gpgsig /, ''
Expand All @@ -86,22 +86,22 @@ module.exports = class Commit
# not doing anything with this yet, but it's sometimes there
if /^encoding/.test lines[0]
encoding = _.last lines.shift().split(" ")

lines.shift()

message_lines = []
while /^ {4}/.test lines[0]
message_lines.push lines.shift()[4..-1]

while lines[0]? && !lines[0].length
lines.shift()

commits.push new Commit(repo, id, parents, tree, author, authored_date, committer, committed_date, gpgsig.join("\n"), message_lines.join("\n"))
return commits


# Internal: Parse the actor.
#
#
# Returns [String name and email, Date]
@actor: (line) ->
[m, actor, epoch] = /^.+? (.*) (\d+) .*$/.exec line
Expand Down
26 changes: 13 additions & 13 deletions src/diff.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ module.exports = class Diff
if b_blob isnt null
@b_blob = new Blob @repo, {id: b_blob}
@b_sha = b_blob

toJSON: ->
{@a_path, @b_path, @a_mode, @b_mode, @new_file
, @deleted_file, @diff, @renamed_file, @similarity_index}

# Public: Parse the Diffs from the command output.
#
#
# text - String stdout of a `git diff` command.
#
#
# Returns Array of Diff.
@parse: (repo, text) ->
lines = text.split "\n"
diffs = []

while lines.length && lines[0]
# FIXME shift is O(n), so iterating n over O(n) operation might be O(n^2)
[m, a_path, b_path] = ///^diff\s--git\s"?a/(.+?)"?\s"?b/(.+)"?$///.exec lines.shift()

if /^old mode/.test lines[0]
[m, a_mode] = /^old mode (\d+)/.exec lines.shift()
[m, b_mode] = /^new mode (\d+)/.exec lines.shift()

if !lines.length || /^diff --git/.test(lines[0])
diffs.push new Diff(repo, a_path, b_path, null, null, a_mode, b_mode, false, false, null)
continue

sim_index = 0
new_file = false
deleted_file = false
renamed_file = false

if /^new file/.test lines[0]
[m, b_mode] = /^new file mode (.+)$/.exec lines.shift()
a_mode = null
Expand All @@ -56,17 +56,17 @@ module.exports = class Diff
# shift away the 2 `rename from/to ...` lines
lines.shift()
lines.shift()

[m, a_blob, b_blob, b_mode] = ///^index\s([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s?(.+)?$///.exec lines.shift()
b_mode = b_mode.trim() if b_mode

diff_lines = []
while lines[0] && !/^diff/.test(lines[0])
diff_lines.push lines.shift()
diff = diff_lines.join "\n"

diffs.push new Diff(repo, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff, renamed_file, sim_index)

return diffs

# Public: Parse the raw diff format from the command output.
Expand Down
10 changes: 5 additions & 5 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
Repo = require './repo'

# Public: Create a Repo from the given path.
#
#
# Returns Repo.
module.exports = Git = (path, bare=false) ->
return new Repo path, bare


# Public: Initialize a git repository.
#
#
# path - The directory to run `git init .` in.
# bare - Create a bare repository when true.
# callback - Receives `(err, repo)`.
#
#
Git.init = (path, bare, callback) ->
[bare, callback] = [callback, bare] if !callback
if bare
Expand All @@ -26,11 +26,11 @@ Git.init = (path, bare, callback) ->
return callback err, (new Repo path, bare)

# Public: Clone a git repository.
#
#
# repository - The repository to clone from.
# path - The directory to clone into.
# callback - Receives `(err, repo)`.
#
#
Git.clone = (repository, path, callback) ->
bash = "git clone #{repository} #{path}"
exec bash, (err, stdout, stderr) ->
Expand Down
12 changes: 6 additions & 6 deletions src/ref.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Commit = require './commit'
exports.Ref = class Ref
constructor: (@name, @commit) ->
{@repo} = @commit

# Public: Get a String representation of the Ref.
toString: ->
"#<Ref '#{@name}'>"

# Internal: Find all refs.
#
#
# options - (optional).
#
#
# Returns Array of Ref.
@find_all: (repo, type, RefClass, callback) ->
repo.git.refs type, {}, (err, text) ->
Expand All @@ -24,7 +24,7 @@ exports.Ref = class Ref
[name, id] = ref.split(' ')
names.push name
ids.push id

Commit.find_commits repo, ids, (err, commits) ->
return callback err if err
refs = []
Expand All @@ -36,7 +36,7 @@ exports.Ref = class Ref
exports.Head = class Head extends Ref
@find_all: (repo, callback) ->
Ref.find_all repo, "head", Head, callback

@current: (repo, callback) ->
fs.readFile "#{repo.dot_git}/HEAD", (err, data) ->
return callback err if err
Expand Down
Loading