James Moger
2013-07-31 88ec32f91b3df15e063a2b2dcea3c90f3c65d835
Improvements to reflog branch deletion
3 files modified
77 ■■■■■ changed files
releases.moxie 1 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/utils/RefLogUtils.java 34 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/wicket/panels/BranchesPanel.java 42 ●●●●● patch | view | raw | blame | history
releases.moxie
@@ -12,6 +12,7 @@
    fixes:
    - Fixed Gitblit Authority startup failures when using alternate user services (issue-280)
    - Manually redirect after branch deletion (issue 282)
    - Improved branch deletion-reflog interaction
    changes: ~
    additions: ~
    dependencyChanges: ~
src/main/java/com/gitblit/utils/RefLogUtils.java
@@ -180,22 +180,22 @@
     * @param ref
     * @return true, if the update was successful
     */
    public static boolean deleteRef(UserModel user, Repository repository, String ref) {
    public static boolean deleteRef(UserModel user, Repository repository, Ref ref) {
        try {
            Ref refObj = repository.getRef(ref);
            if (refObj == null && !ref.startsWith(Constants.R_HEADS) && ref.startsWith(Constants.R_TAGS)) {
                // find fully qualified ref
                refObj = repository.getRef(Constants.R_HEADS + ref);
                if (refObj == null) {
                    refObj = repository.getRef(Constants.R_TAGS + ref);
                }
            if (ref == null) {
                return false;
            }
            if (refObj == null) {
            RefModel reflogBranch = getRefLogBranch(repository);
            if (reflogBranch == null) {
                return false;
            }
            
            ReceiveCommand cmd = new ReceiveCommand(refObj.getObjectId(), ObjectId.zeroId(), refObj.getName());
            List<RevCommit> log = JGitUtils.getRevLog(repository, reflogBranch.getName(), ref.getName(), 0, 1);
            if (log.isEmpty()) {
                // this ref is not in the reflog branch
                return false;
            }
            ReceiveCommand cmd = new ReceiveCommand(ref.getObjectId(), ObjectId.zeroId(), ref.getName());
            return updateRefLog(user, repository, Arrays.asList(cmd));
        } catch (Throwable t) {
            error(t, repository, "Failed to commit reflog entry to {0}");
@@ -442,9 +442,13 @@
            UserModel user = newUserModelFrom(push.getAuthorIdent());
            Date date = push.getAuthorIdent().getWhen();
            
            RefLogEntry log = new RefLogEntry(repositoryName, date, user);
            list.add(log);
            RefLogEntry log = new RefLogEntry(repositoryName, date, user);
            List<PathChangeModel> changedRefs = JGitUtils.getFilesInCommit(repository, push);
            if (changedRefs.isEmpty()) {
                // skip empty commits
                continue;
            }
            list.add(log);
            for (PathChangeModel change : changedRefs) {
                switch (change.changeType) {
                case DELETE:
@@ -458,6 +462,10 @@
                    String oldId = fields[1];
                    String newId = fields[2];
                    log.updateRef(change.path, ReceiveCommand.Type.valueOf(fields[0]), oldId, newId);
                    if (ObjectId.zeroId().getName().equals(newId)) {
                        // ref deletion
                        continue;
                    }
                    List<RevCommit> pushedCommits = JGitUtils.getRevLog(repository, oldId, newId);
                    for (RevCommit pushedCommit : pushedCommits) {
                        RepositoryCommit repoCommit = log.addCommit(change.path, pushedCommit);
src/main/java/com/gitblit/wicket/panels/BranchesPanel.java
@@ -199,28 +199,34 @@
                    return;
                }
                final String branch = entry.getName();
                boolean success = JGitUtils.deleteBranchRef(r, branch);
                if (success) {
                    // clear commit cache
                    CommitCache.instance().clear(repositoryModel.name, branch);
                Ref ref = null;
                try {
                    ref = r.getRef(branch);
                    if (ref == null && !branch.startsWith(Constants.R_HEADS)) {
                        ref = r.getRef(Constants.R_HEADS + branch);
                    }
                } catch (IOException e) {
                }
                if (ref != null) {
                    boolean success = JGitUtils.deleteBranchRef(r, ref.getName());
                    if (success) {
                        // clear commit cache
                        CommitCache.instance().clear(repositoryModel.name, branch);
                        // optionally update reflog
                        if (RefLogUtils.hasRefLogBranch(r)) {
                            UserModel user = GitBlitWebSession.get().getUser();
                            RefLogUtils.deleteRef(user, r, ref);
                        }
                    }
                    
                    // optionally update reflog
                    if (RefLogUtils.hasRefLogBranch(r)) {
                        UserModel user = GitBlitWebSession.get().getUser();
                        success = RefLogUtils.deleteRef(user, r, branch);
                    if (success) {
                        info(MessageFormat.format("Branch \"{0}\" deleted", branch));
                    } else {
                        error(MessageFormat.format("Failed to delete branch \"{0}\"", branch));
                    }
                }
                r.close();
                if (success) {
                    info(MessageFormat.format("Branch \"{0}\" deleted", branch));
                    // redirect to the owning page
                    setResponsePage(getPage().getClass(), WicketUtils.newRepositoryParameter(repositoryModel.name));
                }
                else {
                    error(MessageFormat.format("Failed to delete branch \"{0}\"", branch));
                }
                
                // redirect to the owning page
                PageParameters params = WicketUtils.newRepositoryParameter(repositoryModel.name);