2525module . exports = function ( grunt ) {
2626 "use strict" ;
2727
28- var child_process = require ( "child_process" ) ,
28+ var build = { } ,
29+ child_process = require ( "child_process" ) ,
30+ path = require ( "path" ) ,
2931 q = require ( "q" ) ,
3032 qexec = q . denodeify ( child_process . exec ) ;
33+
34+ function getGitInfo ( cwd ) {
35+ var opts = { cwd : cwd , maxBuffer : 1024 * 1024 } ,
36+ json = { } ;
37+
38+ // count the number of commits for our version number
39+ // <major>.<sprint>.<patch>-<number of commits>
40+ return qexec ( "git log --format=%h" , opts ) . then ( function ( stdout ) {
41+ json . commits = stdout . toString ( ) . match ( / [ 0 - 9 a - f ] \n / g) . length ;
42+
43+ // get the hash for the current commit (HEAD)
44+ return qexec ( "git rev-parse HEAD" , opts ) ;
45+ } ) . then ( function ( stdout ) {
46+ json . sha = / ( [ a - f 0 - 9 ] + ) / . exec ( stdout . toString ( ) ) [ 1 ] ;
47+
48+ // compare HEAD to the HEADs on the remote
49+ return qexec ( "git ls-remote --heads origin" , opts ) ;
50+ } ) . then ( function ( stdout ) {
51+ var log = stdout . toString ( ) ,
52+ re = new RegExp ( json . sha + "\\srefs/heads/(\\S+)\\s" ) ,
53+ match = re . exec ( log ) ,
54+ reflog ;
55+
56+ // if HEAD matches to a remote branch HEAD, grab the branch name
57+ if ( match ) {
58+ json . branch = match [ 1 ] ;
59+ return json ;
60+ }
61+
62+ // else, try match HEAD using reflog
63+ reflog = qexec ( "git reflog show --no-abbrev-commit --all" , opts ) ;
64+
65+ return reflog . then ( function ( stdout ) {
66+ var log = stdout . toString ( ) ,
67+ re = new RegExp ( json . sha + "\\srefs/(remotes/origin|heads)/(\\S+)@" ) ,
68+ match = re . exec ( log ) ;
69+
70+ json . branch = ( match && match [ 2 ] ) || "(no branch)" ;
71+
72+ return json ;
73+ } ) ;
74+ } ) ;
75+ }
76+
77+ function toProperties ( prefix , json ) {
78+ var out = "" ;
79+
80+ Object . keys ( json ) . forEach ( function ( key ) {
81+ out += prefix + key + "=" + json [ key ] + "\n" ;
82+ } ) ;
83+
84+ return out ;
85+ }
3186
3287 // task: build-num
3388 grunt . registerTask ( "build-prop" , "Write build.prop properties file for Jenkins" , function ( ) {
34- var done = this . async ( ) ,
35- out = "" ,
36- num ,
37- branch ,
38- sha ,
39- opts = { cwd : process . cwd ( ) , maxBuffer : 1024 * 1024 } ,
40- version = grunt . config ( "pkg" ) . version ;
41-
42- qexec ( "git log --format=%h" , opts ) . then ( function ( stdout , stderr ) {
43- num = stdout . toString ( ) . match ( / [ 0 - 9 a - f ] \n / g) . length ;
44- return qexec ( "git status" , opts ) ;
45- } ) . then ( function ( stdout , stderr ) {
46- branch = / O n b r a n c h ( .* ) / . exec ( stdout . toString ( ) . trim ( ) ) [ 1 ] ;
47- return qexec ( "git log -1" , opts ) ;
48- } ) . then ( function ( stdout , stderr ) {
49- sha = / c o m m i t ( .* ) / . exec ( stdout . toString ( ) . trim ( ) ) [ 1 ] ;
89+ var done = this . async ( ) ,
90+ json = { } ,
91+ out = "" ,
92+ opts = { cwd : process . cwd ( ) , maxBuffer : 1024 * 1024 } ,
93+ version = grunt . config ( "pkg" ) . version ,
94+ www_repo = process . cwd ( ) ,
95+ shell_repo = path . resolve ( www_repo , grunt . config ( "shell.repo" ) ) ,
96+ www_git ,
97+ shell_git ;
5098
51- out += "build.version=" + version . substr ( 0 , version . lastIndexOf ( "-" ) + 1 ) + num + "\n" ;
52- out += "build.number=" + num + "\n" ;
53- out += "build.branch=" + branch + "\n" ;
54- out += "build.sha=" + sha + "\n" ;
99+ getGitInfo ( www_repo ) . then ( function ( json ) {
100+ www_git = json ;
101+ return getGitInfo ( shell_repo ) ;
102+ } ) . then ( function ( json ) {
103+ shell_git = json ;
104+ } , function ( err ) {
105+ // shell git info is optional
106+ grunt . log . writeln ( err ) ;
107+ } ) . finally ( function ( ) {
108+ out += "brackets_build_version=" + version . substr ( 0 , version . lastIndexOf ( "-" ) + 1 ) + www_git . commits + "\n" ;
109+ out += toProperties ( "brackets_www_" , www_git ) ;
110+
111+ if ( shell_git ) {
112+ out += toProperties ( "brackets_shell_" , shell_git ) ;
113+ }
55114
56115 grunt . log . write ( out ) ;
57116 grunt . file . write ( "build.prop" , out ) ;
58117
59118 done ( ) ;
60- } , function ( err ) {
61- grunt . log . writeln ( err ) ;
62- done ( false ) ;
63119 } ) ;
64120 } ) ;
121+
122+ build . getGitInfo = getGitInfo ;
123+
124+ return build ;
65125} ;
0 commit comments