Skip to content

Commit 271a68b

Browse files
committed
Update Gitblit reflog on branch delete from UI if Gitblit has an existing reflog
1 parent a5ae3da commit 271a68b

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

releases.moxie

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ r18: {
2222
- Fixed Ubuntu service script for LSB compliance
2323
- Inserted "sleep 5" in Ubuntu & Centos bash script for service restart
2424
changes:
25+
- Use trash icon in Gitblit Reflog for branch and tag deletion
26+
- Update Gitblit Reflog on branch deletion from web UI
2527
- updated Chinese translation
2628
- updated Dutch translation
2729
- updated Spanish translation

src/main/java/com/gitblit/utils/RefLogUtils.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.text.MessageFormat;
2121
import java.text.SimpleDateFormat;
2222
import java.util.ArrayList;
23+
import java.util.Arrays;
2324
import java.util.Collection;
2425
import java.util.Collections;
2526
import java.util.Date;
@@ -41,6 +42,7 @@
4142
import org.eclipse.jgit.lib.ObjectId;
4243
import org.eclipse.jgit.lib.ObjectInserter;
4344
import org.eclipse.jgit.lib.PersonIdent;
45+
import org.eclipse.jgit.lib.Ref;
4446
import org.eclipse.jgit.lib.RefRename;
4547
import org.eclipse.jgit.lib.RefUpdate;
4648
import org.eclipse.jgit.lib.RefUpdate.Result;
@@ -96,6 +98,21 @@ private static void error(Throwable t, Repository repository, String pattern, Ob
9698
}
9799
LOGGER.error(MessageFormat.format(pattern, parameters.toArray()), t);
98100
}
101+
102+
/**
103+
* Returns true if the repository has a reflog branch.
104+
*
105+
* @param repository
106+
* @return true if the repository has a reflog branch
107+
*/
108+
public static boolean hasRefLogBranch(Repository repository) {
109+
try {
110+
return repository.getRef(GB_REFLOG) != null;
111+
} catch(Exception e) {
112+
LOGGER.error("failed to determine hasRefLogBranch", e);
113+
}
114+
return false;
115+
}
99116

100117
/**
101118
* Returns a RefModel for the reflog branch in the repository. If the
@@ -155,6 +172,37 @@ private static UserModel newUserModelFrom(PersonIdent ident) {
155172
return user;
156173
}
157174

175+
/**
176+
* Logs a ref deletion.
177+
*
178+
* @param user
179+
* @param repository
180+
* @param ref
181+
* @return true, if the update was successful
182+
*/
183+
public static boolean deleteRef(UserModel user, Repository repository, String ref) {
184+
try {
185+
Ref refObj = repository.getRef(ref);
186+
if (refObj == null && !ref.startsWith(Constants.R_HEADS) && ref.startsWith(Constants.R_TAGS)) {
187+
// find fully qualified ref
188+
refObj = repository.getRef(Constants.R_HEADS + ref);
189+
if (refObj == null) {
190+
refObj = repository.getRef(Constants.R_TAGS + ref);
191+
}
192+
}
193+
194+
if (refObj == null) {
195+
return false;
196+
}
197+
198+
ReceiveCommand cmd = new ReceiveCommand(refObj.getObjectId(), ObjectId.zeroId(), refObj.getName());
199+
return updateRefLog(user, repository, Arrays.asList(cmd));
200+
} catch (Throwable t) {
201+
error(t, repository, "Failed to commit reflog entry to {0}");
202+
}
203+
return false;
204+
}
205+
158206
/**
159207
* Updates the reflog with the received commands.
160208
*

src/main/java/com/gitblit/wicket/panels/BranchesPanel.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import com.gitblit.models.RefModel;
3838
import com.gitblit.models.RepositoryModel;
3939
import com.gitblit.models.UserModel;
40+
import com.gitblit.utils.CommitCache;
4041
import com.gitblit.utils.JGitUtils;
42+
import com.gitblit.utils.RefLogUtils;
4143
import com.gitblit.utils.StringUtils;
4244
import com.gitblit.wicket.GitBlitWebSession;
4345
import com.gitblit.wicket.WicketUtils;
@@ -191,15 +193,28 @@ public void onClick() {
191193
}
192194
return;
193195
}
194-
boolean success = JGitUtils.deleteBranchRef(r, entry.getName());
196+
final String branch = entry.getName();
197+
boolean success = JGitUtils.deleteBranchRef(r, branch);
198+
if (success) {
199+
// clear commit cache
200+
CommitCache.instance().clear(repositoryModel.name, branch);
201+
202+
// optionally update reflog
203+
if (RefLogUtils.hasRefLogBranch(r)) {
204+
UserModel user = GitBlitWebSession.get().getUser();
205+
success = RefLogUtils.deleteRef(user, r, branch);
206+
}
207+
}
208+
195209
r.close();
210+
196211
if (success) {
197-
info(MessageFormat.format("Branch \"{0}\" deleted", entry.displayName));
212+
info(MessageFormat.format("Branch \"{0}\" deleted", branch));
198213
// redirect to the owning page
199214
setResponsePage(getPage().getClass(), WicketUtils.newRepositoryParameter(repositoryModel.name));
200215
}
201216
else {
202-
error(MessageFormat.format("Failed to delete branch \"{0}\"", entry.displayName));
217+
error(MessageFormat.format("Failed to delete branch \"{0}\"", branch));
203218
}
204219
}
205220
};

src/main/java/com/gitblit/wicket/panels/ReflogPanel.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.wicket.markup.repeater.data.ListDataProvider;
3131
import org.apache.wicket.model.StringResourceModel;
3232
import org.eclipse.jgit.lib.Repository;
33+
import org.eclipse.jgit.transport.ReceiveCommand.Type;
3334

3435
import com.gitblit.Constants;
3536
import com.gitblit.GitBlit;
@@ -154,7 +155,9 @@ public void populateItem(final Item<RefLogEntry> changeItem) {
154155
changeItem.add(new Label("whenChanged", fuzzydate + ", " + df.format(changeDate)));
155156

156157
Label changeIcon = new Label("changeIcon");
157-
if (isTag) {
158+
if (Type.DELETE.equals(change.getChangeType(fullRefName))) {
159+
WicketUtils.setCssClass(changeIcon, "iconic-trash-stroke");
160+
} else if (isTag) {
158161
WicketUtils.setCssClass(changeIcon, "iconic-tag");
159162
} else {
160163
WicketUtils.setCssClass(changeIcon, "iconic-upload");

0 commit comments

Comments
 (0)