diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/distrib/data/defaults.properties | 7 | ||||
-rw-r--r-- | src/main/java/com/gitblit/git/GitblitReceivePack.java | 243 | ||||
-rw-r--r-- | src/main/java/com/gitblit/git/PatchsetReceivePack.java | 333 | ||||
-rw-r--r-- | src/main/java/com/gitblit/models/TicketModel.java | 218 | ||||
-rw-r--r-- | src/main/java/com/gitblit/tickets/ITicketService.java | 103 | ||||
-rw-r--r-- | src/main/java/com/gitblit/tickets/TicketNotifier.java | 13 | ||||
-rw-r--r-- | src/main/java/com/gitblit/utils/JGitUtils.java | 171 | ||||
-rw-r--r-- | src/main/java/com/gitblit/wicket/GitBlitWebApp.properties | 4 | ||||
-rw-r--r-- | src/main/java/com/gitblit/wicket/pages/TicketPage.html | 2 | ||||
-rw-r--r-- | src/main/java/com/gitblit/wicket/pages/TicketPage.java | 43 | ||||
-rw-r--r-- | src/main/resources/gitblit.css | 15 |
11 files changed, 952 insertions, 200 deletions
diff --git a/src/main/distrib/data/defaults.properties b/src/main/distrib/data/defaults.properties index 403b7417..0c7d6cd4 100644 --- a/src/main/distrib/data/defaults.properties +++ b/src/main/distrib/data/defaults.properties @@ -574,6 +574,13 @@ tickets.requireApproval = false # SINCE 1.5.0 tickets.closeOnPushCommitMessageRegex = (?:fixes|closes)[\\s-]+#?(\\d+) +# The case-insensitive regular expression used to identify and link tickets on +# push to the commits based on commit message. In the case of a patchset +# self references are ignored +# +# SINCE 1.8.0 +tickets.linkOnPushCommitMessageRegex = (?:ref|task|issue|bug)?[\\s-]*#(\\d+) + # Specify the location of the Lucene Ticket index # # SINCE 1.4.0 diff --git a/src/main/java/com/gitblit/git/GitblitReceivePack.java b/src/main/java/com/gitblit/git/GitblitReceivePack.java index 34bbea27..f271f6f1 100644 --- a/src/main/java/com/gitblit/git/GitblitReceivePack.java +++ b/src/main/java/com/gitblit/git/GitblitReceivePack.java @@ -22,18 +22,28 @@ import groovy.util.GroovyScriptEngine; import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
+import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.NullProgressMonitor;
+import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.ProgressMonitor;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.PostReceiveHook;
import org.eclipse.jgit.transport.PreReceiveHook;
import org.eclipse.jgit.transport.ReceiveCommand;
@@ -50,14 +60,24 @@ import com.gitblit.client.Translation; import com.gitblit.extensions.ReceiveHook;
import com.gitblit.manager.IGitblit;
import com.gitblit.models.RepositoryModel;
+import com.gitblit.models.TicketModel;
import com.gitblit.models.UserModel;
+import com.gitblit.models.TicketModel.Change;
+import com.gitblit.models.TicketModel.Field;
+import com.gitblit.models.TicketModel.Patchset;
+import com.gitblit.models.TicketModel.Status;
+import com.gitblit.models.TicketModel.TicketAction;
+import com.gitblit.models.TicketModel.TicketLink;
import com.gitblit.tickets.BranchTicketService;
+import com.gitblit.tickets.ITicketService;
+import com.gitblit.tickets.TicketNotifier;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.ClientLogger;
import com.gitblit.utils.CommitCache;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.RefLogUtils;
import com.gitblit.utils.StringUtils;
+import com.google.common.collect.Lists;
/**
@@ -92,6 +112,11 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P protected final IStoredSettings settings;
protected final IGitblit gitblit;
+
+ protected final ITicketService ticketService;
+
+ protected final TicketNotifier ticketNotifier;
+
public GitblitReceivePack(
IGitblit gitblit,
@@ -114,6 +139,14 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P } catch (IOException e) {
}
+ if (gitblit.getTicketService().isAcceptingTicketUpdates(repository)) {
+ this.ticketService = gitblit.getTicketService();
+ this.ticketNotifier = this.ticketService.createNotifier();
+ } else {
+ this.ticketService = null;
+ this.ticketNotifier = null;
+ }
+
// set advanced ref permissions
setAllowCreates(user.canCreateRef(repository));
setAllowDeletes(user.canDeleteRef(repository));
@@ -500,6 +533,104 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P }
}
}
+
+ //
+ // if there are ref update receive commands that were
+ // successfully processed and there is an active ticket service for the repository
+ // then process any referenced tickets
+ //
+ if (ticketService != null) {
+ List<ReceiveCommand> allUpdates = ReceiveCommand.filter(batch.getCommands(), Result.OK);
+ if (!allUpdates.isEmpty()) {
+ int ticketsProcessed = 0;
+ for (ReceiveCommand cmd : allUpdates) {
+ switch (cmd.getType()) {
+ case CREATE:
+ case UPDATE:
+ if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
+ Collection<TicketModel> tickets = processReferencedTickets(cmd);
+ ticketsProcessed += tickets.size();
+ for (TicketModel ticket : tickets) {
+ ticketNotifier.queueMailing(ticket);
+ }
+ }
+ break;
+
+ case UPDATE_NONFASTFORWARD:
+ if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
+ String base = JGitUtils.getMergeBase(getRepository(), cmd.getOldId(), cmd.getNewId());
+ List<TicketLink> deletedRefs = JGitUtils.identifyTicketsBetweenCommits(getRepository(), settings, base, cmd.getOldId().name());
+ for (TicketLink link : deletedRefs) {
+ link.isDelete = true;
+ }
+ Change deletion = new Change(user.username);
+ deletion.pendingLinks = deletedRefs;
+ ticketService.updateTicket(repository, 0, deletion);
+
+ Collection<TicketModel> tickets = processReferencedTickets(cmd);
+ ticketsProcessed += tickets.size();
+ for (TicketModel ticket : tickets) {
+ ticketNotifier.queueMailing(ticket);
+ }
+ }
+ break;
+ case DELETE:
+ //Identify if the branch has been merged
+ SortedMap<Integer, String> bases = new TreeMap<Integer, String>();
+ try {
+ ObjectId dObj = cmd.getOldId();
+ Collection<Ref> tips = getRepository().getRefDatabase().getRefs(Constants.R_HEADS).values();
+ for (Ref ref : tips) {
+ ObjectId iObj = ref.getObjectId();
+ String mergeBase = JGitUtils.getMergeBase(getRepository(), dObj, iObj);
+ if (mergeBase != null) {
+ int d = JGitUtils.countCommits(getRepository(), getRevWalk(), mergeBase, dObj.name());
+ bases.put(d, mergeBase);
+ //All commits have been merged into some other branch
+ if (d == 0) {
+ break;
+ }
+ }
+ }
+
+ if (bases.isEmpty()) {
+ //TODO: Handle orphan branch case
+ } else {
+ if (bases.firstKey() > 0) {
+ //Delete references from the remaining commits that haven't been merged
+ String mergeBase = bases.get(bases.firstKey());
+ List<TicketLink> deletedRefs = JGitUtils.identifyTicketsBetweenCommits(getRepository(),
+ settings, mergeBase, dObj.name());
+
+ for (TicketLink link : deletedRefs) {
+ link.isDelete = true;
+ }
+ Change deletion = new Change(user.username);
+ deletion.pendingLinks = deletedRefs;
+ ticketService.updateTicket(repository, 0, deletion);
+ }
+ }
+
+ } catch (IOException e) {
+ LOGGER.error(null, e);
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ if (ticketsProcessed == 1) {
+ sendInfo("1 ticket updated");
+ } else if (ticketsProcessed > 1) {
+ sendInfo("{0} tickets updated", ticketsProcessed);
+ }
+ }
+
+ // reset the ticket caches for the repository
+ ticketService.resetCaches(repository);
+ }
}
protected void setGitblitUrl(String url) {
@@ -616,4 +747,116 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P public UserModel getUserModel() {
return user;
}
+
+ /**
+ * Automatically closes open tickets and adds references to tickets if made in the commit message.
+ *
+ * @param cmd
+ */
+ private Collection<TicketModel> processReferencedTickets(ReceiveCommand cmd) {
+ Map<Long, TicketModel> changedTickets = new LinkedHashMap<Long, TicketModel>();
+
+ final RevWalk rw = getRevWalk();
+ try {
+ rw.reset();
+ rw.markStart(rw.parseCommit(cmd.getNewId()));
+ if (!ObjectId.zeroId().equals(cmd.getOldId())) {
+ rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
+ }
+
+ RevCommit c;
+ while ((c = rw.next()) != null) {
+ rw.parseBody(c);
+ List<TicketLink> ticketLinks = JGitUtils.identifyTicketsFromCommitMessage(getRepository(), settings, c);
+ if (ticketLinks == null) {
+ continue;
+ }
+
+ for (TicketLink link : ticketLinks) {
+
+ TicketModel ticket = ticketService.getTicket(repository, link.targetTicketId);
+ if (ticket == null) {
+ continue;
+ }
+
+ Change change = null;
+ String commitSha = c.getName();
+ String branchName = Repository.shortenRefName(cmd.getRefName());
+
+ switch (link.action) {
+ case Commit: {
+ //A commit can reference a ticket in any branch even if the ticket is closed.
+ //This allows developers to identify and communicate related issues
+ change = new Change(user.username);
+ change.referenceCommit(commitSha);
+ } break;
+
+ case Close: {
+ // As this isn't a patchset theres no merging taking place when closing a ticket
+ if (ticket.isClosed()) {
+ continue;
+ }
+
+ change = new Change(user.username);
+ change.setField(Field.status, Status.Fixed);
+
+ if (StringUtils.isEmpty(ticket.responsible)) {
+ // unassigned tickets are assigned to the closer
+ change.setField(Field.responsible, user.username);
+ }
+ }
+
+ default: {
+ //No action
+ } break;
+ }
+
+ if (change != null) {
+ ticket = ticketService.updateTicket(repository, ticket.number, change);
+ }
+
+ if (ticket != null) {
+ sendInfo("");
+ sendHeader("#{0,number,0}: {1}", ticket.number, StringUtils.trimString(ticket.title, Constants.LEN_SHORTLOG));
+
+ switch (link.action) {
+ case Commit: {
+ sendInfo("referenced by push of {0} to {1}", commitSha, branchName);
+ changedTickets.put(ticket.number, ticket);
+ } break;
+
+ case Close: {
+ sendInfo("closed by push of {0} to {1}", commitSha, branchName);
+ changedTickets.put(ticket.number, ticket);
+ } break;
+
+ default: { }
+ }
+
+ sendInfo(ticketService.getTicketUrl(ticket));
+ sendInfo("");
+ } else {
+ switch (link.action) {
+ case Commit: {
+ sendError("FAILED to reference ticket {0} by push of {1}", link.targetTicketId, commitSha);
+ } break;
+
+ case Close: {
+ sendError("FAILED to close ticket {0} by push of {1}", link.targetTicketId, commitSha);
+ } break;
+
+ default: { }
+ }
+ }
+ }
+ }
+
+ } catch (IOException e) {
+ LOGGER.error("Can't scan for changes to reference or close", e);
+ } finally {
+ rw.reset();
+ }
+
+ return changedTickets.values();
+ }
}
diff --git a/src/main/java/com/gitblit/git/PatchsetReceivePack.java b/src/main/java/com/gitblit/git/PatchsetReceivePack.java index ef0b409b..33fa4705 100644 --- a/src/main/java/com/gitblit/git/PatchsetReceivePack.java +++ b/src/main/java/com/gitblit/git/PatchsetReceivePack.java @@ -30,7 +30,6 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
@@ -60,6 +59,8 @@ import com.gitblit.models.TicketModel.Field; import com.gitblit.models.TicketModel.Patchset;
import com.gitblit.models.TicketModel.PatchsetType;
import com.gitblit.models.TicketModel.Status;
+import com.gitblit.models.TicketModel.TicketAction;
+import com.gitblit.models.TicketModel.TicketLink;
import com.gitblit.models.UserModel;
import com.gitblit.tickets.BranchTicketService;
import com.gitblit.tickets.ITicketService;
@@ -485,9 +486,27 @@ public class PatchsetReceivePack extends GitblitReceivePack { switch (cmd.getType()) {
case CREATE:
case UPDATE:
+ if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
+ Collection<TicketModel> tickets = processReferencedTickets(cmd);
+ ticketsProcessed += tickets.size();
+ for (TicketModel ticket : tickets) {
+ ticketNotifier.queueMailing(ticket);
+ }
+ }
+ break;
+
case UPDATE_NONFASTFORWARD:
if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
- Collection<TicketModel> tickets = processMergedTickets(cmd);
+ String base = JGitUtils.getMergeBase(getRepository(), cmd.getOldId(), cmd.getNewId());
+ List<TicketLink> deletedRefs = JGitUtils.identifyTicketsBetweenCommits(getRepository(), settings, base, cmd.getOldId().name());
+ for (TicketLink link : deletedRefs) {
+ link.isDelete = true;
+ }
+ Change deletion = new Change(user.username);
+ deletion.pendingLinks = deletedRefs;
+ ticketService.updateTicket(repository, 0, deletion);
+
+ Collection<TicketModel> tickets = processReferencedTickets(cmd);
ticketsProcessed += tickets.size();
for (TicketModel ticket : tickets) {
ticketNotifier.queueMailing(ticket);
@@ -604,15 +623,17 @@ public class PatchsetReceivePack extends GitblitReceivePack { return null;
}
}
-
+
// check to see if this commit is already linked to a ticket
- long id = identifyTicket(tipCommit, false);
- if (id > 0) {
- sendError("{0} has already been pushed to ticket {1,number,0}.", shortTipId, id);
+ if (ticket != null &&
+ JGitUtils.getTicketNumberFromCommitBranch(getRepository(), tipCommit) == ticket.number) {
+ sendError("{0} has already been pushed to ticket {1,number,0}.", shortTipId, ticket.number);
sendRejection(cmd, "everything up-to-date");
return null;
}
-
+
+ List<TicketLink> ticketLinks = JGitUtils.identifyTicketsFromCommitMessage(getRepository(), settings, tipCommit);
+
PatchsetCommand psCmd;
if (ticket == null) {
/*
@@ -802,6 +823,10 @@ public class PatchsetReceivePack extends GitblitReceivePack { }
break;
}
+
+ Change change = psCmd.getChange();
+ change.pendingLinks = ticketLinks;
+
return psCmd;
}
@@ -890,11 +915,11 @@ public class PatchsetReceivePack extends GitblitReceivePack { /**
* Automatically closes open tickets that have been merged to their integration
- * branch by a client.
+ * branch by a client and adds references to tickets if made in the commit message.
*
* @param cmd
*/
- private Collection<TicketModel> processMergedTickets(ReceiveCommand cmd) {
+ private Collection<TicketModel> processReferencedTickets(ReceiveCommand cmd) {
Map<Long, TicketModel> mergedTickets = new LinkedHashMap<Long, TicketModel>();
final RevWalk rw = getRevWalk();
try {
@@ -907,105 +932,151 @@ public class PatchsetReceivePack extends GitblitReceivePack { RevCommit c;
while ((c = rw.next()) != null) {
rw.parseBody(c);
- long ticketNumber = identifyTicket(c, true);
- if (ticketNumber == 0L || mergedTickets.containsKey(ticketNumber)) {
- continue;
- }
-
- TicketModel ticket = ticketService.getTicket(repository, ticketNumber);
- if (ticket == null) {
- continue;
- }
- String integrationBranch;
- if (StringUtils.isEmpty(ticket.mergeTo)) {
- // unspecified integration branch
- integrationBranch = null;
- } else {
- // specified integration branch
- integrationBranch = Constants.R_HEADS + ticket.mergeTo;
- }
-
- // ticket must be open and, if specified, the ref must match the integration branch
- if (ticket.isClosed() || (integrationBranch != null && !integrationBranch.equals(cmd.getRefName()))) {
+ List<TicketLink> ticketLinks = JGitUtils.identifyTicketsFromCommitMessage(getRepository(), settings, c);
+ if (ticketLinks == null) {
continue;
}
- String baseRef = PatchsetCommand.getBasePatchsetBranch(ticket.number);
- boolean knownPatchset = false;
- Set<Ref> refs = getRepository().getAllRefsByPeeledObjectId().get(c.getId());
- if (refs != null) {
- for (Ref ref : refs) {
- if (ref.getName().startsWith(baseRef)) {
- knownPatchset = true;
- break;
- }
- }
- }
-
- String mergeSha = c.getName();
- String mergeTo = Repository.shortenRefName(cmd.getRefName());
- Change change;
- Patchset patchset;
- if (knownPatchset) {
- // identify merged patchset by the patchset tip
- patchset = null;
- for (Patchset ps : ticket.getPatchsets()) {
- if (ps.tip.equals(mergeSha)) {
- patchset = ps;
- break;
- }
+ for (TicketLink link : ticketLinks) {
+
+ if (mergedTickets.containsKey(link.targetTicketId)) {
+ continue;
}
-
- if (patchset == null) {
- // should not happen - unless ticket has been hacked
- sendError("Failed to find the patchset for {0} in ticket {1,number,0}?!",
- mergeSha, ticket.number);
+
+ TicketModel ticket = ticketService.getTicket(repository, link.targetTicketId);
+ if (ticket == null) {
continue;
}
+ String integrationBranch;
+ if (StringUtils.isEmpty(ticket.mergeTo)) {
+ // unspecified integration branch
+ integrationBranch = null;
+ } else {
+ // specified integration branch
+ integrationBranch = Constants.R_HEADS + ticket.mergeTo;
+ }
+
+ Change change;
+ Patchset patchset = null;
+ String mergeSha = c.getName();
+ String mergeTo = Repository.shortenRefName(cmd.getRefName());
+
+ if (link.action == TicketAction.Commit) {
+ //A commit can reference a ticket in any branch even if the ticket is closed.
+ //This allows developers to identify and communicate related issues
+ change = new Change(user.username);
+ change.referenceCommit(mergeSha);
+ } else {
+ // ticket must be open and, if specified, the ref must match the integration branch
+ if (ticket.isClosed() || (integrationBranch != null && !integrationBranch.equals(cmd.getRefName()))) {
+ continue;
+ }
+
+ String baseRef = PatchsetCommand.getBasePatchsetBranch(ticket.number);
+ boolean knownPatchset = false;
+ Set<Ref> refs = getRepository().getAllRefsByPeeledObjectId().get(c.getId());
+ if (refs != null) {
+ for (Ref ref : refs) {
+ if (ref.getName().startsWith(baseRef)) {
+ knownPatchset = true;
+ break;
+ }
+ }
+ }
+
+ if (knownPatchset) {
+ // identify merged patchset by the patchset tip
+ for (Patchset ps : ticket.getPatchsets()) {
+ if (ps.tip.equals(mergeSha)) {
+ patchset = ps;
+ break;
+ }
+ }
+
+ if (patchset == null) {
+ // should not happen - unless ticket has been hacked
+ sendError("Failed to find the patchset for {0} in ticket {1,number,0}?!",
+ mergeSha, ticket.number);
+ continue;
+ }
+
+ // create a new change
+ change = new Change(user.username);
+ } else {
+ // new patchset pushed by user
+ String base = cmd.getOldId().getName();
+ patchset = newPatchset(ticket, base, mergeSha);
+ PatchsetCommand psCmd = new PatchsetCommand(user.username, patchset);
+ psCmd.updateTicket(c, mergeTo, ticket, null);
+
+ // create a ticket patchset ref
+ updateRef(psCmd.getPatchsetBranch(), c.getId(), patchset.type);
+ RefUpdate ru = updateRef(psCmd.getTicketBranch(), c.getId(), patchset.type);
+ updateReflog(ru);
+
+ // create a change from the patchset command
+ change = psCmd.getChange();
+ }
+
+ // set the common change data about the merge
+ change.setField(Field.status, Status.Merged);
+ change.setField(Field.mergeSha, mergeSha);
+ change.setField(Field.mergeTo, mergeTo);
+
+ if (StringUtils.isEmpty(ticket.responsible)) {
+ // unassigned tickets are assigned to the closer
+ change.setField(Field.responsible, user.username);
+ }
+ }
+
+ ticket = ticketService.updateTicket(repository, ticket.number, change);
+
+ if (ticket != null) {
+ sendInfo("");
+ sendHeader("#{0,number,0}: {1}", ticket.number, StringUtils.trimString(ticket.title, Constants.LEN_SHORTLOG));
+
+ switch (link.action) {
+ case Commit: {
+ sendInfo("referenced by push of {0} to {1}", c.getName(), mergeTo);
+ }
+ break;
- // create a new change
- change = new Change(user.username);
- } else {
- // new patchset pushed by user
- String base = cmd.getOldId().getName();
- patchset = newPatchset(ticket, base, mergeSha);
- PatchsetCommand psCmd = new PatchsetCommand(user.username, patchset);
- psCmd.updateTicket(c, mergeTo, ticket, null);
-
- // create a ticket patchset ref
- updateRef(psCmd.getPatchsetBranch(), c.getId(), patchset.type);
- RefUpdate ru = updateRef(psCmd.getTicketBranch(), c.getId(), patchset.type);
- updateReflog(ru);
-
- // create a change from the patchset command
- change = psCmd.getChange();
- }
+ case Close: {
+ sendInfo("closed by push of {0} to {1}", patchset, mergeTo);
+ mergedTickets.put(ticket.number, ticket);
+ }
+ break;
- // set the common change data about the merge
- change.setField(Field.status, Status.Merged);
- change.setField(Field.mergeSha, mergeSha);
- change.setField(Field.mergeTo, mergeTo);
+ default: {
+
+ }
+ }
- if (StringUtils.isEmpty(ticket.responsible)) {
- // unassigned tickets are assigned to the closer
- change.setField(Field.responsible, user.username);
- }
+ sendInfo(ticketService.getTicketUrl(ticket));
+ sendInfo("");
- ticket = ticketService.updateTicket(repository, ticket.number, change);
- if (ticket != null) {
- sendInfo("");
- sendHeader("#{0,number,0}: {1}", ticket.number, StringUtils.trimString(ticket.title, Constants.LEN_SHORTLOG));
- sendInfo("closed by push of {0} to {1}", patchset, mergeTo);
- sendInfo(ticketService.getTicketUrl(ticket));
- sendInfo("");
- mergedTickets.put(ticket.number, ticket);
- } else {
- String shortid = mergeSha.substring(0, settings.getInteger(Keys.web.shortCommitIdLength, 6));
- sendError("FAILED to close ticket {0,number,0} by push of {1}", ticketNumber, shortid);
+ } else {
+ String shortid = mergeSha.substring(0, settings.getInteger(Keys.web.shortCommitIdLength, 6));
+
+ switch (link.action) {
+ case Commit: {
+ sendError("FAILED to reference ticket {0,number,0} by push of {1}", link.targetTicketId, shortid);
+ }
+ break;
+ case Close: {
+ sendError("FAILED to close ticket {0,number,0} by push of {1}", link.targetTicketId, shortid);
+ } break;
+
+ default: {
+
+ }
+ }
+ }
}
}
+
} catch (IOException e) {
- LOGGER.error("Can't scan for changes to close", e);
+ LOGGER.error("Can't scan for changes to reference or close", e);
} finally {
rw.reset();
}
@@ -1013,75 +1084,9 @@ public class PatchsetReceivePack extends GitblitReceivePack { return mergedTickets.values();
}
- /**
- * Try to identify a ticket id from the commit.
- *
- * @param commit
- * @param parseMessage
- * @return a ticket id or 0
- */
- private long identifyTicket(RevCommit commit, boolean parseMessage) {
- // try lookup by change ref
- Map<AnyObjectId, Set<Ref>> map = getRepository().getAllRefsByPeeledObjectId();
- Set<Ref> refs = map.get(commit.getId());
- if (!ArrayUtils.isEmpty(refs)) {
- for (Ref ref : refs) {
- long number = PatchsetCommand.getTicketNumber(ref.getName());
- if (number > 0) {
- return number;
- }
- }
- }
-
- if (parseMessage) {
- // parse commit message looking for fixes/closes #n
- String dx = "(?:fixes|closes)[\\s-]+#?(\\d+)";
- String x = settings.getString(Keys.tickets.closeOnPushCommitMessageRegex, dx);
- if (StringUtils.isEmpty(x)) {
- x = dx;
- }
- try {
- Pattern p = Pattern.compile(x, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(commit.getFullMessage());
- while (m.find()) {
- String val = m.group(1);
- return Long.parseLong(val);
- }
- } catch (Exception e) {
- LOGGER.error(String.format("Failed to parse \"%s\" in commit %s", x, commit.getName()), e);
- }
- }
- return 0L;
- }
+
- private int countCommits(String baseId, String tipId) {
- int count = 0;
- RevWalk walk = getRevWalk();
- walk.reset();
- walk.sort(RevSort.TOPO);
- walk.sort(RevSort.REVERSE, true);
- try {
- RevCommit tip = walk.parseCommit(getRepository().resolve(tipId));
- RevCommit base = walk.parseCommit(getRepository().resolve(baseId));
- walk.markStart(tip);
- walk.markUninteresting(base);
- for (;;) {
- RevCommit c = walk.next();
- if (c == null) {
- break;
- }
- count++;
- }
- } catch (IOException e) {
- // Should never happen, the core receive process would have
- // identified the missing object earlier before we got control.
- LOGGER.error("failed to get commit count", e);
- return 0;
- } finally {
- walk.close();
- }
- return count;
- }
+
/**
* Creates a new patchset with metadata.
@@ -1091,7 +1096,7 @@ public class PatchsetReceivePack extends GitblitReceivePack { * @param tip
*/
private Patchset newPatchset(TicketModel ticket, String mergeBase, String tip) {
- int totalCommits = countCommits(mergeBase, tip);
+ int totalCommits = JGitUtils.countCommits(getRepository(), getRevWalk(), mergeBase, tip);
Patchset newPatchset = new Patchset();
newPatchset.tip = tip;
diff --git a/src/main/java/com/gitblit/models/TicketModel.java b/src/main/java/com/gitblit/models/TicketModel.java index 7495448f..d5345891 100644 --- a/src/main/java/com/gitblit/models/TicketModel.java +++ b/src/main/java/com/gitblit/models/TicketModel.java @@ -107,6 +107,7 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { TicketModel ticket; List<Change> effectiveChanges = new ArrayList<Change>(); Map<String, Change> comments = new HashMap<String, Change>(); + Map<String, Change> references = new HashMap<String, Change>(); Map<Integer, Integer> latestRevisions = new HashMap<Integer, Integer>(); int latestPatchsetNumber = -1; @@ -159,6 +160,18 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { effectiveChanges.add(change); } + } else if (change.reference != null){ + if (references.containsKey(change.reference.toString())) { + Change original = references.get(change.reference.toString()); + Change clone = copy(original); + clone.reference.deleted = change.reference.deleted; + int idx = effectiveChanges.indexOf(original); + effectiveChanges.remove(original); + effectiveChanges.add(idx, clone); + } else { + effectiveChanges.add(change); + references.put(change.reference.toString(), change); + } } else { effectiveChanges.add(change); } @@ -167,10 +180,16 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { // effective ticket ticket = new TicketModel(); for (Change change : effectiveChanges) { + //Ensure deleted items are not included if (!change.hasComment()) { - // ensure we do not include a deleted comment change.comment = null; } + if (!change.hasReference()) { + change.reference = null; + } + if (!change.hasPatchset()) { + change.patchset = null; + } ticket.applyChange(change); } return ticket; @@ -354,6 +373,15 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { return false; } + public boolean hasReferences() { + for (Change change : changes) { + if (change.hasReference()) { + return true; + } + } + return false; + } + public List<Attachment> getAttachments() { List<Attachment> list = new ArrayList<Attachment>(); for (Change change : changes) { @@ -364,6 +392,16 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { return list; } + public List<Reference> getReferences() { + List<Reference> list = new ArrayList<Reference>(); + for (Change change : changes) { + if (change.hasReference()) { + list.add(change.reference); + } + } + return list; + } + public List<Patchset> getPatchsets() { List<Patchset> list = new ArrayList<Patchset>(); for (Change change : changes) { @@ -573,8 +611,12 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { } } - // add the change to the ticket - changes.add(change); + // add real changes to the ticket and ensure deleted changes are removed + if (change.isEmptyChange()) { + changes.remove(change); + } else { + changes.add(change); + } } protected String toString(Object value) { @@ -645,6 +687,8 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { public Comment comment; + public Reference reference; + public Map<Field, String> fields; public Set<Attachment> attachments; @@ -655,6 +699,10 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { private transient String id; + //Once links have been made they become a reference on the target ticket + //The ticket service handles promoting links to references + public transient List<TicketLink> pendingLinks; + public Change(String author) { this(author, new Date()); } @@ -678,7 +726,7 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { } public boolean hasPatchset() { - return patchset != null; + return patchset != null && !patchset.isDeleted(); } public boolean hasReview() { @@ -688,11 +736,42 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { public boolean hasComment() { return comment != null && !comment.isDeleted() && comment.text != null; } + + public boolean hasReference() { + return reference != null && !reference.isDeleted(); + } + + public boolean hasPendingLinks() { + return pendingLinks != null && pendingLinks.size() > 0; + } public Comment comment(String text) { comment = new Comment(text); comment.id = TicketModel.getSHA1(date.toString() + author + text); + // parse comment looking for ref #n + //TODO: Ideally set via settings + String x = "(?:ref|task|issue|bug)?[\\s-]*#(\\d+)"; + + try { + Pattern p = Pattern.compile(x, Pattern.CASE_INSENSITIVE); + Matcher m = p.matcher(text); + while (m.find()) { + String val = m.group(1); + long targetTicketId = Long.parseLong(val); + + if (targetTicketId > 0) { + if (pendingLinks == null) { + pendingLinks = new ArrayList<TicketLink>(); + } + + pendingLinks.add(new TicketLink(targetTicketId, TicketAction.Comment)); + } + } + } catch (Exception e) { + // ignore + } + try { Pattern mentions = Pattern.compile("\\s@([A-Za-z0-9-_]+)"); Matcher m = mentions.matcher(text); @@ -706,6 +785,16 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { return comment; } + public Reference referenceCommit(String commitHash) { + reference = new Reference(commitHash); + return reference; + } + + public Reference referenceTicket(long ticketId, String changeHash) { + reference = new Reference(ticketId, changeHash); + return reference; + } + public Review review(Patchset patchset, Score score, boolean addReviewer) { if (addReviewer) { plusList(Field.reviewers, author); @@ -876,6 +965,17 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { } return false; } + + /* + * Identify if this is an empty change. i.e. only an author and date is defined. + * This can occur when items have been deleted + * @returns true if the change is empty + */ + private boolean isEmptyChange() { + return ((comment == null) && (reference == null) && + (fields == null) && (attachments == null) && + (patchset == null) && (review == null)); + } @Override public String toString() { @@ -885,6 +985,8 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { sb.append(" commented on by "); } else if (hasPatchset()) { sb.append(MessageFormat.format(" {0} uploaded by ", patchset)); + } else if (hasReference()) { + sb.append(MessageFormat.format(" referenced in {0} by ", reference)); } else { sb.append(" changed by "); } @@ -1145,6 +1247,114 @@ public class TicketModel implements Serializable, Comparable<TicketModel> { return text; } } + + + public static enum TicketAction { + Commit, Comment, Patchset, Close + } + + //Intentionally not serialized, links are persisted as "references" + public static class TicketLink { + public long targetTicketId; + public String hash; + public TicketAction action; + public boolean success; + public boolean isDelete; + + public TicketLink(long targetTicketId, TicketAction action) { + this.targetTicketId = targetTicketId; + this.action = action; + success = false; + isDelete = false; + } + + public TicketLink(long targetTicketId, TicketAction action, String hash) { + this.targetTicketId = targetTicketId; + this.action = action; + this.hash = hash; + success = false; + isDelete = false; + } + } + + public static enum ReferenceType { + Undefined, Commit, Ticket; + + @Override + public String toString() { + return name().toLowerCase().replace('_', ' '); + } + + public static ReferenceType fromObject(Object o, ReferenceType defaultType) { + if (o instanceof ReferenceType) { + // cast and return + return (ReferenceType) o; + } else if (o instanceof String) { + // find by name + for (ReferenceType type : values()) { + String str = o.toString(); + if (type.name().equalsIgnoreCase(str) + || type.toString().equalsIgnoreCase(str)) { + return type; + } + } + } else if (o instanceof Number) { + // by ordinal + int id = ((Number) o).intValue(); + if (id >= 0 && id < values().length) { + return values()[id]; + } + } + + return defaultType; + } + } + + public static class Reference implements Serializable { + + private static final long serialVersionUID = 1L; + + public String hash; + public Long ticketId; + + public Boolean deleted; + + Reference(String commitHash) { + this.hash = commitHash; + } + + Reference(long ticketId, String changeHash) { + this.ticketId = ticketId; + this.hash = changeHash; + } + + public ReferenceType getSourceType(){ + if (hash != null) { + if (ticketId != null) { + return ReferenceType.Ticket; + } else { + return ReferenceType.Commit; + } + } + + return ReferenceType.Undefined; + } + + public boolean isDeleted() { + return deleted != null && deleted; + } + + @Override + public String toString() { + switch (getSourceType()) { + case Commit: return hash; + case Ticket: return ticketId.toString() + "#" + hash; + default: {} break; + } + + return String.format("Unknown Reference Type"); + } + } public static class Attachment implements Serializable { diff --git a/src/main/java/com/gitblit/tickets/ITicketService.java b/src/main/java/com/gitblit/tickets/ITicketService.java index e8310039..20b6505b 100644 --- a/src/main/java/com/gitblit/tickets/ITicketService.java +++ b/src/main/java/com/gitblit/tickets/ITicketService.java @@ -50,9 +50,11 @@ import com.gitblit.models.TicketModel.Field; import com.gitblit.models.TicketModel.Patchset; import com.gitblit.models.TicketModel.PatchsetType; import com.gitblit.models.TicketModel.Status; +import com.gitblit.models.TicketModel.TicketLink; import com.gitblit.tickets.TicketIndexer.Lucene; import com.gitblit.utils.DeepCopier; import com.gitblit.utils.DiffUtils; +import com.gitblit.utils.JGitUtils; import com.gitblit.utils.DiffUtils.DiffStat; import com.gitblit.utils.StringUtils; import com.google.common.cache.Cache; @@ -1021,12 +1023,12 @@ public abstract class ITicketService implements IManager { } /** - * Updates a ticket. + * Updates a ticket and promotes pending links into references. * * @param repository - * @param ticketId + * @param ticketId, or 0 to action pending links in general * @param change - * @return the ticket model if successful + * @return the ticket model if successful, null if failure or using 0 ticketId * @since 1.4.0 */ public final TicketModel updateTicket(RepositoryModel repository, long ticketId, Change change) { @@ -1038,28 +1040,78 @@ public abstract class ITicketService implements IManager { throw new RuntimeException("must specify a change author!"); } - TicketKey key = new TicketKey(repository, ticketId); - ticketsCache.invalidate(key); - - boolean success = commitChangeImpl(repository, ticketId, change); + boolean success = true; + TicketModel ticket = null; + + if (ticketId > 0) { + TicketKey key = new TicketKey(repository, ticketId); + ticketsCache.invalidate(key); + + success = commitChangeImpl(repository, ticketId, change); + + if (success) { + ticket = getTicket(repository, ticketId); + ticketsCache.put(key, ticket); + indexer.index(ticket); + + // call the ticket hooks + if (pluginManager != null) { + for (TicketHook hook : pluginManager.getExtensions(TicketHook.class)) { + try { + hook.onUpdateTicket(ticket, change); + } catch (Exception e) { + log.error("Failed to execute extension", e); + } + } + } + } + } + if (success) { - TicketModel ticket = getTicket(repository, ticketId); - ticketsCache.put(key, ticket); - indexer.index(ticket); + //Now that the ticket has been successfully persisted add references to this ticket from linked tickets + if (change.hasPendingLinks()) { + for (TicketLink link : change.pendingLinks) { + TicketModel linkedTicket = getTicket(repository, link.targetTicketId); + Change dstChange = null; + + //Ignore if not available or self reference + if (linkedTicket != null && link.targetTicketId != ticketId) { + dstChange = new Change(change.author, change.date); + + switch (link.action) { + case Comment: { + if (ticketId == 0) { + throw new RuntimeException("must specify a ticket when linking a comment!"); + } + dstChange.referenceTicket(ticketId, change.comment.id); + } break; + + case Commit: { + dstChange.referenceCommit(link.hash); + } break; + + default: { + throw new RuntimeException( + String.format("must add persist logic for link of type %s", link.action)); + } + } + } + + if (dstChange != null) { + //If not deleted then remain null in journal + if (link.isDelete) { + dstChange.reference.deleted = true; + } - // call the ticket hooks - if (pluginManager != null) { - for (TicketHook hook : pluginManager.getExtensions(TicketHook.class)) { - try { - hook.onUpdateTicket(ticket, change); - } catch (Exception e) { - log.error("Failed to execute extension", e); + if (updateTicket(repository, link.targetTicketId, dstChange) != null) { + link.success = true; + } } } } - return ticket; } - return null; + + return ticket; } /** @@ -1232,9 +1284,18 @@ public abstract class ITicketService implements IManager { deletion.patchset.number = patchset.number; deletion.patchset.rev = patchset.rev; deletion.patchset.type = PatchsetType.Delete; + //Find and delete references to tickets by the removed commits + List<TicketLink> patchsetTicketLinks = JGitUtils.identifyTicketsBetweenCommits( + repositoryManager.getRepository(ticket.repository), + settings, patchset.base, patchset.tip); - RepositoryModel repository = repositoryManager.getRepositoryModel(ticket.repository); - TicketModel revisedTicket = updateTicket(repository, ticket.number, deletion); + for (TicketLink link : patchsetTicketLinks) { + link.isDelete = true; + } + deletion.pendingLinks = patchsetTicketLinks; + + RepositoryModel repositoryModel = repositoryManager.getRepositoryModel(ticket.repository); + TicketModel revisedTicket = updateTicket(repositoryModel, ticket.number, deletion); return revisedTicket; } diff --git a/src/main/java/com/gitblit/tickets/TicketNotifier.java b/src/main/java/com/gitblit/tickets/TicketNotifier.java index 5979cf26..8c7fe6d4 100644 --- a/src/main/java/com/gitblit/tickets/TicketNotifier.java +++ b/src/main/java/com/gitblit/tickets/TicketNotifier.java @@ -317,6 +317,19 @@ public class TicketNotifier { // comment update sb.append(MessageFormat.format("**{0}** commented on this ticket.", user.getDisplayName())); sb.append(HARD_BRK); + } else if (lastChange.hasReference()) { + // reference update + String type = "?"; + + switch (lastChange.reference.getSourceType()) { + case Commit: { type = "commit"; } break; + case Ticket: { type = "ticket"; } break; + default: { } break; + } + + sb.append(MessageFormat.format("**{0}** referenced this ticket in {1} {2}", type, lastChange.toString())); + sb.append(HARD_BRK); + } else { // general update pattern = "**{0}** has updated this ticket."; diff --git a/src/main/java/com/gitblit/utils/JGitUtils.java b/src/main/java/com/gitblit/utils/JGitUtils.java index adcbb4de..a02fc3ff 100644 --- a/src/main/java/com/gitblit/utils/JGitUtils.java +++ b/src/main/java/com/gitblit/utils/JGitUtils.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -46,12 +47,15 @@ import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
+import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
+import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.errors.StopWalkException;
import org.eclipse.jgit.internal.JGitText;
+import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.BlobBasedConfig;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
@@ -91,19 +95,22 @@ import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.FS;
+import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.gitblit.GitBlit;
import com.gitblit.GitBlitException;
-import com.gitblit.manager.GitblitManager;
+import com.gitblit.IStoredSettings;
+import com.gitblit.Keys;
+import com.gitblit.git.PatchsetCommand;
import com.gitblit.models.FilestoreModel;
import com.gitblit.models.GitNote;
import com.gitblit.models.PathModel;
import com.gitblit.models.PathModel.PathChangeModel;
+import com.gitblit.models.TicketModel.TicketAction;
+import com.gitblit.models.TicketModel.TicketLink;
import com.gitblit.models.RefModel;
import com.gitblit.models.SubmoduleModel;
-import com.gitblit.servlet.FilestoreServlet;
import com.google.common.base.Strings;
/**
@@ -2740,5 +2747,163 @@ public class JGitUtils { }
return false;
}
+
+ /*
+ * Identify ticket by considering the branch the commit is on
+ *
+ * @param repository
+ * @param commit
+ * @return ticket number, or 0 if no ticket
+ */
+ public static long getTicketNumberFromCommitBranch(Repository repository, RevCommit commit) {
+ // try lookup by change ref
+ Map<AnyObjectId, Set<Ref>> map = repository.getAllRefsByPeeledObjectId();
+ Set<Ref> refs = map.get(commit.getId());
+ if (!ArrayUtils.isEmpty(refs)) {
+ for (Ref ref : refs) {
+ long number = PatchsetCommand.getTicketNumber(ref.getName());
+
+ if (number > 0) {
+ return number;
+ }
+ }
+ }
+
+ return 0;
+ }
+
+
+ /**
+ * Try to identify all referenced tickets from the commit.
+ *
+ * @param commit
+ * @return a collection of TicketLinks
+ */
+ @NotNull
+ public static List<TicketLink> identifyTicketsFromCommitMessage(Repository repository, IStoredSettings settings,
+ RevCommit commit) {
+ List<TicketLink> ticketLinks = new ArrayList<TicketLink>();
+ List<Long> linkedTickets = new ArrayList<Long>();
+
+ // parse commit message looking for fixes/closes #n
+ final String xFixDefault = "(?:fixes|closes)[\\s-]+#?(\\d+)";
+ String xFix = settings.getString(Keys.tickets.closeOnPushCommitMessageRegex, xFixDefault);
+ if (StringUtils.isEmpty(xFix)) {
+ xFix = xFixDefault;
+ }
+ try {
+ Pattern p = Pattern.compile(xFix, Pattern.CASE_INSENSITIVE);
+ Matcher m = p.matcher(commit.getFullMessage());
+ while (m.find()) {
+ String val = m.group(1);
+ long number = Long.parseLong(val);
+
+ if (number > 0) {
+ ticketLinks.add(new TicketLink(number, TicketAction.Close));
+ linkedTickets.add(number);
+ }
+ }
+ } catch (Exception e) {
+ LOGGER.error(String.format("Failed to parse \"%s\" in commit %s", xFix, commit.getName()), e);
+ }
+
+ // parse commit message looking for ref #n
+ final String xRefDefault = "(?:ref|task|issue|bug)?[\\s-]*#(\\d+)";
+ String xRef = settings.getString(Keys.tickets.linkOnPushCommitMessageRegex, xRefDefault);
+ if (StringUtils.isEmpty(xRef)) {
+ xRef = xRefDefault;
+ }
+ try {
+ Pattern p = Pattern.compile(xRef, Pattern.CASE_INSENSITIVE);
+ Matcher m = p.matcher(commit.getFullMessage());
+ while (m.find()) {
+ String val = m.group(1);
+ long number = Long.parseLong(val);
+ //Most generic case so don't included tickets more precisely linked
+ if ((number > 0) && (!linkedTickets.contains(number))) {
+ ticketLinks.add( new TicketLink(number, TicketAction.Commit, commit.getName()));
+ linkedTickets.add(number);
+ }
+ }
+ } catch (Exception e) {
+ LOGGER.error(String.format("Failed to parse \"%s\" in commit %s", xRef, commit.getName()), e);
+ }
+ return ticketLinks;
+ }
+
+ /**
+ * Try to identify all referenced tickets between two commits
+ *
+ * @param commit
+ * @param parseMessage
+ * @param currentTicketId, or 0 if not on a ticket branch
+ * @return a collection of TicketLink, or null if commit is already linked
+ */
+ public static List<TicketLink> identifyTicketsBetweenCommits(Repository repository, IStoredSettings settings,
+ String baseSha, String tipSha) {
+ List<TicketLink> links = new ArrayList<TicketLink>();
+ if (repository == null) { return links; }
+
+ RevWalk walk = new RevWalk(repository);
+ walk.sort(RevSort.TOPO);
+ walk.sort(RevSort.REVERSE, true);
+ try {
+ RevCommit tip = walk.parseCommit(repository.resolve(tipSha));
+ RevCommit base = walk.parseCommit(repository.resolve(baseSha));
+ walk.markStart(tip);
+ walk.markUninteresting(base);
+ for (;;) {
+ RevCommit commit = walk.next();
+ if (commit == null) {
+ break;
+ }
+ links.addAll(JGitUtils.identifyTicketsFromCommitMessage(repository, settings, commit));
+ }
+ } catch (IOException e) {
+ LOGGER.error("failed to identify tickets between commits.", e);
+ } finally {
+ walk.dispose();
+ }
+
+ return links;
+ }
+
+ public static int countCommits(Repository repository, RevWalk walk, ObjectId baseId, ObjectId tipId) {
+ int count = 0;
+ walk.reset();
+ walk.sort(RevSort.TOPO);
+ walk.sort(RevSort.REVERSE, true);
+ try {
+ RevCommit tip = walk.parseCommit(tipId);
+ RevCommit base = walk.parseCommit(baseId);
+ walk.markStart(tip);
+ walk.markUninteresting(base);
+ for (;;) {
+ RevCommit c = walk.next();
+ if (c == null) {
+ break;
+ }
+ count++;
+ }
+ } catch (IOException e) {
+ // Should never happen, the core receive process would have
+ // identified the missing object earlier before we got control.
+ LOGGER.error("failed to get commit count", e);
+ return 0;
+ } finally {
+ walk.close();
+ }
+ return count;
+ }
+
+ public static int countCommits(Repository repository, RevWalk walk, String baseId, String tipId) {
+ int count = 0;
+ try {
+ count = countCommits(repository, walk, repository.resolve(baseId),repository.resolve(tipId));
+ } catch (IOException e) {
+ LOGGER.error("failed to get commit count", e);
+ }
+ return count;
+ }
}
diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties b/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties index cee7eaba..a215b4d6 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties @@ -778,4 +778,6 @@ gb.fileNotMergeable = Unable to commit {0}. This file can not be automatically gb.fileCommitted = Successfully committed {0}. gb.deletePatchset = Delete Patchset {0} gb.deletePatchsetSuccess = Deleted Patchset {0}. -gb.deletePatchsetFailure = Error deleting Patchset {0}. +gb.deletePatchsetFailure = Error deleting Patchset {0}. +gb.referencedByCommit = Referenced by commit. +gb.referencedByTicket = Referenced by ticket. diff --git a/src/main/java/com/gitblit/wicket/pages/TicketPage.html b/src/main/java/com/gitblit/wicket/pages/TicketPage.html index 974dcc03..46c0f7ee 100644 --- a/src/main/java/com/gitblit/wicket/pages/TicketPage.html +++ b/src/main/java/com/gitblit/wicket/pages/TicketPage.html @@ -461,7 +461,7 @@ pt push</pre> <td style="text-align:right;">
<span wicket:id="patchsetType">[revision type]</span>
</td>
- <td><span class="hidden-phone hidden-tablet aui-lozenge aui-lozenge-subtle" wicket:id="patchsetRevision">[R1]</span>
+ <td><span class="hidden-phone hidden-tablet" wicket:id="patchsetRevision">[R1]</span>
<span class="fa fa-fw" style="padding-left:15px;"><a wicket:id="deleteRevision" class="fa fa-fw fa-trash delete-patchset"></a></span>
<span class="hidden-tablet hidden-phone" style="padding-left:15px;"><span wicket:id="patchsetDiffStat"></span></span>
</td>
diff --git a/src/main/java/com/gitblit/wicket/pages/TicketPage.java b/src/main/java/com/gitblit/wicket/pages/TicketPage.java index b2e63a60..cd049f4d 100644 --- a/src/main/java/com/gitblit/wicket/pages/TicketPage.java +++ b/src/main/java/com/gitblit/wicket/pages/TicketPage.java @@ -36,7 +36,6 @@ import org.apache.wicket.AttributeModifier; import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
-import org.apache.wicket.RequestCycle;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.SimpleAttributeModifier;
@@ -45,7 +44,6 @@ import org.apache.wicket.markup.html.image.ContextImage; import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
-import org.apache.wicket.markup.html.link.StatelessLink;
import org.apache.wicket.markup.html.pages.RedirectPage;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.repeater.Item;
@@ -54,7 +52,6 @@ import org.apache.wicket.markup.repeater.data.ListDataProvider; import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.RequestUtils;
import org.apache.wicket.protocol.http.WebRequest;
-import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
@@ -863,9 +860,6 @@ public class TicketPage extends RepositoryPage { if (event.hasPatchset()) {
// patchset
Patchset patchset = event.patchset;
- //In the case of using a cached change list
- item.setVisible(!patchset.isDeleted());
-
String what;
if (event.isStatusChange() && (Status.New == event.getStatus())) {
what = getString("gb.proposedThisChange");
@@ -883,6 +877,7 @@ public class TicketPage extends RepositoryPage { LinkPanel psr = new LinkPanel("patchsetRevision", null, patchset.number + "-" + patchset.rev,
ComparePage.class, WicketUtils.newRangeParameter(repositoryName, patchset.parent == null ? patchset.base : patchset.parent, patchset.tip), true);
WicketUtils.setHtmlTooltip(psr, patchset.toString());
+ WicketUtils.setCssClass(psr, "aui-lozenge aui-lozenge-subtle");
item.add(psr);
String typeCss = getPatchsetTypeCss(patchset.type);
Label typeLabel = new Label("patchsetType", patchset.type.toString());
@@ -910,6 +905,42 @@ public class TicketPage extends RepositoryPage { item.add(new Label("patchsetType").setVisible(false));
item.add(new Label("deleteRevision").setVisible(false));
item.add(new Label("patchsetDiffStat").setVisible(false));
+ } else if (event.hasReference()) {
+ // reference
+ switch (event.reference.getSourceType()) {
+ case Commit: {
+ final int shaLen = app().settings().getInteger(Keys.web.shortCommitIdLength, 6);
+
+ item.add(new Label("what", getString("gb.referencedByCommit")));
+ LinkPanel psr = new LinkPanel("patchsetRevision", null, event.reference.toString().substring(0, shaLen),
+ CommitPage.class, WicketUtils.newObjectParameter(repositoryName, event.reference.toString()), true);
+ WicketUtils.setHtmlTooltip(psr, event.reference.toString());
+ WicketUtils.setCssClass(psr, "ticketReference-commit shortsha1");
+ item.add(psr);
+
+ } break;
+
+ case Ticket: {
+ final String text = MessageFormat.format("ticket/{0}", event.reference.ticketId);
+
+ item.add(new Label("what", getString("gb.referencedByTicket")));
+ //NOTE: Ideally reference the exact comment using reference.toString,
+ // however anchor hash is used and is escaped resulting in broken link
+ LinkPanel psr = new LinkPanel("patchsetRevision", null, text,
+ TicketsPage.class, WicketUtils.newObjectParameter(repositoryName, event.reference.ticketId.toString()), true);
+ WicketUtils.setCssClass(psr, "ticketReference-comment");
+ item.add(psr);
+ } break;
+
+ default: {
+ item.add(new Label("what").setVisible(false));
+ item.add(new Label("patchsetRevision").setVisible(false));
+ }
+ }
+
+ item.add(new Label("patchsetType").setVisible(false));
+ item.add(new Label("deleteRevision").setVisible(false));
+ item.add(new Label("patchsetDiffStat").setVisible(false));
} else if (event.hasReview()) {
// review
String score;
diff --git a/src/main/resources/gitblit.css b/src/main/resources/gitblit.css index 3318441a..10c9a0e8 100644 --- a/src/main/resources/gitblit.css +++ b/src/main/resources/gitblit.css @@ -2391,4 +2391,19 @@ table.filestore-status { .delete-patchset {
color:#D51900;
font-size: 1.2em;
+}
+
+.ticketReference-comment {
+ font-family: sans-serif;
+ font-weight: 200;
+ font-size: 1em;
+ font-variant: normal;
+ text-transform: none;
+}
+
+.ticketReference-commit {
+ font-family: monospace;
+ font-weight: 200;
+ font-size: 1em;
+ font-variant: normal;
}
\ No newline at end of file |