git post-receive hook & npm

อาทิตย์นี้ทำ script เพื่อให้รัน test ทุกครั้งที่ push เจออะไรแปลกๆ บางอย่างเเมื่อใช้คู่กับ npm ที่สั่งให้ install dependencies หลังจาก push สำเร็จแล้ว ความแปลกอยู่ที่ถ้าไฟล์ package.json ใช้ private repo ที่ชี้ไปยัง git repo อื่นจะไม่สามารถ install dependencies ด้วย post-receive hook ได้ขึ้น error ทุกครั้งประมาณว่าไม่สามารถ checkout branch ออกมาได้

remote: npm ERR! git checkout master fatal: Not a git repository: '.'
remote: npm ERR! Error: `git "checkout" "master"` failed with 128
remote: npm ERR!     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/exec.js:49:20)
remote: npm ERR!     at ChildProcess.emit (events.js:67:17)
remote: npm ERR!     at ChildProcess.onexit (child_process.js:192:12)
remote: npm ERR! Report this *entire* log at:
remote: npm ERR!     <http://github.com/isaacs/npm/issues>
remote: npm ERR! or email it to:
remote: npm ERR!     <npm-@googlegroups.com>
remote: npm ERR!
remote: npm ERR! System FreeBSD 8.2-RELEASE
remote: npm ERR! command "node" "/usr/local/bin/npm" "install"
remote: npm ERR! cwd /tmp/1322622091
remote: npm ERR! node -v v0.4.12
remote: npm ERR! npm -v 1.0.106
remote: npm ERR!
remote: npm ERR! Additional logging details can be found in:
remote: npm ERR!     /tmp/1322622091/npm-debug.log
remote: npm not ok

ทางแก้แบบชั่วคราวตอนนี้คือแก้ npm source เองตามด้านล่าง

diff --git a/lib/utils/exec.js b/lib/utils/exec.js
index 0fa0371..9c6a8ac 100644
--- a/lib/utils/exec.js
+++ b/lib/utils/exec.js
@@ -46,11 +46,11 @@ function exec (cmd, args, env, takeOver, cwd, uid, gid, cb) {
   })
   cp.on("exit", function (code) {
     var er = null
-    if (code) er = new Error("`"+cmd
-                            +(args.length ? " "
-                                          + args.map(JSON.stringify).join(" ")
-                                          : "")
-                            +"` failed with "+code)
     cb(er, code, stdout, stderr)
   })
   return cp

diff ด้านบนเพื่อให้ skip error ชั่วคราวแต่ถ้าจะแก้จริงๆ คงต้องไล่ให้มากกว่านี้ก่อน สำหรับ script เจ้าปัญหาคือ

#!/bin/sh
read oldrev newrev ref

TMP=/tmp/`date +%s`
mkdir $TMP

echo "Archive $newrev"
/usr/local/bin/git archive $newrev | /usr/bin/tar -x -C $TMP -f -

cd $TMP
/usr/local/bin/npm install
echo "Install dependencies success."

cd ../tests
/usr/local/bin/node runtest.js

cd /

rm -rf $TMP

runtest.js ที่เห็นด้านบนเป็น script ทำมือเพื่อรวม test ทั้งหมดมารันทีเดียว วางแผนไว้ว่าจะทำให้มันดูดีกว่านี้แต่ยังขี้เกียจอยู่ ทางแก้ต่อไปก็คงต้องไปลองถามคนทำ npm หละว่าจะแก้อย่างไรได้บ้าง เพื่อให้มันแก้ได้อย่างถาวร

About llun

Just a programmer

, , , ,