-
Notifications
You must be signed in to change notification settings - Fork 218
Fix draft file saving issue in genaiscript #1486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,6 @@ export class GitClient implements Git { | |
| readonly cwd: string | ||
| readonly git = "git" // Git command identifier | ||
| private _defaultBranch: string // Stores the default branch name | ||
| private _branch: string // Stores the current branch name | ||
|
|
||
| constructor(cwd: string) { | ||
| this.cwd = cwd || process.cwd() | ||
|
|
@@ -72,12 +71,14 @@ export class GitClient implements Git { | |
| * @returns {Promise<string>} The default branch name. | ||
| */ | ||
| async defaultBranch(): Promise<string> { | ||
| if (!this._defaultBranch) { | ||
| if (this._defaultBranch === undefined) { | ||
| dbg(`fetching default branch from remote`) | ||
| const res = await this.exec(["remote", "show", "origin"], {}) | ||
| this._defaultBranch = /^\s*HEAD branch:\s+(?<name>.+)\s*$/m.exec( | ||
| res | ||
| )?.groups?.name | ||
| const res = await this.exec(["remote", "show", "origin"], { | ||
| valueOnError: "", | ||
| }) | ||
| this._defaultBranch = | ||
| /^\s*HEAD branch:\s+(?<name>.+)\s*$/m.exec(res)?.groups?.name || | ||
| "" | ||
| } | ||
| return this._defaultBranch | ||
| } | ||
|
|
@@ -87,17 +88,16 @@ export class GitClient implements Git { | |
| * @returns | ||
| */ | ||
| async branch(): Promise<string> { | ||
| if (!this._branch) { | ||
| dbg(`fetching current branch`) | ||
| const res = await this.exec(["branch", "--show-current"]) | ||
| this._branch = res.trim() | ||
| } | ||
| return this._branch | ||
| dbg(`fetching current branch`) | ||
| const res = await this.exec(["branch", "--show-current"], { | ||
| valueOnError: "", | ||
| }) | ||
| return res.trim() | ||
| } | ||
|
|
||
| async listBranches(): Promise<string[]> { | ||
| dbg(`listing all branches`) | ||
| const res = await this.exec(["branch", "--list"]) | ||
| const res = await this.exec(["branch", "--list"], { valueOnError: "" }) | ||
| return res | ||
| .split("\n") | ||
| .map((b) => b.trim()) | ||
|
|
@@ -112,8 +112,9 @@ export class GitClient implements Git { | |
| */ | ||
| async exec( | ||
| args: string | string[], | ||
| options?: { label?: string } | ||
| options?: { label?: string; valueOnError?: string } | ||
| ): Promise<string> { | ||
| const { valueOnError } = options || {} | ||
| const opts: ShellOptions = { | ||
| ...(options || {}), | ||
| cwd: this.cwd, | ||
|
|
@@ -128,6 +129,7 @@ export class GitClient implements Git { | |
| if (res.stdout) dbg(res.stdout) | ||
| if (res.exitCode !== 0) { | ||
| dbg(`error: ${res.stderr}`) | ||
| if (valueOnError !== undefined) return valueOnError | ||
| throw new Error(res.stderr) | ||
| } | ||
| return res.stdout | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both 'defaultBranch()' and 'branch()', an error from git now returns "", which is indistinguishable from a valid empty branch name. This can silently mask runtime git errors (e.g. detached HEAD, no remote 'origin'). Consider properly handling or surfacing such errors. See: https://git-scm.com/docs/git-branch
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In 'exec()', the new 'valueOnError' option can silently swallow all git command errors, potentially leading to incorrect or inconsistent states if not carefully managed at every call site. Best practice is to fail fast on errors unless a very specific fallback is required and communicated to the caller. See: https://nodejs.org/api/errors.html#errors_propagation-and-interception
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The member '_defaultBranch' is marked as 'private' but is also accessed without accessor methods, and its usage changes from "undefined" to empty string. As a best practice, use 'private' members cautiously and prefer using getters/setters for consistency and encapsulation. See: https://www.typescriptlang.org/docs/handbook/classes.html#member-visibility