create git client and sample usage

This commit is contained in:
Tejas Srikanth 2023-03-18 17:34:10 -04:00
parent 6505cde2db
commit c4421bcd9f
4 changed files with 118 additions and 40 deletions

104
package-lock.json generated
View File

@ -17,7 +17,8 @@
"pm2": "^5.2.2",
"rimraf": "^3.0.2",
"typeorm": "^0.3.11",
"typescript": "^4.8.3"
"typescript": "^4.8.3",
"uuidv4": "^6.2.13"
},
"devDependencies": {
"@types/jest": "^29.2.5",
@ -1046,6 +1047,15 @@
"node": ">=6.0"
}
},
"node_modules/@opencensus/core/node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
"bin": {
"uuid": "bin/uuid"
}
},
"node_modules/@opencensus/propagation-b3": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/@opencensus/propagation-b3/-/propagation-b3-0.0.8.tgz",
@ -1073,6 +1083,15 @@
"node": ">=6.0"
}
},
"node_modules/@opencensus/propagation-b3/node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
"bin": {
"uuid": "bin/uuid"
}
},
"node_modules/@pm2/agent": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@pm2/agent/-/agent-2.0.1.tgz",
@ -1457,6 +1476,11 @@
"@types/superagent": "*"
}
},
"node_modules/@types/uuid": {
"version": "8.3.4",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz",
"integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw=="
},
"node_modules/@types/yargs": {
"version": "17.0.22",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.22.tgz",
@ -6995,14 +7019,6 @@
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz",
"integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg=="
},
"node_modules/typeorm/node_modules/uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/typescript": {
"version": "4.9.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
@ -7078,12 +7094,28 @@
}
},
"node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==",
"bin": {
"uuid": "bin/uuid"
"uuid": "dist/bin/uuid"
}
},
"node_modules/uuidv4": {
"version": "6.2.13",
"resolved": "https://registry.npmjs.org/uuidv4/-/uuidv4-6.2.13.tgz",
"integrity": "sha512-AXyzMjazYB3ovL3q051VLH06Ixj//Knx7QnUSi1T//Ie3io6CpsPu9nVMOx5MoLWh6xV0B9J0hIaxungxXUbPQ==",
"dependencies": {
"@types/uuid": "8.3.4",
"uuid": "8.3.2"
}
},
"node_modules/uuidv4/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/v8-to-istanbul": {
@ -8129,6 +8161,13 @@
"semver": "^5.5.0",
"shimmer": "^1.2.0",
"uuid": "^3.2.1"
},
"dependencies": {
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
}
}
},
"@opencensus/propagation-b3": {
@ -8151,6 +8190,11 @@
"shimmer": "^1.2.0",
"uuid": "^3.2.1"
}
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
}
}
},
@ -8490,6 +8534,11 @@
"@types/superagent": "*"
}
},
"@types/uuid": {
"version": "8.3.4",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz",
"integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw=="
},
"@types/yargs": {
"version": "17.0.22",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.22.tgz",
@ -12548,11 +12597,6 @@
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz",
"integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg=="
},
"uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg=="
}
}
},
@ -12599,9 +12643,25 @@
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA=="
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg=="
},
"uuidv4": {
"version": "6.2.13",
"resolved": "https://registry.npmjs.org/uuidv4/-/uuidv4-6.2.13.tgz",
"integrity": "sha512-AXyzMjazYB3ovL3q051VLH06Ixj//Knx7QnUSi1T//Ie3io6CpsPu9nVMOx5MoLWh6xV0B9J0hIaxungxXUbPQ==",
"requires": {
"@types/uuid": "8.3.4",
"uuid": "8.3.2"
},
"dependencies": {
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
}
}
},
"v8-to-istanbul": {
"version": "9.1.0",

View File

@ -23,7 +23,8 @@
"pm2": "^5.2.2",
"rimraf": "^3.0.2",
"typeorm": "^0.3.11",
"typescript": "^4.8.3"
"typescript": "^4.8.3",
"uuidv4": "^6.2.13"
},
"devDependencies": {
"@types/jest": "^29.2.5",

View File

@ -3,18 +3,23 @@ import { tmpdir } from "os";
import { join } from "path";
const { exec } = require("child_process");
const {uuid} = require("uuidv4")
function checkError(error){
if (error) {
console.log(`error: ${error.message}`);
return;
}
}
export function cloneRepo(httpAddr, callback){
mkdtemp(join(tmpdir(), 'dt-'), (err, folder) => {
if (err) throw err;
var fString = folder;
exec("git clone " + httpAddr + " " + fString, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
checkError(error)
console.log(`stdout: ${stderr}`);
createAndPublishNewBranch(fString);
callback(fString);
return fString;
})
@ -23,28 +28,28 @@ export function cloneRepo(httpAddr, callback){
export function pushRepo(repoPath){
exec("git -C " + repoPath + " push", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
checkError(error)
console.log(`stdout: ${stderr}`);
})
}
export function commitRepo(repoPath){
exec("git -C " + repoPath + " add .", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
console.log(`stdout: ${stderr}`);
checkError(error)
exec("git -C " + repoPath + " commit -m \"generic commit message\"", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
checkError(error)
console.log(`stdout: ${stderr}`);
})
})
}
function createAndPublishNewBranch(repoPath){
var id = uuid();
exec("git -C " + repoPath + " checkout -b " + id + " && git -C " + repoPath + " push -u origin "+ id, (error, stdout, stderr) => {
checkError(error)
console.log(`stdout: ${stderr}`);
return id;
}
)
}

12
src/useGitClient.ts Normal file
View File

@ -0,0 +1,12 @@
import { exec } from 'child_process';
import { SimpleConsoleLogger } from 'typeorm'
import {cloneRepo, commitRepo, pushRepo} from './gitClient'
//Sample usage
cloneRepo('https://git.csclub.uwaterloo.ca/www/library.git', (temporaryDir) => {
//temporaryDir is the temp directory name
// exec("touch "+ temporaryDir + "/thing.js"); (add a new file to the directory)
commitRepo(temporaryDir); // commit to the directory
pushRepo(temporaryDir); // push to the directory
});