git.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const tools = require( './tools' );
  7. const defaultOrigin = 'origin';
  8. module.exports = {
  9. /**
  10. * Parses GitHub URL. Extracts used server, repository and branch.
  11. *
  12. * @param {String} url GitHub URL from package.json file.
  13. * @returns {Object} urlInfo
  14. * @returns {String} urlInfo.server
  15. * @returns {String} urlInfo.repository
  16. * @returns {String} urlInfo.user
  17. * @returns {String} urlInfo.name
  18. * @returns {String} urlInfo.branch
  19. */
  20. parseRepositoryUrl( url ) {
  21. const regexp = /^((?:git@|(?:http[s]?|git):\/\/)github\.com(?:\/|:))?(([\w-]+)\/([\w-]+(?:\.git)?))(?:#([\w-\/]+))?$/;
  22. const match = url.match( regexp );
  23. let server;
  24. let repository;
  25. let branch;
  26. let name;
  27. let user;
  28. if ( !match ) {
  29. return null;
  30. }
  31. server = match[ 1 ] || 'git@github.com:';
  32. repository = match[ 2 ];
  33. user = match[ 3 ];
  34. name = match[ 4 ];
  35. branch = match[ 5 ] || 'master';
  36. name = /\.git$/.test( name ) ? name.slice( 0, -4 ) : name;
  37. return {
  38. server: server,
  39. repository: repository,
  40. branch: branch,
  41. user: user,
  42. name: name
  43. };
  44. },
  45. /**
  46. * Clones repository to workspace.
  47. *
  48. * @param {Object} urlInfo Parsed URL object from {@link #parseRepositoryUrl}.
  49. * @param {String} workspacePath Path to the workspace location where repository will be cloned.
  50. */
  51. cloneRepository( urlInfo, workspacePath ) {
  52. const cloneCommands = [
  53. `cd ${ workspacePath }`,
  54. `git clone ${ urlInfo.server + urlInfo.repository }`
  55. ];
  56. tools.shExec( cloneCommands.join( ' && ' ) );
  57. },
  58. /**
  59. * Checks out branch on selected repository.
  60. *
  61. * @param {String} repositoryLocation Absolute path to repository.
  62. * @param {String} branchName Name of the branch to checkout.
  63. */
  64. checkout( repositoryLocation, branchName ) {
  65. const checkoutCommands = [
  66. `cd ${ repositoryLocation }`,
  67. `git checkout ${ branchName }`
  68. ];
  69. tools.shExec( checkoutCommands.join( ' && ' ) );
  70. },
  71. /**
  72. * Pulls specified branch from origin.
  73. *
  74. * @param {String} repositoryLocation Absolute path to repository.
  75. * @param {String} branchName Branch name to pull.
  76. */
  77. pull( repositoryLocation, branchName ) {
  78. const pullCommands = [
  79. `cd ${ repositoryLocation }`,
  80. `git pull ${ defaultOrigin } ${ branchName }`
  81. ];
  82. tools.shExec( pullCommands.join( ' && ' ) );
  83. },
  84. /**
  85. * Fetch all branches from each origin on selected repository.
  86. *
  87. * @param {String} repositoryLocation
  88. */
  89. fetchAll( repositoryLocation ) {
  90. const fetchCommands = [
  91. `cd ${ repositoryLocation }`,
  92. `git fetch --all`
  93. ];
  94. tools.shExec( fetchCommands.join( ' && ' ) );
  95. },
  96. /**
  97. * Initializes new repository, adds and merges CKEditor5 boilerplate project.
  98. *
  99. * @param {String} repositoryPath Absolute path where repository should be created.
  100. */
  101. initializeRepository( repositoryPath ) {
  102. tools.shExec( `git init ${ repositoryPath }` );
  103. },
  104. /**
  105. * Returns Git status of repository stored under specified path. It runs `git status --porcelain -sb` command.
  106. *
  107. * @param {String} repositoryPath Absolute path to repository.
  108. * @returns {String} Executed command's result.
  109. */
  110. getStatus( repositoryPath ) {
  111. return tools.shExec( `cd ${ repositoryPath } && git status --porcelain -sb`, false );
  112. },
  113. /**
  114. * Creates initial commit on repository under specified path.
  115. *
  116. * @param {String} pluginName
  117. * @param {String} repositoryPath
  118. */
  119. initialCommit( pluginName, repositoryPath ) {
  120. const commitCommands = [
  121. `cd ${ repositoryPath }`,
  122. `git add .`,
  123. `git commit -m "Initial commit for ${ pluginName }."`
  124. ];
  125. tools.shExec( commitCommands.join( ' && ' ) );
  126. },
  127. /**
  128. * Adds remote to repository under specified path.
  129. *
  130. * @param {String} repositoryPath
  131. * @param {String} gitHubPath
  132. */
  133. addRemote( repositoryPath, gitHubPath ) {
  134. const addRemoteCommands = [
  135. `cd ${ repositoryPath }`,
  136. `git remote add ${ defaultOrigin } git@github.com:${ gitHubPath }.git`
  137. ];
  138. tools.shExec( addRemoteCommands.join( ' && ' ) );
  139. },
  140. /*
  141. * Creates commit on repository under specified path
  142. *
  143. * @param {String} message
  144. * @param {String} repositoryPath
  145. */
  146. commit( message, repositoryPath ) {
  147. const commitCommands = [
  148. `cd ${ repositoryPath }`,
  149. `git commit --all --message "${ message }"`
  150. ];
  151. tools.shExec( commitCommands.join( ' && ' ) );
  152. },
  153. /**
  154. * Pushes changes to repository's default location
  155. *
  156. * @param {String} repositoryPath
  157. */
  158. push( repositoryPath ) {
  159. const pushCommands = [
  160. `cd ${ repositoryPath }`,
  161. `git push`
  162. ];
  163. tools.shExec( pushCommands.join( ' && ' ) );
  164. }
  165. };