feat: add rename branch functionality with UI integration
This commit is contained in:
@@ -3818,6 +3818,30 @@ async function main(options = {}) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.put('/api/git/branches/rename', async (req, res) => {
|
||||
const { renameBranch } = await getGitLibraries();
|
||||
try {
|
||||
const directory = req.query.directory;
|
||||
if (!directory) {
|
||||
return res.status(400).json({ error: 'directory parameter is required' });
|
||||
}
|
||||
|
||||
const { oldName, newName } = req.body;
|
||||
if (!oldName) {
|
||||
return res.status(400).json({ error: 'oldName is required' });
|
||||
}
|
||||
if (!newName) {
|
||||
return res.status(400).json({ error: 'newName is required' });
|
||||
}
|
||||
|
||||
const result = await renameBranch(directory, oldName, newName);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error('Failed to rename branch:', error);
|
||||
res.status(500).json({ error: error.message || 'Failed to rename branch' });
|
||||
}
|
||||
});
|
||||
app.delete('/api/git/remote-branches', async (req, res) => {
|
||||
const { deleteRemoteBranch } = await getGitLibraries();
|
||||
try {
|
||||
|
||||
@@ -29,6 +29,22 @@ const normalizeDirectoryPath = (value) => {
|
||||
return trimmed;
|
||||
};
|
||||
|
||||
const cleanBranchName = (branch) => {
|
||||
if (!branch) {
|
||||
return branch;
|
||||
}
|
||||
if (branch.startsWith('refs/heads/')) {
|
||||
return branch.substring('refs/heads/'.length);
|
||||
}
|
||||
if (branch.startsWith('heads/')) {
|
||||
return branch.substring('heads/'.length);
|
||||
}
|
||||
if (branch.startsWith('refs/')) {
|
||||
return branch.substring('refs/'.length);
|
||||
}
|
||||
return branch;
|
||||
};
|
||||
|
||||
export async function isGitRepository(directory) {
|
||||
const directoryPath = normalizeDirectoryPath(directory);
|
||||
if (!directoryPath || !fs.existsSync(directoryPath)) {
|
||||
@@ -805,7 +821,7 @@ export async function getWorktrees(directory) {
|
||||
} else if (line.startsWith('HEAD ')) {
|
||||
current.head = line.substring(5);
|
||||
} else if (line.startsWith('branch ')) {
|
||||
current.branch = line.substring(7);
|
||||
current.branch = cleanBranchName(line.substring(7));
|
||||
} else if (line === '') {
|
||||
if (current.worktree) {
|
||||
worktrees.push(current);
|
||||
@@ -1086,3 +1102,16 @@ export async function getCommitFiles(directory, commitHash) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function renameBranch(directory, oldName, newName) {
|
||||
const git = simpleGit(normalizeDirectoryPath(directory));
|
||||
|
||||
try {
|
||||
// Use git branch -m command to rename the branch
|
||||
await git.raw(['branch', '-m', oldName, newName]);
|
||||
return { success: true, branch: newName };
|
||||
} catch (error) {
|
||||
console.error('Failed to rename branch:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user