|
| 1 | +importtempyfrom'tempy'; |
| 2 | +importfileUrlfrom'file-url'; |
| 3 | +importexecafrom'execa'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Create a temporary git repository and change the current working directory to the repository root. |
| 7 | + * |
| 8 | + *@return {string} The path of the repository. |
| 9 | + */ |
| 10 | +exportasyncfunctiongitRepo(){ |
| 11 | +constdir=tempy.directory(); |
| 12 | + |
| 13 | +process.chdir(dir); |
| 14 | +awaitexeca('git',['init']); |
| 15 | +awaitgitCheckout('master'); |
| 16 | +returndir; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Checkout a branch on the current git repository. |
| 21 | + * |
| 22 | + *@param {string} branch Branch name. |
| 23 | + *@param {boolean} create `true` to create the branche ans switch, `false` to only switch. |
| 24 | + */ |
| 25 | +exportasyncfunctiongitCheckout(branch,create=true){ |
| 26 | +awaitexeca('git',create ?['checkout','-b',branch] :['checkout',branch]); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Create commit on the current git repository. |
| 31 | + * |
| 32 | + *@param {String} message commit message. |
| 33 | + * |
| 34 | + *@returns {Commit} The created commits. |
| 35 | + */ |
| 36 | +exportasyncfunctiongitCommit(message){ |
| 37 | +const{stdout}=awaitexeca('git',['commit','-m',message,'--allow-empty','--no-gpg-sign']); |
| 38 | +const[,branch,hash]=/^\[(\w+)\(?.*?\)?(\w+)\].+(?:\n|$)/.exec(stdout); |
| 39 | +return{branch, hash, message}; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Create a tag on the head commit in the current git repository. |
| 44 | + * |
| 45 | + *@param {string} tagName The tag name to create. |
| 46 | + *@param {string} [sha] The commit on which to create the tag. If undefined the tag is created on the last commit. |
| 47 | + * |
| 48 | + *@return {string} The commit sha of the created tag. |
| 49 | + */ |
| 50 | +exportasyncfunctiongitTagVersion(tagName,sha){ |
| 51 | +awaitexeca('git',sha ?['tag','-f',tagName,sha] :['tag',tagName]); |
| 52 | +return(awaitexeca('git',['rev-list','-1','--tags',tagName])).stdout; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Create a shallow clone of a git repository and change the current working directory to the cloned repository root. |
| 57 | + * The shallow will contain a limited number of commit and no tags. |
| 58 | + * |
| 59 | + *@param {string} origin The path of the repository to clone. |
| 60 | + *@param {number} [depth=1] The number of commit to clone. |
| 61 | + *@return {string} The path of the cloned repository. |
| 62 | + */ |
| 63 | +exportasyncfunctiongitShallowClone(origin,branch='master',depth=1){ |
| 64 | +constdir=tempy.directory(); |
| 65 | + |
| 66 | +process.chdir(dir); |
| 67 | +awaitexeca('git',['clone','--no-hardlinks','--no-tags','-b',branch,'--depth',depth,fileUrl(origin),dir]); |
| 68 | +returndir; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + *@return {Array<string>} The list of commit sha from the current git repository. |
| 73 | + */ |
| 74 | +exportasyncfunctiongitLog(){ |
| 75 | +return(awaitexeca('git',['log','--format=format:%H'])).stdout.split('\n').filter(sha=>Boolean(sha)); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Pack heads and tags of the current git repository. |
| 80 | + */ |
| 81 | +exportasyncfunctiongitPackRefs(){ |
| 82 | +awaitexeca('git',['pack-refs','--all']); |
| 83 | +} |