8
0

git.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const tools = require( './tools' );
  7. module.exports = {
  8. /**
  9. * Parses GitHub URL. Extracts used server, repository and branch.
  10. *
  11. * @param {String} url GitHub URL from package.json file.
  12. * @returns {Object} urlInfo
  13. * @returns {String} urlInfo.server
  14. * @returns {String} urlInfo.repository
  15. * @returns {String} urlInfo.branch
  16. */
  17. parseRepositoryUrl( url ) {
  18. const regexp = /^(git@github\.com:|https?:\/\/github.com\/)?([^#]+)(?:#)?(.*)$/;
  19. const match = url.match( regexp );
  20. let server;
  21. let repository;
  22. let branch;
  23. if ( !match ) {
  24. return null;
  25. }
  26. server = match[ 1 ] || 'https://github.com/';
  27. repository = match[ 2 ] || '';
  28. branch = match[ 3 ] || 'master';
  29. if ( !repository ) {
  30. return null;
  31. }
  32. return {
  33. server: server,
  34. repository: repository,
  35. branch: branch
  36. };
  37. },
  38. /**
  39. * Clones repository to workspace.
  40. *
  41. * @param {Object} urlInfo Parsed URL object from {@link #parseRepositoryUrl}.
  42. * @param {String} workspacePath Path to the workspace location where repository will be cloned.
  43. */
  44. cloneRepository( urlInfo, workspacePath ) {
  45. const cloneCommands = [
  46. `cd ${ workspacePath }`,
  47. `git clone ${ urlInfo.server + urlInfo.repository }`
  48. ];
  49. tools.shExec( cloneCommands.join( ' && ' ) );
  50. },
  51. /**
  52. * Checks out branch on selected repository.
  53. *
  54. * @param {String} repositoryLocation Absolute path to repository.
  55. * @param {String} branchName Name of the branch to checkout.
  56. */
  57. checkout( repositoryLocation, branchName ) {
  58. const checkoutCommands = [
  59. `cd ${ repositoryLocation }`,
  60. `git checkout ${ branchName }`
  61. ];
  62. tools.shExec( checkoutCommands.join( ' && ' ) );
  63. }
  64. };