Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

removed ability to send path as arg to gitr.

  • Loading branch information...
commit 276a52206dbdf2b1a11e554ab86179f50c8d0568 1 parent 53f94bb
@kenglxn authored
View
21 lib/gitr.coffee
@@ -1,11 +1,9 @@
fs = require 'fs', _ = require 'underscore', cp = require 'child_process', color = require './color', q = require './q'
class GitR
-
- checkDirArg = (dirArg) -> if dirArg?.length then "#{dirArg}".replace /\/$/, '' else "#{process.cwd()}"
log = (m...) => console.log m...
- findRepos = (dir) =>
+ findRepos = (dir = process.cwd()) =>
dirs = []
if fs.statSync(dir).isDirectory()
subDirs = fs.readdirSync(dir)
@@ -16,8 +14,8 @@ class GitR
dirs.push findRepos dir + '/' + subDir
_.flatten dirs
- exec = (gitCmd, repo, cb) =>
- cp.exec gitCmd, (err, stdout, stderr) ->
+ exec = (cmd, repo, cb) =>
+ cp.exec "git --git-dir=#{repo}/.git --work-tree=#{repo} #{cmd.join(' ')}", (err, stdout, stderr) ->
msg = ''
msg += "#{stdout}\n#{color.cls}" if stdout?.length > 0
msg += "#{color.red}#{err}#{color.cls}" if err?.length > 0
@@ -25,16 +23,11 @@ class GitR
log "#{color.yellow}::#{repo}::#{color.green}\n#{msg}" if msg?.length > 0
cb()
- ls: (dir) => _.each findRepos(checkDirArg(dir)), (repo) => log repo
-
- do: (cmd = '', path) =>
+ do: (cmd...) =>
fns = []
- dir = checkDirArg(path)
- repos = findRepos(dir)
- _.each repos, (repo) =>
- fns.push (cb) =>
- exec "git --git-dir=#{repo}/.git --work-tree=#{repo} #{cmd}", repo, cb
+ repos = findRepos()
+ _.each repos, (repo) => fns.push (cb) => exec cmd, repo, cb
q.dequeue fns, =>
- log "#{color.red}no git repos under #{color.yellow}#{dir}#{color.cls}" if repos.length == 0
+ log "#{color.red}no git repos under #{color.yellow}#{process.cwd()}#{color.cls}" if repos.length == 0
exports = module.exports = GitR
View
3  lib/runr.coffee
@@ -1,5 +1,4 @@
argv = process.argv.slice(2)
GitR = require './gitr'
-gitr = new GitR()
-gitr.do argv...
+new GitR().do argv...
View
2  package.json
@@ -1,6 +1,6 @@
{
"name":"gitr",
- "version":"0.0.2",
+ "version":"0.0.3",
"description":"perform git commands recursively",
"keywords": [ "git", "recursive", "command", "line", "tool" ],
"main":"./lib/gitr.js",
View
71 spec/gitr.spec.coffee
@@ -15,80 +15,39 @@ describe 'gitr', ->
'testDir/withGitRepoAtSecondLevel/secondLevel',
'testDir/withGitRepoAtSecondLevel/secondLevel/.git',
]
+ origDir = process.cwd()
+ testDir = 'testDir'
beforeEach ->
for directory in directoryStructure
unless fs.existsSync(directory)
fs.mkdirSync(directory)
fs.writeFileSync(directory + '/tmpFile', 'stuff') if directory.search('.git$') == -1
+ process.chdir(testDir)
afterEach ->
- rimraf.sync 'testDir'
+ process.chdir(origDir)
+ rimraf.sync testDir
- it 'should be defined', ->
- expect(GitR).toBeDefined()
-
- it 'should list all git enabled directories under given directory', ->
- spyOn(console, 'log')
- gitr.ls('testDir')
-
- expect(console.log).toHaveBeenCalled()
- expect(console.log.callCount).toBe(2)
- expect(console.log.calls[0].args[0]).toBe('testDir/withGitRepo')
- expect(console.log.calls[1].args[0]).toBe('testDir/withGitRepoAtSecondLevel/secondLevel')
-
- it 'should trim trailing slash list all git enabled directories under given directory', ->
- spyOn(console, 'log')
- gitr.ls('testDir')
-
- expect(console.log).toHaveBeenCalled()
- expect(console.log.callCount).toBe(2)
- expect(console.log.calls[0].args[0]).toBe('testDir/withGitRepo')
- expect(console.log.calls[1].args[0]).toBe('testDir/withGitRepoAtSecondLevel/secondLevel')
-
- it 'should ls from cwd if no argument is passed', ->
- spyOn(console, 'log')
- gitr.ls()
- expect(console.log).toHaveBeenCalled()
- expect(console.log.callCount).toBe(1)
- expect(console.log.calls[0].args[0]).toBe(process.cwd())
-
- it 'should ls with absolute path', ->
- spyOn(console, 'log')
- gitr.ls('/Users/ken/dev/git/gitr/testDir')
- expect(console.log).toHaveBeenCalled()
- expect(console.log.callCount).toBe(2)
- expect(console.log.calls[0].args[0]).toBe('/Users/ken/dev/git/gitr/testDir/withGitRepo')
- expect(console.log.calls[1].args[0]).toBe('/Users/ken/dev/git/gitr/testDir/withGitRepoAtSecondLevel/secondLevel')
-
- it 'should have function for executing a git command', ->
- expect(gitr.do).toBeDefined()
- expect(cp.exec).toBeDefined();
- spyOn(cp, 'exec');
- gitr.do 'status', '/Users/ken/dev/git/gitr'
- expect(cp.exec).toHaveBeenCalled()
- expect(cp.exec.callCount).toBe(1)
- command = 'git --git-dir=/Users/ken/dev/git/gitr/.git --work-tree=/Users/ken/dev/git/gitr status'
- expect(cp.exec.calls[0].args[0]).toBe(command)
-
- it 'should execute git command at cmd if path is not supplied', ->
+ it 'should execute git command recursively for all git enabled repos', ->
expect(gitr.do).toBeDefined()
expect(cp.exec).toBeDefined();
- spyOn(cp, 'exec');
+ spyOn(cp, 'exec').andCallFake (cmd, cb) -> cb()
gitr.do 'status'
expect(cp.exec).toHaveBeenCalled()
- expect(cp.exec.callCount).toBe(1)
- command = 'git --git-dir=/Users/ken/dev/git/gitr/.git --work-tree=/Users/ken/dev/git/gitr status'
- expect(cp.exec.calls[0].args[0]).toBe(command)
+ expect(cp.exec.callCount).toBe(2)
+ expect(cp.exec.calls[0].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel/.git --work-tree=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel status")
+ expect(cp.exec.calls[1].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepo/.git --work-tree=#{process.cwd()}/withGitRepo status")
- it 'should execute git command recursively for all git enabled repos', ->
+ it 'should support splats', ->
expect(gitr.do).toBeDefined()
expect(cp.exec).toBeDefined();
spyOn(cp, 'exec').andCallFake (cmd, cb) -> cb()
- gitr.do 'status', 'testDir'
+ gitr.do 'diff', '--staged'
expect(cp.exec).toHaveBeenCalled()
expect(cp.exec.callCount).toBe(2)
- expect(cp.exec.calls[0].args[0]).toBe('git --git-dir=testDir/withGitRepoAtSecondLevel/secondLevel/.git --work-tree=testDir/withGitRepoAtSecondLevel/secondLevel status')
- expect(cp.exec.calls[1].args[0]).toBe('git --git-dir=testDir/withGitRepo/.git --work-tree=testDir/withGitRepo status')
+ expect(cp.exec.calls[0].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel/.git --work-tree=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel diff --staged")
+ expect(cp.exec.calls[1].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepo/.git --work-tree=#{process.cwd()}/withGitRepo diff --staged")
+
Please sign in to comment.
Something went wrong with that request. Please try again.