wikijs-fork/models/git.js

298 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-08-20 04:50:29 +00:00
"use strict";
var NodeGit = require("nodegit"),
Promise = require('bluebird'),
path = require('path'),
os = require('os'),
fs = Promise.promisifyAll(require("fs")),
2016-08-25 22:55:42 +00:00
moment = require('moment'),
2016-08-20 04:50:29 +00:00
_ = require('lodash');
/**
* Git Model
*/
module.exports = {
_git: null,
_repo: {
path: '',
2016-08-25 02:10:03 +00:00
branch: 'master',
2016-08-20 04:50:29 +00:00
exists: false,
2016-08-25 02:10:03 +00:00
inst: null,
sync: true
},
2016-08-25 22:55:42 +00:00
_signature: {
name: 'Wiki',
email: 'user@example.com'
},
2016-08-25 02:10:03 +00:00
_opts: {
clone: {},
push: {}
2016-08-20 04:50:29 +00:00
},
/**
* Initialize Git model
*
* @param {Object} appconfig The application config
* @return {Object} Git model instance
*/
init(appconfig) {
let self = this;
//-> Build repository path
if(_.isEmpty(appconfig.git.path) || appconfig.git.path === 'auto') {
self._repo.path = path.join(ROOTPATH, 'repo');
} else {
self._repo.path = appconfig.git.path;
}
//-> Initialize repository
self._initRepo(appconfig).then((repo) => {
self._repo.inst = repo;
2016-08-25 02:10:03 +00:00
if(self._repo.sync) {
self.resync();
}
2016-08-20 04:50:29 +00:00
});
2016-08-25 22:55:42 +00:00
// Define signature
self._signature.name = appconfig.git.userinfo.name || 'Wiki';
self._signature.email = appconfig.git.userinfo.email || 'user@example.com';
2016-08-20 04:50:29 +00:00
return self;
},
/**
* Initialize Git repository
*
* @param {Object} appconfig The application config
* @return {Object} Promise
*/
_initRepo(appconfig) {
let self = this;
winston.info('[GIT] Initializing Git repository...');
//-> Check if path is accessible
return fs.mkdirAsync(self._repo.path).catch((err) => {
if(err.code !== 'EEXIST') {
winston.error('Invalid Git repository path or missing permissions.');
}
}).then(() => {
//-> Check if path already contains a git working folder
return fs.statAsync(path.join(self._repo.path, '.git')).then((stat) => {
self._repo.exists = stat.isDirectory();
}).catch((err) => {
self._repo.exists = false;
});
}).then(() => {
//-> Init repository
let repoInitOperation = null;
2016-08-25 02:10:03 +00:00
self._repo.branch = appconfig.git.branch;
self._repo.sync = appconfig.git.remote;
self._opts.clone = self._generateCloneOptions(appconfig);
self._opts.push = self._generatePushOptions(appconfig);
2016-08-20 04:50:29 +00:00
if(self._repo.exists) {
winston.info('[GIT] Using existing repository...');
repoInitOperation = NodeGit.Repository.open(self._repo.path);
2016-08-25 02:10:03 +00:00
} else if(appconfig.git.remote) {
2016-08-20 04:50:29 +00:00
winston.info('[GIT] Cloning remote repository for first time...');
2016-08-25 02:10:03 +00:00
repoInitOperation = NodeGit.Clone(appconfig.git.url, self._repo.path, self._opts.clone);
2016-08-20 04:50:29 +00:00
} else {
winston.info('[GIT] Using offline local repository...');
repoInitOperation = NodeGit.Repository.init(self._repo.path, 0);
}
return repoInitOperation;
}).catch((err) => {
winston.error('Unable to open or clone Git repository!');
winston.error(err);
}).then((repo) => {
2016-08-25 02:10:03 +00:00
if(self._repo.sync) {
NodeGit.Remote.setPushurl(repo, 'origin', appconfig.git.url);
}
return repo;
2016-08-20 04:50:29 +00:00
winston.info('[GIT] Git repository is now ready.');
});
},
/**
* Generate Clone Options object
*
* @param {Object} appconfig The application configuration
* @return {Object} CloneOptions object
*/
_generateCloneOptions(appconfig) {
2016-08-25 02:10:03 +00:00
let cloneOptions = new NodeGit.CloneOptions();
cloneOptions.fetchOpts = this._generateFetchOptions(appconfig);
return cloneOptions;
},
_generateFetchOptions(appconfig) {
let fetchOptions = new NodeGit.FetchOptions();
fetchOptions.callbacks = this._generateRemoteCallbacks(appconfig);
return fetchOptions;
},
_generatePushOptions(appconfig) {
let pushOptions = new NodeGit.PushOptions();
pushOptions.callbacks = this._generateRemoteCallbacks(appconfig);
return pushOptions;
},
_generateRemoteCallbacks(appconfig) {
let remoteCallbacks = new NodeGit.RemoteCallbacks();
let credFunc = this._generateCredentials(appconfig);
remoteCallbacks.credentials = () => { return credFunc; };
2016-08-25 22:55:42 +00:00
remoteCallbacks.transferProgress = _.noop;
2016-08-20 04:50:29 +00:00
if(os.type() === 'Darwin') {
2016-08-25 02:10:03 +00:00
remoteCallbacks.certificateCheck = () => { return 1; }; // Bug in OS X, bypass certs check workaround
2016-08-25 22:55:42 +00:00
} else {
remoteCallbacks.certificateCheck = _.noop;
2016-08-20 04:50:29 +00:00
}
2016-08-25 02:10:03 +00:00
return remoteCallbacks;
},
_generateCredentials(appconfig) {
let cred = null;
switch(appconfig.git.auth.type) {
case 'basic':
cred = NodeGit.Cred.userpassPlaintextNew(
appconfig.git.auth.user,
appconfig.git.auth.pass
);
break;
case 'oauth':
cred = NodeGit.Cred.userpassPlaintextNew(
appconfig.git.auth.token,
"x-oauth-basic"
);
break;
case 'ssh':
cred = NodeGit.Cred.sshKeyNew(
appconfig.git.auth.user,
appconfig.git.auth.publickey,
appconfig.git.auth.privatekey,
appconfig.git.auth.passphrase
);
break;
default:
cred = NodeGit.Cred.defaultNew();
break;
}
return cred;
},
resync() {
let self = this;
// Fetch
return self._repo.inst.fetch('origin', self._opts.clone.fetchOpts)
.catch((err) => {
winston.error('Unable to fetch from git origin!' + err);
})
// Merge
.then(() => {
return self._repo.inst.mergeBranches(self._repo.branch, 'origin/' + self._repo.branch);
})
.catch((err) => {
winston.error('Unable to merge from remote head!' + err);
})
// Push
.then(() => {
return self._repo.inst.getRemote('origin').then((remote) => {
2016-08-25 22:55:42 +00:00
// Get modified files
return self._repo.inst.refreshIndex().then((index) => {
return self._repo.inst.getStatus().then(function(arrayStatusFile) {
let addOp = [];
// Add to next commit
_.forEach(arrayStatusFile, (v) => {
addOp.push(arrayStatusFile[0].path());
});
console.log('DUDE1');
// Create Commit
let sig = NodeGit.Signature.create(self._signature.name, self._signature.email, moment().utc().unix(), 0);
return self._repo.inst.createCommitOnHead(addOp, sig, sig, "Wiki Sync").then(() => {
console.log('DUDE2');
return remote.connect(NodeGit.Enums.DIRECTION.PUSH, self._opts.push.callbacks).then(() => {
console.log('DUDE3');
// Push to remote
return remote.push( ["refs/heads/master:refs/heads/master"], self._opts.push).then((errNum) => {
console.log('DUDE' + errNum);
}).catch((err) => {
console.log(err);
});
});
});
});
})
/**/
2016-08-25 02:10:03 +00:00
});
}).catch((err) => {
winston.error('Unable to push to git origin!' + err);
});
2016-08-20 04:50:29 +00:00
}
};