8
0

git.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * Holds boilerplate repository Git URL.
  10. *
  11. * @private
  12. * @readonly
  13. * @type {String}
  14. */
  15. BOILERPLATE_REPOSITORY: 'git@github.com:ckeditor/ckeditor-boilerplate.git',
  16. /**
  17. * Holds boilerplate branch used in CKEditor5 projects.
  18. *
  19. * @private
  20. * @readonly
  21. * @type {String}
  22. */
  23. BOILERPLATE_BRANCH: 'ckeditor5',
  24. /**
  25. * Parses GitHub URL. Extracts used server, repository and branch.
  26. *
  27. * @param {String} url GitHub URL from package.json file.
  28. * @returns {Object} urlInfo
  29. * @returns {String} urlInfo.server
  30. * @returns {String} urlInfo.repository
  31. * @returns {String} urlInfo.user
  32. * @returns {String} urlInfo.name
  33. * @returns {String} urlInfo.branch
  34. */
  35. parseRepositoryUrl( url ) {
  36. const regexp = /^((?:git@|(?:http[s]?|git):\/\/)github\.com(?:\/|:))?(([\w-]+)\/([\w-]+(?:\.git)?))(?:#([\w-\/]+))?$/;
  37. const match = url.match( regexp );
  38. let server;
  39. let repository;
  40. let branch;
  41. let name;
  42. let user;
  43. if ( !match ) {
  44. return null;
  45. }
  46. server = match[ 1 ] || 'https://github.com/';
  47. repository = match[ 2 ];
  48. user = match[ 3 ];
  49. name = match[ 4 ];
  50. branch = match[ 5 ] || 'master';
  51. name = /\.git$/.test( name ) ? name.slice( 0, -4 ) : name;
  52. return {
  53. server: server,
  54. repository: repository,
  55. branch: branch,
  56. user: user,
  57. name: name
  58. };
  59. },
  60. /**
  61. * Clones repository to workspace.
  62. *
  63. * @param {Object} urlInfo Parsed URL object from {@link #parseRepositoryUrl}.
  64. * @param {String} workspacePath Path to the workspace location where repository will be cloned.
  65. */
  66. cloneRepository( urlInfo, workspacePath ) {
  67. const cloneCommands = [
  68. `cd ${ workspacePath }`,
  69. `git clone ${ urlInfo.server + urlInfo.repository }`
  70. ];
  71. tools.shExec( cloneCommands.join( ' && ' ) );
  72. },
  73. /**
  74. * Checks out branch on selected repository.
  75. *
  76. * @param {String} repositoryLocation Absolute path to repository.
  77. * @param {String} branchName Name of the branch to checkout.
  78. */
  79. checkout( repositoryLocation, branchName ) {
  80. const checkoutCommands = [
  81. `cd ${ repositoryLocation }`,
  82. `git checkout ${ branchName }`
  83. ];
  84. tools.shExec( checkoutCommands.join( ' && ' ) );
  85. },
  86. /**
  87. * Pulls specified branch from origin.
  88. *
  89. * @param {String} repositoryLocation Absolute path to repository.
  90. * @param {String} branchName Branch name to pull.
  91. */
  92. pull( repositoryLocation, branchName ) {
  93. const checkoutCommands = [
  94. `cd ${ repositoryLocation }`,
  95. `git pull origin ${ branchName }`
  96. ];
  97. tools.shExec( checkoutCommands.join( ' && ' ) );
  98. },
  99. /**
  100. * Initializes new repository, adds and merges CKEditor5 boilerplate project.
  101. *
  102. * @param {String} repositoryPath Absolute path where repository should be created.
  103. */
  104. initializeRepository( repositoryPath ) {
  105. const initializeCommands = [
  106. `git init ${ repositoryPath }`,
  107. `cd ${ repositoryPath }`,
  108. `git remote add boilerplate ${ this.BOILERPLATE_REPOSITORY }`,
  109. `git fetch boilerplate ${ this.BOILERPLATE_BRANCH }`,
  110. `git merge boilerplate/${ this.BOILERPLATE_BRANCH }`
  111. ];
  112. tools.shExec( initializeCommands.join( ' && ' ) );
  113. },
  114. /**
  115. * Returns Git status of repository stored under specified path. It runs `git status --porcelain -sb` command.
  116. *
  117. * @param {String} repositoryPath Absolute path to repository.
  118. * @returns {String} Executed command's result.
  119. */
  120. getStatus( repositoryPath ) {
  121. return tools.shExec( `cd ${ repositoryPath } && git status --porcelain -sb` );
  122. },
  123. /**
  124. * Updates boilerplate project in specified repository.
  125. * @param {String} repositoryPath Absolute path to repository.
  126. */
  127. updateBoilerplate( repositoryPath ) {
  128. const regexp = /boilerplate(\n|$)/;
  129. // Try to add boilerplate remote if one is not already added.
  130. if ( !regexp.test( tools.shExec( `cd ${ repositoryPath } && git remote` ) ) ) {
  131. tools.shExec( `cd ${ repositoryPath } && git remote add boilerplate ${ this.BOILERPLATE_REPOSITORY }` );
  132. }
  133. const updateCommands = [
  134. `cd ${ repositoryPath }`,
  135. `git fetch boilerplate ${ this.BOILERPLATE_BRANCH }`,
  136. `git merge boilerplate/${ this.BOILERPLATE_BRANCH }`
  137. ];
  138. tools.shExec( updateCommands.join( ' && ' ) );
  139. },
  140. /**
  141. * Creates initial commit on repository under specified path.
  142. *
  143. * @param {String} pluginName
  144. * @param {String} repositoryPath
  145. */
  146. initialCommit( pluginName, repositoryPath ) {
  147. const commitCommands = [
  148. `cd ${ repositoryPath }`,
  149. `git add .`,
  150. `git commit -m "Initial commit for ${ pluginName }."`
  151. ];
  152. tools.shExec( commitCommands.join( ' && ' ) );
  153. }
  154. };