1
0
mirror of https://github.com/danog/gift.git synced 2024-11-30 04:19:37 +01:00

added Repo#identity and Repo#identify with a unit test

This commit is contained in:
Luke Plaster 2013-03-14 23:27:37 +00:00
parent 9d56103693
commit 2ece84faf6
2 changed files with 187 additions and 128 deletions

View File

@ -1,5 +1,6 @@
_ = require 'underscore'
cmd = require './git'
Actor = require './actor'
Commit = require './commit'
Tree = require './tree'
Diff = require './diff'
@ -15,30 +16,63 @@ module.exports = class Repo
else
@dot_git = "#{@path}/.git"
@git = cmd @path, @dot_git
# Public: Get the commit identity for this repository.
#
# callback - Receives `(err, actor)`, where `actor` is an Actor.
#
identity: (callback) ->
# git config user.email
@git "config", {}, ["user.email"]
, (err, stdout = '') =>
return callback err if err
email = stdout?.trim()
# git config user.name
@git "config", {}, ["user.name"]
, (err, stdout = '') =>
return callback err if err
name = stdout?.trim()
return callback null, new Actor(name, email)
# Public: Set your account's default identity for commits.
#
# actor - An instance of Actor.
# callback - Receives `(err)`.
#
identify: (actor, callback) ->
# git config user.email "you@example.com"
@git "config", {}, ["user.email", "\"#{actor.email}\""], (err) =>
return callback err if err
# git config user.name "Your Name"
@git "config", {}, ["user.name", "\"#{actor.name}\""], (err) =>
return callback err if err
return callback null
# Public: Get a list of commits.
#
#
# treeish - String (optional).
# limit - Integer (optional).
# skip - Integer (optional).
# callback - Function which receives `(err, commits)`, where `commits` is
# an Array of Commits.
#
#
# Examples
#
#
# # Get the 10 most recent commits to master.
# repo.commits (err, commits) ->
#
#
# # Or to a different tag or branch.
# repo.commits "v0.0.3", (err, commits) ->
#
#
# # Limit the maximum number of commits returned.
# repo.commits "master", 30, (err, commits) ->
#
#
# # Skip some (for pagination):
# repo.commits "master", 30, 30, (err, commits) ->
#
#
commits: (start, limit, skip, callback) ->
[skip, callback] = [callback, skip] if !callback
[limit, callback] = [callback, limit] if !callback
@ -47,26 +81,26 @@ module.exports = class Repo
start ?= "master"
limit ?= 10
skip ?= 0
Commit.find_all this, start, {"max-count": limit, skip}, callback
# Public: The tree object for the treeish or master.
#
#
# treeish - String treeish (such as a branch or tag) (optional).
#
#
# Returns Tree.
tree: (treeish="master") ->
return new Tree this, treeish
# Public: Get the difference between the trees.
#
#
# commitA - A Commit or String commit id.
# commitB - A Commit or String commit id.
# paths - A list of String paths to restrict the difference to (optional).
# callback - A Function which receives `(err, diffs)`.
#
#
diff: (commitA, commitB, paths, callback) ->
[callback, paths] = [paths, callback] if !callback
paths ?= []
@ -76,33 +110,33 @@ module.exports = class Repo
, (err, stdout, stderr) =>
return callback err if err
return callback err, Diff.parse(this, stdout)
# Public: Get the repository's remotes.
#
#
# callback - Receives `(err, remotes)`.
#
#
remotes: (callback) ->
Ref.find_all this, "remote", Ref, callback
# Public: List the repository's remotes.
#
#
# callback - Receives `(err, names)`.
#
#
remote_list: (callback) ->
@git.list_remotes callback
# Public: Add a remote.
#
#
# name - String name of the remote.
# url - String url of the remote.
# callback - Receives `(err)`
#
#
remote_add: (name, url, callback) ->
@git "remote", {}, ["add", name, url]
, (err, stdout, stderr) ->
callback err
# Public: Remove a remote.
#
# name - String name of the remote.
@ -112,17 +146,17 @@ module.exports = class Repo
@git "remote", {}, ["rm", name]
, (err, stdout, stderr) ->
callback err
# Public: `git fetch <name>`.
#
#
# name - String name of the remote
# callback - Receives `(err)`.
#
#
remote_fetch: (name, callback) ->
@git "fetch", {}, name
, (err, stdout, stderr) ->
callback err
# Public: `git push <name>`.
#
# name - String name of the remote
@ -132,82 +166,82 @@ module.exports = class Repo
@git "push", {}, name
, (err, stdout, stderr) ->
callback err
# Public: `git merge <name>`.
#
#
# name - String name of the source
# callback - Receives `(err)`.
#
#
merge: (name, callback) ->
@git "merge", {}, name
, (err, stdout, stderr) ->
callback err
# Public: Get the repository's status (`git status`).
#
#
# callback - Receives `(err, status)`
#
#
status: (callback) ->
return Status(this, callback)
# Public: Get the repository's tags.
#
#
# callback - Receives `(err, tags)`.
#
#
tags: (callback) ->
Tag.find_all this, callback
# Public: Create a tag.
#
#
# name - String
# options - An Object of command line arguments to pass to
# `git tag` (optional).
# callback - Receives `(err)`.
#
#
create_tag: (name, options, callback) ->
[options, callback] = [callback, options] if !callback
@git "tag", options, [name], callback
# Public: Delete the tag.
#
#
# name - String
# callback - Receives `(err)`.
#
#
delete_tag: (name, callback) ->
@git "tag", {d: name}, callback
# Public: Get a list of branches.
#
#
# callback - Receives `(err, heads)`.
#
#
branches: (callback) ->
Head.find_all this, callback
# Public: Create a branch with the given name.
#
#
# name - String name of the new branch.
# callback - Receives `(err)`.
#
#
create_branch: (name, callback) ->
@git "branch", {}, name, (err, stdout, stderr) ->
return callback err
# Public: Delete the branch with the given name.
#
#
# name - String name of the branch to delete.
# callback - Receives `(err)`.
#
#
delete_branch: (name, callback) ->
@git "branch", {d: true}, name, (err, stdout, stderr) ->
return callback err
# Public: Get the Branch with the given name.
#
#
# name - String (optional). By default, get the current branch.
# callback - Receives `(err, head)`
#
#
branch: (name, callback) ->
[name, callback] = [callback, name] if !callback
if !name
@ -218,51 +252,51 @@ module.exports = class Repo
for head in heads
return callback null, head if head.name == name
return callback new Error "No branch named '#{name}' found"
# Public: Checkout the treeish.
checkout: (treeish, callback) ->
@git "checkout", {}, treeish, callback
# Public: Commit some code.
#
#
# message - String
# options - Object (optional).
# "amend" - Boolean
# "all" - Boolean
# callback - Receives `(err)`.
#
#
commit: (message, options, callback) ->
[options, callback] = [callback, options] if !callback
options ?= {}
options = _.extend options, {m: "'#{message}'"}
@git "commit", options, (err, stdout, stderr) ->
callback err
# Public: Add files to the index.
#
#
# files - Array of String paths; or a String path.
# callback - Receives `(err)`.
#
#
add: (files, callback) ->
files = [files] if _.isString files
@git "add", {}, files, callback
# Public: Remove files from the index.
#
#
# files - Array of String paths; or a String path.
# callback - Receives `(err)`.
#
#
remove: (files, callback) ->
files = [files] if _.isString files
@git "rm", {}, files, callback
# Public: Revert the given commit.
revert: (sha, callback) ->
@git "revert", {}, sha, callback
# Public: Sync the current branch with the remote.
#
# callback - Receives `(err)`.

View File

@ -2,6 +2,7 @@ should = require 'should'
fs = require 'fs'
fixtures = require './fixtures'
git = require '../src'
Actor = require '../src/actor'
Commit = require '../src/commit'
Tree = require '../src/tree'
Diff = require '../src/diff'
@ -12,6 +13,30 @@ Status = require '../src/status'
{exec} = require 'child_process'
describe "Repo", ->
describe "#identify", ->
describe "when asked to set the identity's name and email", ->
repo = fixtures.branched
id = '' + new Date().getTime()
name = "name-#{id}"
email = "#{id}@domain"
ident = null
before (done) ->
actor = new Actor(name, email)
repo.identify actor, (err) ->
done err if err
repo.identity (err, _actor) ->
ident = _actor
done err
after (done) ->
exec "git checkout -- #{ repo.path }", done
it "has correctly set them", ->
ident.name.should.eql name
ident.email.should.eql email
describe "#commits", ->
describe "with only a callback", ->
repo = fixtures.branched
@ -20,10 +45,10 @@ describe "Repo", ->
repo.commits (err, _commits) ->
commits = _commits
done err
it "passes an Array", ->
commits.should.be.an.instanceof Array
it "is a list of commits", ->
commits[0].id.should.eql "913318e66e9beed3e89e9c402c1d6585ef3f7e6f"
commits[0].repo.should.eql repo
@ -33,7 +58,7 @@ describe "Repo", ->
commits[0].committed_date.should.be.an.instanceof Date
commits[0].parents().should.be.an.instanceof Array
commits[0].message.should.eql "add a sub dir"
describe "specify a branch", ->
repo = fixtures.branched
commits = null
@ -41,15 +66,15 @@ describe "Repo", ->
repo.commits "something", (err, _commits) ->
commits = _commits
done err
# The first commit ...
it "is the latest commit", ->
commits[0].message.should.eql "2"
it "has a parent commit", ->
commits[0].parents().should.have.lengthOf 1
commits[0].parents()[0].id.should.eql commits[1].id
describe "specify a tag", ->
repo = fixtures.tagged
commits = null
@ -57,10 +82,10 @@ describe "Repo", ->
repo.commits "tag-1", (err, _commits) ->
commits = _commits
done err
it "is the latest commit on the tag", ->
commits[0].message.should.include "commit 5"
describe "limit the number of commits", ->
repo = fixtures.tagged
commits = null
@ -68,10 +93,10 @@ describe "Repo", ->
repo.commits "master", 2, (err, _commits) ->
commits = _commits
done err
it "returns 2 commits", ->
commits.should.have.lengthOf 2
describe "skip commits", ->
repo = fixtures.tagged
commits = null
@ -79,35 +104,35 @@ describe "Repo", ->
repo.commits "master", 1, 2, (err, _commits) ->
commits = _commits
done err
it "returns 2 commits", ->
commits[0].message.should.include "commit 4"
describe "#tree", ->
repo = fixtures.branched
describe "master", ->
it "is a Tree", ->
repo.tree().should.be.an.instanceof Tree
it "checks out branch:master", (done) ->
repo.tree().blobs (err, blobs) ->
blobs[0].data (err, data) ->
data.should.include "Bla"
data.should.not.include "Bla2"
done err
describe "specific branch", ->
it "is a Tree", ->
repo.tree("something").should.be.an.instanceof Tree
it "checks out branch:something", (done) ->
repo.tree("something").blobs (err, blobs) ->
blobs[0].data (err, data) ->
data.should.include "Bla2"
done err
describe "#diff", ->
repo = fixtures.branched
describe "between 2 branches", ->
@ -116,22 +141,22 @@ describe "Repo", ->
repo.diff "something", "master", (err, _diffs) ->
diffs = _diffs
done err
it "is passes an Array of Diffs", ->
diffs.should.be.an.instanceof Array
diffs[0].should.be.an.instanceof Diff
# The first diff...
it "modifies the README.md file", ->
diffs[0].a_path.should.eql "README.md"
diffs[0].b_path.should.eql "README.md"
# The second diff...
it "creates some/hi.txt", ->
diffs[1].new_file.should.be.true
diffs[1].b_path.should.eql "some/hi.txt"
describe "#remotes", ->
describe "in a repository with remotes", ->
repo = fixtures.remotes
@ -140,24 +165,24 @@ describe "Repo", ->
repo.remotes (err, _remotes) ->
remotes = _remotes
done err
it "is an Array of Refs", ->
remotes.should.be.an.instanceof Array
remotes[0].should.be.an.instanceof Ref
it "contains the correct Refs", ->
remotes[0].commit.id.should.eql "bdd3996d38d885e18e5c5960df1c2c06e34d673f"
remotes[0].name.should.eql "origin/HEAD"
remotes[1].commit.id.should.eql "bdd3996d38d885e18e5c5960df1c2c06e34d673f"
remotes[1].name.should.eql "origin/master"
describe "when there are no remotes", ->
repo = fixtures.branched
it "is an empty Array", ->
repo.remotes (err, remotes) ->
remotes.should.eql []
describe "#remote_list", ->
describe "in a repository with remotes", ->
repo = fixtures.remotes
@ -166,18 +191,18 @@ describe "Repo", ->
repo.remote_list (err, _remotes) ->
remotes = _remotes
done err
it "is a list of remotes", ->
remotes.should.have.lengthOf 1
remotes[0].should.eql "origin"
describe "when there are no remotes", ->
repo = fixtures.branched
it "is an empty Array", ->
repo.remote_list (err, remotes) ->
remotes.should.eql []
describe "#tags", ->
describe "a repo with tags", ->
repo = fixtures.tagged
@ -186,21 +211,21 @@ describe "Repo", ->
repo.tags (err, _tags) ->
tags = _tags
done err
it "is an Array of Tags", ->
tags.should.be.an.instanceof Array
tags[0].should.be.an.instanceof Tag
it "is the correct tag", ->
tags[0].name.should.eql "tag-1"
describe "a repo without tags", ->
repo = fixtures.branched
it "is an empty array", (done) ->
repo.tags (err, tags) ->
tags.should.eql []
done err
describe "#create_tag", ->
repo = null
git_dir = __dirname + "/fixtures/junk_create_tag"
@ -220,7 +245,7 @@ describe "Repo", ->
it "creates a tag", (done) ->
repo.create_tag "foo", done
describe "#delete_tag", ->
describe "deleting a tag that does not exist", ->
repo = fixtures.branched
@ -228,8 +253,8 @@ describe "Repo", ->
repo.delete_tag "nonexistant-tag", (err) ->
should.exist err
done()
describe "#branches", ->
repo = fixtures.branched
branches = null
@ -237,16 +262,16 @@ describe "Repo", ->
repo.branches (err, _branches) ->
branches = _branches
done err
it "is an Array of Heads", ->
branches.should.be.an.instanceof Array
branches[0].should.be.an.instanceof Head
it "has the correct branches", ->
branches[0].name.should.eql "master"
branches[1].name.should.eql "something"
describe "#branch", ->
describe "when a branch name is given", ->
repo = fixtures.branched
@ -255,13 +280,13 @@ describe "Repo", ->
repo.branch "something", (err, b) ->
branch = b
done err
it "is a Head", ->
branch.should.be.an.instanceof Head
it "has the correct name", ->
branch.name.should.eql "something"
describe "when no branch name is given", ->
repo = fixtures.branched
branch = null
@ -269,10 +294,10 @@ describe "Repo", ->
repo.branch (err, b) ->
branch = b
done err
it "has the correct name", ->
branch.name.should.eql "master"
describe "an invalid branch", ->
repo = fixtures.branched
it "passes an error", (done) ->
@ -280,8 +305,8 @@ describe "Repo", ->
should.exist err
should.not.exist b
done()
describe "#delete_branch", ->
describe "a branch that does not exist", ->
repo = fixtures.branched
@ -289,4 +314,4 @@ describe "Repo", ->
repo.delete_branch "nonexistant-branch", (err) ->
should.exist err
done()