Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

removed ability to send path as arg to gitr.

  • Loading branch information...
commit 276a52206dbdf2b1a11e554ab86179f50c8d0568 1 parent 53f94bb
Ken Gullaksen authored
21 lib/gitr.coffee
... ... @@ -1,11 +1,9 @@
1 1 fs = require 'fs', _ = require 'underscore', cp = require 'child_process', color = require './color', q = require './q'
2 2
3 3 class GitR
4   -
5   - checkDirArg = (dirArg) -> if dirArg?.length then "#{dirArg}".replace /\/$/, '' else "#{process.cwd()}"
6 4 log = (m...) => console.log m...
7 5
8   - findRepos = (dir) =>
  6 + findRepos = (dir = process.cwd()) =>
9 7 dirs = []
10 8 if fs.statSync(dir).isDirectory()
11 9 subDirs = fs.readdirSync(dir)
@@ -16,8 +14,8 @@ class GitR
16 14 dirs.push findRepos dir + '/' + subDir
17 15 _.flatten dirs
18 16
19   - exec = (gitCmd, repo, cb) =>
20   - cp.exec gitCmd, (err, stdout, stderr) ->
  17 + exec = (cmd, repo, cb) =>
  18 + cp.exec "git --git-dir=#{repo}/.git --work-tree=#{repo} #{cmd.join(' ')}", (err, stdout, stderr) ->
21 19 msg = ''
22 20 msg += "#{stdout}\n#{color.cls}" if stdout?.length > 0
23 21 msg += "#{color.red}#{err}#{color.cls}" if err?.length > 0
@@ -25,16 +23,11 @@ class GitR
25 23 log "#{color.yellow}::#{repo}::#{color.green}\n#{msg}" if msg?.length > 0
26 24 cb()
27 25
28   - ls: (dir) => _.each findRepos(checkDirArg(dir)), (repo) => log repo
29   -
30   - do: (cmd = '', path) =>
  26 + do: (cmd...) =>
31 27 fns = []
32   - dir = checkDirArg(path)
33   - repos = findRepos(dir)
34   - _.each repos, (repo) =>
35   - fns.push (cb) =>
36   - exec "git --git-dir=#{repo}/.git --work-tree=#{repo} #{cmd}", repo, cb
  28 + repos = findRepos()
  29 + _.each repos, (repo) => fns.push (cb) => exec cmd, repo, cb
37 30 q.dequeue fns, =>
38   - log "#{color.red}no git repos under #{color.yellow}#{dir}#{color.cls}" if repos.length == 0
  31 + log "#{color.red}no git repos under #{color.yellow}#{process.cwd()}#{color.cls}" if repos.length == 0
39 32
40 33 exports = module.exports = GitR
3  lib/runr.coffee
... ... @@ -1,5 +1,4 @@
1 1 argv = process.argv.slice(2)
2 2
3 3 GitR = require './gitr'
4   -gitr = new GitR()
5   -gitr.do argv...
  4 +new GitR().do argv...
2  package.json
... ... @@ -1,6 +1,6 @@
1 1 {
2 2 "name":"gitr",
3   - "version":"0.0.2",
  3 + "version":"0.0.3",
4 4 "description":"perform git commands recursively",
5 5 "keywords": [ "git", "recursive", "command", "line", "tool" ],
6 6 "main":"./lib/gitr.js",
71 spec/gitr.spec.coffee
@@ -15,80 +15,39 @@ describe 'gitr', ->
15 15 'testDir/withGitRepoAtSecondLevel/secondLevel',
16 16 'testDir/withGitRepoAtSecondLevel/secondLevel/.git',
17 17 ]
  18 + origDir = process.cwd()
  19 + testDir = 'testDir'
18 20
19 21 beforeEach ->
20 22 for directory in directoryStructure
21 23 unless fs.existsSync(directory)
22 24 fs.mkdirSync(directory)
23 25 fs.writeFileSync(directory + '/tmpFile', 'stuff') if directory.search('.git$') == -1
  26 + process.chdir(testDir)
24 27
25 28 afterEach ->
26   - rimraf.sync 'testDir'
  29 + process.chdir(origDir)
  30 + rimraf.sync testDir
27 31
28   - it 'should be defined', ->
29   - expect(GitR).toBeDefined()
30   -
31   - it 'should list all git enabled directories under given directory', ->
32   - spyOn(console, 'log')
33   - gitr.ls('testDir')
34   -
35   - expect(console.log).toHaveBeenCalled()
36   - expect(console.log.callCount).toBe(2)
37   - expect(console.log.calls[0].args[0]).toBe('testDir/withGitRepo')
38   - expect(console.log.calls[1].args[0]).toBe('testDir/withGitRepoAtSecondLevel/secondLevel')
39   -
40   - it 'should trim trailing slash list all git enabled directories under given directory', ->
41   - spyOn(console, 'log')
42   - gitr.ls('testDir')
43   -
44   - expect(console.log).toHaveBeenCalled()
45   - expect(console.log.callCount).toBe(2)
46   - expect(console.log.calls[0].args[0]).toBe('testDir/withGitRepo')
47   - expect(console.log.calls[1].args[0]).toBe('testDir/withGitRepoAtSecondLevel/secondLevel')
48   -
49   - it 'should ls from cwd if no argument is passed', ->
50   - spyOn(console, 'log')
51   - gitr.ls()
52   - expect(console.log).toHaveBeenCalled()
53   - expect(console.log.callCount).toBe(1)
54   - expect(console.log.calls[0].args[0]).toBe(process.cwd())
55   -
56   - it 'should ls with absolute path', ->
57   - spyOn(console, 'log')
58   - gitr.ls('/Users/ken/dev/git/gitr/testDir')
59   - expect(console.log).toHaveBeenCalled()
60   - expect(console.log.callCount).toBe(2)
61   - expect(console.log.calls[0].args[0]).toBe('/Users/ken/dev/git/gitr/testDir/withGitRepo')
62   - expect(console.log.calls[1].args[0]).toBe('/Users/ken/dev/git/gitr/testDir/withGitRepoAtSecondLevel/secondLevel')
63   -
64   - it 'should have function for executing a git command', ->
65   - expect(gitr.do).toBeDefined()
66   - expect(cp.exec).toBeDefined();
67   - spyOn(cp, 'exec');
68   - gitr.do 'status', '/Users/ken/dev/git/gitr'
69   - expect(cp.exec).toHaveBeenCalled()
70   - expect(cp.exec.callCount).toBe(1)
71   - command = 'git --git-dir=/Users/ken/dev/git/gitr/.git --work-tree=/Users/ken/dev/git/gitr status'
72   - expect(cp.exec.calls[0].args[0]).toBe(command)
73   -
74   - it 'should execute git command at cmd if path is not supplied', ->
  32 + it 'should execute git command recursively for all git enabled repos', ->
75 33 expect(gitr.do).toBeDefined()
76 34 expect(cp.exec).toBeDefined();
77   - spyOn(cp, 'exec');
  35 + spyOn(cp, 'exec').andCallFake (cmd, cb) -> cb()
78 36 gitr.do 'status'
79 37 expect(cp.exec).toHaveBeenCalled()
80   - expect(cp.exec.callCount).toBe(1)
81   - command = 'git --git-dir=/Users/ken/dev/git/gitr/.git --work-tree=/Users/ken/dev/git/gitr status'
82   - expect(cp.exec.calls[0].args[0]).toBe(command)
  38 + expect(cp.exec.callCount).toBe(2)
  39 + expect(cp.exec.calls[0].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel/.git --work-tree=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel status")
  40 + expect(cp.exec.calls[1].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepo/.git --work-tree=#{process.cwd()}/withGitRepo status")
83 41
84   - it 'should execute git command recursively for all git enabled repos', ->
  42 + it 'should support splats', ->
85 43 expect(gitr.do).toBeDefined()
86 44 expect(cp.exec).toBeDefined();
87 45 spyOn(cp, 'exec').andCallFake (cmd, cb) -> cb()
88   - gitr.do 'status', 'testDir'
  46 + gitr.do 'diff', '--staged'
89 47 expect(cp.exec).toHaveBeenCalled()
90 48 expect(cp.exec.callCount).toBe(2)
91   - expect(cp.exec.calls[0].args[0]).toBe('git --git-dir=testDir/withGitRepoAtSecondLevel/secondLevel/.git --work-tree=testDir/withGitRepoAtSecondLevel/secondLevel status')
92   - expect(cp.exec.calls[1].args[0]).toBe('git --git-dir=testDir/withGitRepo/.git --work-tree=testDir/withGitRepo status')
  49 + expect(cp.exec.calls[0].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel/.git --work-tree=#{process.cwd()}/withGitRepoAtSecondLevel/secondLevel diff --staged")
  50 + expect(cp.exec.calls[1].args[0]).toBe("git --git-dir=#{process.cwd()}/withGitRepo/.git --work-tree=#{process.cwd()}/withGitRepo diff --staged")
  51 +
93 52
94 53

0 comments on commit 276a522

Please sign in to comment.
Something went wrong with that request. Please try again.