glu-cli
Version:
Git stacked branch management with GitHub integration
91 lines • 2.86 kB
JavaScript
import { GitError, GitErrorType } from "../core/errors/git-error.js";
export class BranchService {
git;
constructor(git) {
this.git = git;
}
// MARK: Branch management
async getCurrentBranch() {
const name = await this.git.getCurrentBranch();
const upstream = await this.git.getUpstreamBranch(name);
return {
name,
isRemote: false,
upstream,
};
}
async getUpstream(branch) {
try {
return await this.git.getUpstreamBranch(branch);
}
catch {
return null;
}
}
async createTempBranch(prefix) {
const timestamp = Date.now();
const branchName = `${prefix}/${timestamp}`;
const currentBranch = await this.git.getCurrentBranch();
const upstream = await this.git.getUpstreamBranch(currentBranch);
await this.git.createBranchFrom(branchName, upstream);
return branchName;
}
async createBranchFrom(newBranch, sourceBranch, force = false) {
try {
await this.git.createBranchFrom(newBranch, sourceBranch, force);
}
catch (error) {
if (error.message?.includes("already exists")) {
throw new GitError(GitErrorType.BRANCH_ALREADY_EXISTS, {
branchName: newBranch,
sourceBranch,
});
}
throw new GitError(GitErrorType.BRANCH_CREATION_FAILED, {
branchName: newBranch,
sourceBranch,
originalError: error.message,
});
}
}
async deleteBranch(name) {
await this.git.deleteLocalBranch(name, true);
}
async push(branch, remote, force) {
try {
const options = force ? ["--force"] : [];
return await this.git.push(remote, branch, options);
}
catch (error) {
if (error.message?.includes("non-fast-forward") ||
error.message?.includes("rejected")) {
throw new GitError(GitErrorType.PUSH_REJECTED, {
branch,
remote,
reason: "non-fast-forward",
originalError: error.message,
});
}
throw error;
}
}
async checkout(branch) {
await this.git.checkout(branch);
}
// MARK: Branch queries
async exists(name) {
return await this.git.branchExists(name);
}
async getBranch(name) {
const exists = await this.exists(name);
if (!exists)
return null;
const upstream = await this.getUpstream(name);
return {
name,
isRemote: false,
upstream,
};
}
}
//# sourceMappingURL=branch-service.js.map