| | |
| | |
|
| | | import com.gitblit.Constants;
|
| | | import com.gitblit.Keys;
|
| | | import com.gitblit.extensions.PatchsetHook;
|
| | | import com.gitblit.manager.IGitblit;
|
| | | import com.gitblit.models.RepositoryModel;
|
| | | import com.gitblit.models.TicketModel;
|
| | |
| | | import com.gitblit.utils.JGitUtils.MergeStatus;
|
| | | import com.gitblit.utils.RefLogUtils;
|
| | | import com.gitblit.utils.StringUtils;
|
| | | import com.google.common.collect.Lists;
|
| | |
|
| | |
|
| | | /**
|
| | |
| | | defaultBranch = getRepository().getBranch();
|
| | | } catch (Exception e) {
|
| | | LOGGER.error("failed to determine default branch for " + repository.name, e);
|
| | | }
|
| | |
|
| | | if (!StringUtils.isEmpty(getRepositoryModel().mergeTo)) {
|
| | | // repository settings specifies a default integration branch
|
| | | defaultBranch = Repository.shortenRefName(getRepositoryModel().mergeTo);
|
| | | }
|
| | |
|
| | | long ticketId = 0L;
|
| | |
| | | }
|
| | |
|
| | | if (isPatchsetRef(cmd.getRefName()) && processPatchsets) {
|
| | |
|
| | | if (ticketService == null) {
|
| | | sendRejection(cmd, "Sorry, the ticket service is unavailable and can not accept patchsets at this time.");
|
| | | continue;
|
| | |
| | | LOGGER.error("{} already has refs in the {} namespace",
|
| | | repository.name, Constants.R_FOR);
|
| | | sendRejection(cmd, "Sorry, a repository administrator will have to remove the {} namespace", Constants.R_FOR);
|
| | | continue;
|
| | | }
|
| | |
|
| | | if (cmd.getNewId().equals(ObjectId.zeroId())) {
|
| | | // ref deletion request
|
| | | if (cmd.getRefName().startsWith(Constants.R_TICKET)) {
|
| | | if (user.canDeleteRef(repository)) {
|
| | | batch.addCommand(cmd);
|
| | | } else {
|
| | | sendRejection(cmd, "Sorry, you do not have permission to delete {}", cmd.getRefName());
|
| | | }
|
| | | } else {
|
| | | sendRejection(cmd, "Sorry, you can not delete {}", cmd.getRefName());
|
| | | }
|
| | | continue;
|
| | | }
|
| | |
|
| | |
| | | patchsetRefCmd.setResult(Result.OK);
|
| | |
|
| | | // update the ticket branch ref
|
| | | RefUpdate ru = updateRef(patchsetCmd.getTicketBranch(), patchsetCmd.getNewId());
|
| | | RefUpdate ru = updateRef(
|
| | | patchsetCmd.getTicketBranch(),
|
| | | patchsetCmd.getNewId(),
|
| | | patchsetCmd.getPatchsetType());
|
| | | updateReflog(ru);
|
| | |
|
| | | TicketModel ticket = processPatchset(patchsetCmd);
|
| | |
| | | break;
|
| | | }
|
| | | }
|
| | | sendError("Sorry, {0} already merged {1} from ticket {2,number,0} to {3}!",
|
| | | if (mergeChange != null) {
|
| | | sendError("Sorry, {0} already merged {1} from ticket {2,number,0} to {3}!",
|
| | | mergeChange.author, mergeChange.patchset, number, ticket.mergeTo);
|
| | | }
|
| | | sendRejection(cmd, "Ticket {0,number,0} already resolved", number);
|
| | | return null;
|
| | | } else if (!StringUtils.isEmpty(ticket.mergeTo)) {
|
| | |
| | |
|
| | | if (patchset.commits > 1) {
|
| | | sendError("");
|
| | | sendError("To create a proposal ticket, please squash your commits and");
|
| | | sendError("provide a meaningful commit message with a short title &");
|
| | | sendError("an optional description/body.");
|
| | | sendError("You may not create a ''{0}'' branch proposal ticket from {1} commits!",
|
| | | forBranch, patchset.commits);
|
| | | sendError("");
|
| | | sendError(minTitle);
|
| | | sendError(maxTitle);
|
| | | // display an ellipsized log of the commits being pushed
|
| | | RevWalk walk = getRevWalk();
|
| | | walk.reset();
|
| | | walk.sort(RevSort.TOPO);
|
| | | int boundary = 3;
|
| | | int count = 0;
|
| | | try {
|
| | | walk.markStart(tipCommit);
|
| | | walk.markUninteresting(mergeBase);
|
| | |
|
| | | for (;;) {
|
| | |
|
| | | RevCommit c = walk.next();
|
| | | if (c == null) {
|
| | | break;
|
| | | }
|
| | |
|
| | | if (count < boundary || count >= (patchset.commits - boundary)) {
|
| | |
|
| | | walk.parseBody(c);
|
| | | sendError(" {0} {1}", c.getName().substring(0, shortCommitIdLen),
|
| | | StringUtils.trimString(c.getShortMessage(), 60));
|
| | |
|
| | | } else if (count == boundary) {
|
| | |
|
| | | sendError(" ... more commits ...");
|
| | |
|
| | | }
|
| | |
|
| | | 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);
|
| | | } finally {
|
| | | walk.close();
|
| | | }
|
| | |
|
| | | sendError("");
|
| | | sendRejection(cmd, "please squash to one commit");
|
| | | sendError("Possible Solutions:");
|
| | | sendError("");
|
| | | int solution = 1;
|
| | | String forSpec = cmd.getRefName().substring(Constants.R_FOR.length());
|
| | | if (forSpec.equals("default") || forSpec.equals("new")) {
|
| | | try {
|
| | | // determine other possible integration targets
|
| | | List<String> bases = Lists.newArrayList();
|
| | | for (Ref ref : getRepository().getRefDatabase().getRefs(Constants.R_HEADS).values()) {
|
| | | if (!ref.getName().startsWith(Constants.R_TICKET)
|
| | | && !ref.getName().equals(forBranchRef.getName())) {
|
| | | if (JGitUtils.isMergedInto(getRepository(), ref.getObjectId(), tipCommit)) {
|
| | | bases.add(Repository.shortenRefName(ref.getName()));
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | if (!bases.isEmpty()) {
|
| | |
|
| | | if (bases.size() == 1) {
|
| | | // suggest possible integration targets
|
| | | String base = bases.get(0);
|
| | | sendError("{0}. Propose this change for the ''{1}'' branch.", solution++, base);
|
| | | sendError("");
|
| | | sendError(" git push origin HEAD:refs/for/{0}", base);
|
| | | sendError(" pt propose {0}", base);
|
| | | sendError("");
|
| | | } else {
|
| | | // suggest possible integration targets
|
| | | sendError("{0}. Propose this change for a different branch.", solution++);
|
| | | sendError("");
|
| | | for (String base : bases) {
|
| | | sendError(" git push origin HEAD:refs/for/{0}", base);
|
| | | sendError(" pt propose {0}", base);
|
| | | sendError("");
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | } catch (IOException e) {
|
| | | LOGGER.error(null, e);
|
| | | }
|
| | | }
|
| | | sendError("{0}. Squash your changes into a single commit with a meaningful message.", solution++);
|
| | | sendError("");
|
| | | sendError("{0}. Open a ticket for your changes and then push your {1} commits to the ticket.",
|
| | | solution++, patchset.commits);
|
| | | sendError("");
|
| | | sendError(" git push origin HEAD:refs/for/{id}");
|
| | | sendError(" pt propose {id}");
|
| | | sendError("");
|
| | | sendRejection(cmd, "too many commits");
|
| | | return null;
|
| | | }
|
| | |
|
| | |
| | | RefLogUtils.updateRefLog(user, getRepository(),
|
| | | Arrays.asList(new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), cmd.getRefName())));
|
| | |
|
| | | // call any patchset hooks
|
| | | for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
|
| | | try {
|
| | | hook.onNewPatchset(ticket);
|
| | | } catch (Exception e) {
|
| | | LOGGER.error("Failed to execute extension", e);
|
| | | }
|
| | | }
|
| | |
|
| | | return ticket;
|
| | | } else {
|
| | | sendError("FAILED to create ticket");
|
| | |
| | | // log the new patchset ref
|
| | | RefLogUtils.updateRefLog(user, getRepository(),
|
| | | Arrays.asList(new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), cmd.getRefName())));
|
| | |
|
| | | // call any patchset hooks
|
| | | final boolean isNewPatchset = change.patchset.rev == 1;
|
| | | for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
|
| | | try {
|
| | | if (isNewPatchset) {
|
| | | hook.onNewPatchset(ticket);
|
| | | } else {
|
| | | hook.onUpdatePatchset(ticket);
|
| | | }
|
| | | } catch (Exception e) {
|
| | | LOGGER.error("Failed to execute extension", e);
|
| | | }
|
| | | }
|
| | |
|
| | | // return the updated ticket
|
| | | return ticket;
|
| | |
| | | }
|
| | |
|
| | | TicketModel ticket = ticketService.getTicket(repository, ticketNumber);
|
| | | if (ticket == null) {
|
| | | continue;
|
| | | }
|
| | | String integrationBranch;
|
| | | if (StringUtils.isEmpty(ticket.mergeTo)) {
|
| | | // unspecified integration branch
|
| | |
| | | psCmd.updateTicket(c, mergeTo, ticket, null);
|
| | |
|
| | | // create a ticket patchset ref
|
| | | updateRef(psCmd.getPatchsetBranch(), c.getId());
|
| | | RefUpdate ru = updateRef(psCmd.getTicketBranch(), c.getId());
|
| | | 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
|
| | |
| | |
|
| | | if (parseMessage) {
|
| | | // parse commit message looking for fixes/closes #n
|
| | | Pattern p = Pattern.compile("(?:fixes|closes)[\\s-]+#?(\\d+)", Pattern.CASE_INSENSITIVE);
|
| | | Matcher m = p.matcher(commit.getFullMessage());
|
| | | while (m.find()) {
|
| | | String val = m.group();
|
| | | return Long.parseLong(val);
|
| | | 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;
|
| | |
| | | LOGGER.error("failed to get commit count", e);
|
| | | return 0;
|
| | | } finally {
|
| | | walk.release();
|
| | | walk.close();
|
| | | }
|
| | | return count;
|
| | | }
|
| | |
| | | return newPatchset;
|
| | | }
|
| | |
|
| | | private RefUpdate updateRef(String ref, ObjectId newId) {
|
| | | private RefUpdate updateRef(String ref, ObjectId newId, PatchsetType type) {
|
| | | ObjectId ticketRefId = ObjectId.zeroId();
|
| | | try {
|
| | | ticketRefId = getRepository().resolve(ref);
|
| | |
| | | try {
|
| | | RefUpdate ru = getRepository().updateRef(ref, false);
|
| | | ru.setRefLogIdent(getRefLogIdent());
|
| | | ru.setForceUpdate(true);
|
| | | switch (type) {
|
| | | case Amend:
|
| | | case Rebase:
|
| | | case Rebase_Squash:
|
| | | case Squash:
|
| | | ru.setForceUpdate(true);
|
| | | break;
|
| | | default:
|
| | | break;
|
| | | }
|
| | |
|
| | | ru.setExpectedOldObjectId(ticketRefId);
|
| | | ru.setNewObjectId(newId);
|
| | | RefUpdate.Result result = ru.update(getRevWalk());
|
| | |
| | | if (ticket != null) {
|
| | | ticketNotifier.queueMailing(ticket);
|
| | |
|
| | | // update the reflog with the merge
|
| | | if (oldRef != null) {
|
| | | ReceiveCommand cmd = new ReceiveCommand(oldRef.getObjectId(),
|
| | | ObjectId.fromString(mergeResult.sha), oldRef.getName());
|
| | | RefLogUtils.updateRefLog(user, getRepository(), Arrays.asList(cmd));
|
| | | cmd.setResult(Result.OK);
|
| | | List<ReceiveCommand> commands = Arrays.asList(cmd);
|
| | |
|
| | | logRefChange(commands);
|
| | | updateIncrementalPushTags(commands);
|
| | | updateGitblitRefLog(commands);
|
| | | }
|
| | |
|
| | | // call patchset hooks
|
| | | for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
|
| | | try {
|
| | | hook.onMergePatchset(ticket);
|
| | | } catch (Exception e) {
|
| | | LOGGER.error("Failed to execute extension", e);
|
| | | }
|
| | | }
|
| | | return mergeResult.status;
|
| | | } else {
|