<commit_before>var config = require('../config'),
	gulp = require('gulp'),
	fs = require('graceful-fs'),
	path = require('path'),
	spawn = require('child_process').execFileSync,
	exec = require('child_process').exec,
	concat = require('concat-stream'),
	minimist = require('minimist'),
	fromString = require('from2-string'),
	recursive = require('recursive-readdir'),
	Client = require('ssh2').Client;

var state = {
	filesRemaining: 0,
	sftp: null,
	doneCb: null,
	localRoot: null,
	hash: {
		production: '',
		local: ''
	},
	files: []
}

var argv = minimist(process.argv.slice(2), {
	alias: {
		h: 'host',
		p: 'port',
		u: 'username',
		r: 'root',
		'pass': 'password'
	}
});

function ftpDeployTask(taskDoneCb) {

	// Keep this for when we are done uploading stuff
	state.doneCb = taskDoneCb;

	var conn = new Client();
	conn.on('ready', function() {
		conn.sftp(sftpConnected);
	}).connect({
		host: argv.host,
		port: argv.port,
		username: argv.username,
		password: argv.password,
	});
}

function sftpConnected(err, sftp) {
	if (err) throw err;

	console.log('SFTP CLIENT :: CONNECTED')

	state.sftp = sftp;

	// Try and read the revison file from the server
	var stream = sftp.createReadStream('.revision');

	// We could not open the revision file, assume this is a new server?
	stream.on('error', function(){
		handleNoRevision(collectFiles);
	});

	// Found a revision file, grab the buffer and go!
	stream.on('data', function(buffer){
		handleRevision(buffer);
	});
}

function handleNoRevision(cb) {
	console.log('no .revision file found, creating one for you and sftp-ing all the files');

	// Execute git to get the current hash
	var localCommit = spawn('git', ['rev-parse', 'HEAD']).toString();
	console.log('hash', localCommit);
	state.hash.local = localCommit;

	// We can now just collect our files and upload ALL THE THINGS!
	cb();
}

// Last step done before trying to upload stuff!
function collectFiles() {

	console.log('going to collect files now...');

	// Set the local root, where files will be taken from
	if (argv.root)
	{
		if (argv.root[argv.root.length-1] !== '/')
		{
			throw 'Invalid root path option! Should be formatted like "public_html/"';
			return;
		}
		state.localRoot = argv.root;
	}

	// We don't have a revision from the server, so collect all the files
	if (state.hash.production === ''){
		console.log('collecting ALL files to upload...');
		var public_html_files = spawn('find', [
			state.localRoot || '.',
			'-not', '-iwholename', '*.git*',
			'-not', '-iwholename', '*node_modules*',
			'-printf', '%P\n']).toString().split('\n');
		state.files = state.files.concat(public_html_files);
	}

	// Ready to upload files!
	state.filesRemaining = state.files.length;
	console.log('about to deploy [' + state.filesRemaining  + '] files!');

	state.files.forEach(sftpFile);
}

function handleRevision(buf) {
	// Save the remote hash
	state.hash.production = buf.toString().trim();

	var localCommit = spawn('git', ['rev-parse', 'HEAD']);
	state.hash.local = localCommit.toString().trim();

	console.log(state.hash.local + ' :: ' + state.hash.production);

	// Do the hashes match?<commit_msg>do not include directories in find system call<commit_after>var config = require('../config'),
	gulp = require('gulp'),
	fs = require('graceful-fs'),
	path = require('path'),
	spawn = require('child_process').execFileSync,
	exec = require('child_process').exec,
	concat = require('concat-stream'),
	minimist = require('minimist'),
	fromString = require('from2-string'),
	recursive = require('recursive-readdir'),
	Client = require('ssh2').Client;

var state = {
	filesRemaining: 0,
	sftp: null,
	doneCb: null,
	localRoot: null,
	hash: {
		production: '',
		local: ''
	},
	files: []
}

var argv = minimist(process.argv.slice(2), {
	alias: {
		h: 'host',
		p: 'port',
		u: 'username',
		r: 'root',
		'pass': 'password'
	}
});

function ftpDeployTask(taskDoneCb) {

	// Keep this for when we are done uploading stuff
	state.doneCb = taskDoneCb;

	var conn = new Client();
	conn.on('ready', function() {
		conn.sftp(sftpConnected);
	}).connect({
		host: argv.host,
		port: argv.port,
		username: argv.username,
		password: argv.password,
	});
}

function sftpConnected(err, sftp) {
	if (err) throw err;

	console.log('SFTP CLIENT :: CONNECTED')

	state.sftp = sftp;

	// Try and read the revison file from the server
	var stream = sftp.createReadStream('.revision');

	// We could not open the revision file, assume this is a new server?
	stream.on('error', function(){
		handleNoRevision(collectFiles);
	});

	// Found a revision file, grab the buffer and go!
	stream.on('data', function(buffer){
		handleRevision(buffer);
	});
}

function handleNoRevision(cb) {
	console.log('no .revision file found, creating one for you and sftp-ing all the files');

	// Execute git to get the current hash
	var localCommit = spawn('git', ['rev-parse', 'HEAD']).toString();
	console.log('hash', localCommit);
	state.hash.local = localCommit;

	// We can now just collect our files and upload ALL THE THINGS!
	cb();
}

// Last step done before trying to upload stuff!
function collectFiles() {

	console.log('going to collect files now...');

	// Set the local root, where files will be taken from
	if (argv.root)
	{
		if (argv.root[argv.root.length-1] !== '/')
		{
			throw 'Invalid root path option! Should be formatted like "public_html/"';
			return;
		}
		state.localRoot = argv.root;
	}

	// We don't have a revision from the server, so collect all the files
	if (state.hash.production === ''){
		console.log('collecting ALL files to upload...');
		var public_html_files = spawn('find', [
			state.localRoot || '.',
			'-not', '-iwholename', '*.git*',
			'-not', '-iwholename', '*node_modules*',
			'!', '-type', 'd',
			'-printf', '%P\n']).toString().split('\n');
		state.files = state.files.concat(public_html_files);
	}

	// Ready to upload files!
	state.filesRemaining = state.files.length;
	console.log('about to deploy [' + state.filesRemaining  + '] files!');

	state.files.forEach(sftpFile);
}

function handleRevision(buf) {
	// Save the remote hash
	state.hash.production = buf.toString().trim();

	var localCommit = spawn('git', ['rev-parse', 'HEAD']);
	state.hash.local = localCommit.toString().trim();

	console.log(state.hash.local + ' :: ' + state.hash.production);

	// Do the hashes match?
{'fraction_non_alphanumeric': 0.16748224151539068, 'fraction_numerical': 0.0015785319652722968, 'mean_word_length': 3.99290780141844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4298838', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2120, 'n_words': 578}