workspace-tools
Advanced tools
Comparing version 0.11.0 to 0.12.0
@@ -5,3 +5,18 @@ { | ||
{ | ||
"date": "Wed, 27 Jan 2021 20:58:44 GMT", | ||
"date": "Tue, 16 Feb 2021 22:41:08 GMT", | ||
"tag": "workspace-tools_v0.12.0", | ||
"version": "0.12.0", | ||
"comments": { | ||
"minor": [ | ||
{ | ||
"comment": "adding missing git functions into workspace-tools from beachball repo", | ||
"author": "kchau@microsoft.com", | ||
"commit": "d6ebe4def291fdf7abb590a4762c964a3e3033a8", | ||
"package": "workspace-tools" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"date": "Wed, 27 Jan 2021 20:58:49 GMT", | ||
"tag": "workspace-tools_v0.11.0", | ||
@@ -8,0 +23,0 @@ "version": "0.11.0", |
# Change Log - workspace-tools | ||
This log was last generated on Wed, 27 Jan 2021 20:58:44 GMT and should not be manually modified. | ||
This log was last generated on Tue, 16 Feb 2021 22:41:08 GMT and should not be manually modified. | ||
<!-- Start content --> | ||
## 0.12.0 | ||
Tue, 16 Feb 2021 22:41:08 GMT | ||
### Minor changes | ||
- adding missing git functions into workspace-tools from beachball repo (kchau@microsoft.com) | ||
## 0.11.0 | ||
Wed, 27 Jan 2021 20:58:44 GMT | ||
Wed, 27 Jan 2021 20:58:49 GMT | ||
@@ -11,0 +19,0 @@ ### Minor changes |
@@ -0,2 +1,13 @@ | ||
declare type ProcessOutput = { | ||
stderr: string; | ||
stdout: string; | ||
success: boolean; | ||
}; | ||
declare type GitObserver = (args: string[], output: ProcessOutput) => void; | ||
/** | ||
* Adds an observer for the git operations, e.g. for testing | ||
* @param observer | ||
*/ | ||
export declare function addGitObserver(observer: GitObserver): void; | ||
/** | ||
* Runs git command - use this for read only commands | ||
@@ -6,8 +17,3 @@ */ | ||
cwd: string; | ||
maxBuffer?: number; | ||
}): { | ||
stderr: string; | ||
stdout: string; | ||
success: boolean; | ||
}; | ||
}): ProcessOutput; | ||
/** | ||
@@ -27,2 +33,3 @@ * Runs git command - use this for commands that makes changes to the file system | ||
export declare function getUnstagedChanges(cwd: string): string[] | undefined; | ||
export declare function getChanges(branch: string, cwd: string): string[] | undefined; | ||
/** | ||
@@ -34,6 +41,3 @@ * Gets all the changes between the branch and the merge-base | ||
export declare function getBranchChanges(branch: string, cwd: string): string[]; | ||
/** | ||
* Gets all the staged changes (changes inside the index) | ||
* @param cwd | ||
*/ | ||
export declare function getChangesBetweenRefs(fromRef: string, toRef: string, options: string[], pattern: string, cwd: string): string[] | undefined; | ||
export declare function getStagedChanges(cwd: string): string[] | undefined; | ||
@@ -51,2 +55,4 @@ export declare function getRecentCommitMessages(branch: string, cwd: string): string[] | undefined; | ||
export declare function init(cwd: string, email?: string, username?: string): void; | ||
export declare function stage(patterns: string[], cwd: string): void; | ||
export declare function commit(message: string, cwd: string): void; | ||
export declare function stageAndCommit(patterns: string[], message: string, cwd: string): void; | ||
@@ -63,1 +69,2 @@ export declare function revertLocalChanges(cwd: string): boolean; | ||
export declare function listAllTrackedFiles(patterns: string[], cwd: string): string[]; | ||
export {}; |
129
lib/git.js
@@ -17,13 +17,20 @@ "use strict"; | ||
const MaxBufferOption = process.env.GIT_MAX_BUFFER ? parseInt(process.env.GIT_MAX_BUFFER) : 500 * 1024 * 1024; | ||
const observers = []; | ||
let observing; | ||
/** | ||
* Adds an observer for the git operations, e.g. for testing | ||
* @param observer | ||
*/ | ||
function addGitObserver(observer) { | ||
observers.push(observer); | ||
} | ||
exports.addGitObserver = addGitObserver; | ||
/** | ||
* Runs git command - use this for read only commands | ||
*/ | ||
function git(args, options) { | ||
const results = child_process_1.spawnSync("git", args, Object.assign({ maxBuffer: MaxBufferOption }, options)); | ||
// These errors are caused by the spawning itself | ||
if (results.error) { | ||
throw new Error(`git command failed: ${args.join(' ')}\n${results.error}`); | ||
} | ||
const results = child_process_1.spawnSync("git", args, options); | ||
let output; | ||
if (results.status === 0) { | ||
return { | ||
output = { | ||
stderr: results.stderr.toString().trimRight(), | ||
@@ -35,3 +42,3 @@ stdout: results.stdout.toString().trimRight(), | ||
else { | ||
return { | ||
output = { | ||
stderr: results.stderr.toString().trimRight(), | ||
@@ -42,2 +49,11 @@ stdout: results.stdout.toString().trimRight(), | ||
} | ||
// notify observers, flipping the observing bit to prevent infinite loops | ||
if (!observing) { | ||
observing = true; | ||
for (const observer of observers) { | ||
observer(args, output); | ||
} | ||
observing = false; | ||
} | ||
return output; | ||
} | ||
@@ -100,14 +116,3 @@ exports.git = git; | ||
try { | ||
const results = git(["--no-pager", "diff", "--name-only", "--relative"], { | ||
cwd, | ||
}); | ||
if (!results.success) { | ||
return []; | ||
} | ||
let changes = results.stdout; | ||
let lines = changes.split(/\n/) || []; | ||
return lines | ||
.filter((line) => line.trim() !== "") | ||
.map((line) => line.trim()) | ||
.filter((line) => !line.includes("node_modules")); | ||
return processGitOutput(git(["--no-pager", "diff", "--name-only", "--relative"], { cwd })); | ||
} | ||
@@ -119,2 +124,11 @@ catch (e) { | ||
exports.getUnstagedChanges = getUnstagedChanges; | ||
function getChanges(branch, cwd) { | ||
try { | ||
return processGitOutput(git(["--no-pager", "diff", "--relative", "--name-only", branch + "..."], { cwd })); | ||
} | ||
catch (e) { | ||
console.error("Cannot gather information about changes: ", e.message); | ||
} | ||
} | ||
exports.getChanges = getChanges; | ||
/** | ||
@@ -126,35 +140,15 @@ * Gets all the changes between the branch and the merge-base | ||
function getBranchChanges(branch, cwd) { | ||
const diffCmd = ["--no-pager", "diff", "--name-only", "--relative", branch + "..."]; | ||
const results = git(diffCmd, { | ||
cwd, | ||
}); | ||
if (!results.success) { | ||
throw new Error(results.stderr); | ||
try { | ||
return processGitOutput(git(["--no-pager", "diff", "--name-only", "--relative", branch + "..."], { cwd })); | ||
} | ||
let changes = results.stdout; | ||
let lines = changes.split(/\n/) || []; | ||
return lines | ||
.filter((line) => line.trim() !== "") | ||
.map((line) => line.trim()) | ||
.filter((line) => !line.includes("node_modules")); | ||
catch (e) { | ||
throw new Error(e.message); | ||
} | ||
} | ||
exports.getBranchChanges = getBranchChanges; | ||
/** | ||
* Gets all the staged changes (changes inside the index) | ||
* @param cwd | ||
*/ | ||
function getStagedChanges(cwd) { | ||
function getChangesBetweenRefs(fromRef, toRef, options, pattern, cwd) { | ||
try { | ||
const results = git(["--no-pager", "diff", "--staged", "--name-only", "--relative"], { | ||
return processGitOutput(git(["--no-pager", "diff", "--relative", "--name-only", ...options, `${fromRef}...${toRef}`, "--", pattern], { | ||
cwd, | ||
}); | ||
if (!results.success) { | ||
return []; | ||
} | ||
let changes = results.stdout; | ||
let lines = changes.split(/\n/) || []; | ||
return lines | ||
.filter((line) => line.trim() !== "") | ||
.map((line) => line.trim()) | ||
.filter((line) => !line.includes("node_modules")); | ||
})); | ||
} | ||
@@ -165,2 +159,11 @@ catch (e) { | ||
} | ||
exports.getChangesBetweenRefs = getChangesBetweenRefs; | ||
function getStagedChanges(cwd) { | ||
try { | ||
return processGitOutput(git(["--no-pager", "diff", "--relative", "--staged", "--name-only"], { cwd })); | ||
} | ||
catch (e) { | ||
console.error("Cannot gather information about changes: ", e.message); | ||
} | ||
} | ||
exports.getStagedChanges = getStagedChanges; | ||
@@ -267,3 +270,3 @@ function getRecentCommitMessages(branch, cwd) { | ||
exports.init = init; | ||
function stageAndCommit(patterns, message, cwd) { | ||
function stage(patterns, cwd) { | ||
try { | ||
@@ -273,2 +276,10 @@ patterns.forEach((pattern) => { | ||
}); | ||
} | ||
catch (e) { | ||
console.error("Cannot stage changes", e.message); | ||
} | ||
} | ||
exports.stage = stage; | ||
function commit(message, cwd) { | ||
try { | ||
const commitResults = git(["commit", "-m", message], { cwd }); | ||
@@ -282,5 +293,10 @@ if (!commitResults.success) { | ||
catch (e) { | ||
console.error("Cannot stage and commit changes", e.message); | ||
console.error("Cannot commit changes", e.message); | ||
} | ||
} | ||
exports.commit = commit; | ||
function stageAndCommit(patterns, message, cwd) { | ||
stage(patterns, cwd); | ||
commit(message, cwd); | ||
} | ||
exports.stageAndCommit = stageAndCommit; | ||
@@ -313,5 +329,3 @@ function revertLocalChanges(cwd) { | ||
const showBranchLines = showBranchResult.stdout.split(/\n/); | ||
const parentLine = showBranchLines.find((line) => line.indexOf("*") > -1 && | ||
line.indexOf(branchName) < 0 && | ||
line.indexOf("publish_") < 0); | ||
const parentLine = showBranchLines.find((line) => line.indexOf("*") > -1 && line.indexOf(branchName) < 0 && line.indexOf("publish_") < 0); | ||
if (!parentLine) { | ||
@@ -410,1 +424,12 @@ return null; | ||
exports.listAllTrackedFiles = listAllTrackedFiles; | ||
function processGitOutput(output) { | ||
if (!output.success) { | ||
return []; | ||
} | ||
let stdout = output.stdout; | ||
let lines = stdout.split(/\n/) || []; | ||
return lines | ||
.filter((line) => line.trim() !== "") | ||
.map((line) => line.trim()) | ||
.filter((line) => !line.includes("node_modules")); | ||
} |
{ | ||
"name": "workspace-tools", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99898
80
2325