Skip to content

Instantly share code, notes, and snippets.

@cognominal
Created May 6, 2011 23:20
Show Gist options
  • Save cognominal/959992 to your computer and use it in GitHub Desktop.
Save cognominal/959992 to your computer and use it in GitHub Desktop.
why match is undefined at the last line of the program?
#!/usr/bin/env coffee
fs = require 'fs'
spawn = (require 'child_process').spawn
log = console.log
data = fs.readFileSync '.git/FETCH_HEAD', 'utf8'
for ln in data.split /\n/
match = /(.{40}).*?branch '(.*)'/.exec ln
continue unless match
args = "log -1 --format=%cd #{match[1]}".split /\s+/
fun = ->
branchNm = match[2]
git = spawn 'git', args
git.stdout.on 'data', (data) -> log "#{data.toString 'utf8', 0, data.length-1} #{branchNm}"
fun()
###
The problem is that scope in javascript is function and not loop. So I have to create a function to capture the branch name
at each iteration of the loop.
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment