Skip to content

Commit

Permalink
Enforce consistent codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 17, 2012
1 parent ce74b35 commit 5472c5b
Showing 1 changed file with 60 additions and 70 deletions.
130 changes: 60 additions & 70 deletions pulley.js
Expand Up @@ -7,10 +7,9 @@
(function() {
"use strict";

var // Application requirements
child = require( "child_process" ),
http = require( "https" ),
fs = require( "fs" ),
var child = require("child_process"),
http = require("https"),
fs = require("fs"),
prompt = require("prompt"),
request = require("request"),

Expand All @@ -19,8 +18,8 @@
spawn = child.spawn,

// Process arguments
id = process.argv[2],
done = process.argv[3],
id = process.argv[ 2 ],
done = process.argv[ 3 ],

// Localized application references
user_repo = "",
Expand All @@ -33,7 +32,7 @@
// We don't want the default prompt message
prompt.message = "";

process.stdout.write( "Initializing... " );
process.stdout.write("Initializing... ");

exec( "git config --global --get pulley.token", function( error, stdout, stderr ) {
token = trim( stdout );
Expand Down Expand Up @@ -84,100 +83,94 @@

function init() {
if ( !id ) {
exit( "No pull request ID specified, please provide one." );
exit("No pull request ID specified, please provide one.");
}
exec( "git remote -v show " + config.remote, function( error, stdout, stderr ) {
user_repo = (/URL:.*?([\w\-]+\/[\w\-]+)/.exec( stdout ) || [])[1];
user_repo = ( /URL:.*?([\w\-]+\/[\w\-]+)/.exec( stdout ) || [] )[ 1 ];
tracker = config.repos[ user_repo ];

if ( user_repo ) {
getStatus();
} else {
exit( "External repository not found." );
exit("External repository not found.");
}
});
}

function getStatus() {

exec( "git status", function( error, stdout, stderr ) {
if ( /Changes to be committed/i.test( stdout ) ) {
if ( done ) {
getPullData();

} else {
exit( "Please commit changed files before attemping a pull/merge." );
exit("Please commit changed files before attemping a pull/merge.");
}

} else if ( /Changes not staged for commit/i.test( stdout ) ) {
if ( done ) {
exit( "Please add files that you wish to commit." );
exit("Please add files that you wish to commit.");

} else {
exit( "Please stash files before attempting a pull/merge." );
exit("Please stash files before attempting a pull/merge.");
}
} else {
if ( done ) {
exit( "It looks like you've broken your merge attempt." );

exit("It looks like you've broken your merge attempt.");
} else {
getPullData();
}
}
});
}


function getPullData() {
process.stdout.write( "done.\n" );
process.stdout.write( "Getting pull request details... " );
process.stdout.write("done.\n");
process.stdout.write("Getting pull request details... ");

callApi({
path: "/repos/" + user_repo + "/pulls/" + id
}, function( data ) {
try {
var pull = JSON.parse(data);
var pull = JSON.parse( data );

process.stdout.write( "done.\n" );
process.stdout.write("done.\n");

if ( done ) {
commit( pull );
} else {
mergePull( pull );
}
} catch( e ) {
exit( "Error retrieving pull request from Github." );
exit("Error retrieving pull request from Github.");
}
});
}

function mergePull( pull ) {
process.stdout.write( "Pulling and merging results... " );
var repo = pull.head.repo.ssh_url,

This comment has been minimized.

Copy link
@mikesherov

mikesherov Apr 17, 2012

Contributor

I'm not sure we need to declare vars before guard clauses to conform to style.

This comment has been minimized.

Copy link
@mikesherov

mikesherov Apr 17, 2012

Contributor

I'm not saying you need to revert this, I'm just saying that my personal preference is guard clause before var declaration. I personally feel it's clearer. To each his own though.

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Apr 17, 2012

Author Contributor

It's just what I'm used to doing. It's also recommended by idiomatic.js.

I find it useful to consistently do it this way, since you cannot always have guard statements on top. Like eg if your guard statement is using a defined variable. And since everything is hoisted anyway, it makes it more clear for noobs.

This comment has been minimized.

Copy link
@mikesherov

mikesherov via email Apr 17, 2012

Contributor

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Apr 17, 2012

Author Contributor

Not at all, I like to do that to :)

repo_branch = pull.head.ref,
branch = "pull-" + id,
checkout = "git checkout -b " + branch;

if( pull.state === "closed" ) {
exit( "Can not merge closed Pull Requests. " );
process.stdout.write("Pulling and merging results... ");

if ( pull.state === "closed" ) {
exit("Can not merge closed Pull Requests.");
}

if ( pull.merged ) {
exit( "This Pull Request has already been merged. " );
exit("This Pull Request has already been merged.");
}

// TODO: give user the option to resolve the merge by themselves
if ( !pull.mergeable ) {
exit( "This Pull Request is not automatically mergeable. " );
exit("This Pull Request is not automatically mergeable.");
}

var repo = pull.head.repo.ssh_url,
repo_branch = pull.head.ref,
branch = "pull-" + id,
checkout = "git checkout -b " + branch;

exec( "git checkout master && git pull " + config.remote + " master && git submodule update --init && " + checkout, function( error, stdout, stderr ) {
if ( /toplevel/i.test( stderr ) ) {
exit( "please call pulley from the toplevel directory of this repository" );
exit("Please call pulley from the toplevel directory of this repo.");
} else if ( /fatal/i.test( stderr ) ) {
exec( "git branch -D " + branch + " && " + checkout, doPull );

} else {
doPull();
}
Expand All @@ -192,44 +185,42 @@

exec( pull_cmds.join( " && " ), function( error, stdout, stderr ) {
if ( /Merge conflict/i.test( stdout ) ) {
exit( "Merge conflict. Please resolve then run: " +
process.argv.join( " " ) + " done" );

exit("Merge conflict. Please resolve then run: " +
process.argv.join(" ") + " done");
} else {
process.stdout.write( "done.\n" );
process.stdout.write("done.\n");
commit( pull );
}
});
}
}

function commit( pull ) {
process.stdout.write( "Getting author and committing changes... " );
process.stdout.write("Getting author and committing changes... ");

callApi({
path: "/repos/" + user_repo + "/pulls/" + id + "/commits"
}, function( data ) {
var match,
msg = "Closes #" + id + ": " + pull.title + ".",
author = JSON.parse(data)[0].commit.author.name,
author = JSON.parse( data )[ 0 ].commit.author.name,
issues = [],
urls = [],
findBug = /#(\d+)/g;

// search title and body for issues
// for issues to link to
// Search title and body for issues for issues to link to
if ( tracker ) {
while ( (match = findBug.exec( pull.title + pull.body )) ) {
urls.push( tracker + match[1] );
while ( ( match = findBug.exec( pull.title + pull.body ) ) ) {
urls.push( tracker + match[ 1 ] );
}
}

// search just body for issues to add to the commit message
while ( (match = findBug.exec( pull.body )) ) {
issues.push( " Fixes #" + match[1] );
// Search just body for issues to add to the commit message
while ( ( match = findBug.exec( pull.body ) ) ) {
issues.push( " Fixes #" + match[ 1 ] );
}

// add issues to the commit message
// Add issues to the commit message
msg += issues.join(",");

if ( urls.length ) {
Expand All @@ -241,7 +232,7 @@
var commit = [ "commit", "-a", "--message=" + msg ];

if ( config.interactive ) {
commit.push( "-e" );
commit.push("-e");
}

if ( author ) {
Expand All @@ -252,14 +243,13 @@
// Thanks to: https://gist.github.com/927052
spawn( "git", commit, {
customFds: [ process.stdin, process.stdout, process.stderr ]
}).on("exit", function() {
}).on( "exit", function() {
getHEAD(function( newCommit ) {

if ( oldCommit === newCommit ) {
reset( "No commit, aborting push." );
reset("No commit, aborting push.");
} else {
exec( "git push " + config.remote + " master", function( error, stdout, stderr ) {
process.stdout.write( "done.\n" );
process.stdout.write("done.\n");
exit();
});
}
Expand All @@ -270,8 +260,8 @@
}

// TODO: Add check to API call if autorization fails. Show login to reauthorize.
function callApi(options, callback, data) {
setTimeout(function(){
function callApi( options, callback, data ) {
setTimeout(function() {
var req, datastring;

options.host = options.host || "api.github.com";
Expand All @@ -281,28 +271,28 @@
Host: "api.github.com"
};

if(data){
datastring = JSON.stringify(data);
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.headers['Content-Length'] = datastring.length;
if ( data ) {
datastring = JSON.stringify( data );
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
options.headers["Content-Length"] = datastring.length;
}

req = http.request(options, function( res ) {
req = http.request( options, function( res ) {
var data = [];

res.on( "data", function( chunk ) {
data.push( chunk );
});

res.on( "end", function() {
setTimeout(function(){
callback(data.join(""));
setTimeout(function() {
callback( data.join("") );
}, 1000);
});
});

if(data){
req.write(datastring);
if ( data ) {
req.write( datastring );
}

req.end();
Expand All @@ -311,18 +301,18 @@

function getHEAD( fn ) {
exec( "git log | head -1", function( error, stdout, stderr ) {
var commit = (/commit (.*)/.exec( stdout ) || [])[1];
var commit = ( /commit (.*)/.exec( stdout ) || [] )[ 1 ];

fn( commit );
});
}

function reset( msg ) {
console.error( "\n" + msg );
process.stderr.write( "Resetting files... " );
process.stderr.write("Resetting files... ");

exec( "git reset --hard ORIG_HEAD", function() {
process.stderr.write( "done.\n" );
process.stderr.write("done.\n");
exit();
});
}
Expand All @@ -336,7 +326,7 @@
}

function trim( string ) {
return string.replace(/^\s*|\s*$/g, '');
return string.replace( /^\s*|\s*$/g, '' );
}

})();

0 comments on commit 5472c5b

Please sign in to comment.