1 /*******************************************************************************
2 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org
.eclipse
.egit
.core
.op
;
13 import org
.eclipse
.core
.resources
.IWorkspace
;
14 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
15 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
16 import org
.eclipse
.core
.runtime
.CoreException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.SubMonitor
;
19 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
20 import org
.eclipse
.egit
.core
.Activator
;
21 import org
.eclipse
.egit
.core
.internal
.CoreText
;
22 import org
.eclipse
.egit
.core
.internal
.job
.RuleUtil
;
23 import org
.eclipse
.jgit
.api
.Git
;
24 import org
.eclipse
.jgit
.api
.errors
.GitAPIException
;
25 import org
.eclipse
.jgit
.api
.errors
.JGitInternalException
;
26 import org
.eclipse
.jgit
.lib
.Ref
;
27 import org
.eclipse
.jgit
.lib
.Repository
;
28 import org
.eclipse
.osgi
.util
.NLS
;
31 * This class implements renaming of a branch
33 public class RenameBranchOperation
implements IEGitOperation
{
34 private final Repository repository
;
36 private final Ref branch
;
38 private final String newName
;
43 * the branch to rename
47 public RenameBranchOperation(Repository repository
, Ref branch
,
49 this.repository
= repository
;
51 this.newName
= newName
;
55 public void execute(IProgressMonitor monitor
) throws CoreException
{
56 IWorkspaceRunnable action
= new IWorkspaceRunnable() {
59 public void run(IProgressMonitor actMonitor
) throws CoreException
{
60 String taskName
= NLS
.bind(
61 CoreText
.RenameBranchOperation_TaskName
, branch
63 SubMonitor progress
= SubMonitor
.convert(actMonitor
);
64 progress
.setTaskName(taskName
);
65 try (Git git
= new Git(repository
)) {
66 git
.branchRename().setOldName(
67 branch
.getName()).setNewName(newName
).call();
68 } catch (JGitInternalException e
) {
69 throw new CoreException(Activator
.error(e
.getMessage(), e
));
70 } catch (GitAPIException e
) {
71 throw new CoreException(Activator
.error(e
.getMessage(), e
));
75 // lock workspace to protect working tree changes
76 ResourcesPlugin
.getWorkspace().run(action
, getSchedulingRule(),
77 IWorkspace
.AVOID_UPDATE
, monitor
);
81 public ISchedulingRule
getSchedulingRule() {
82 return RuleUtil
.getRule(repository
);