[16] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Syntax: MergeActor.py ticketnum component version requestor |
---|
| 4 | |
---|
| 5 | Merge a branch to its trunk. |
---|
| 6 | """ |
---|
| 7 | |
---|
| 8 | import os |
---|
[17] | 9 | import shutil |
---|
[16] | 10 | |
---|
[17] | 11 | from mergebot import SvnOps |
---|
| 12 | from mergebot.Actor import Actor |
---|
[16] | 13 | |
---|
[17] | 14 | class MergeActor(Actor): |
---|
| 15 | """Merges a branch to the line of development on which it is based. |
---|
| 16 | """ |
---|
| 17 | def execute(self): |
---|
| 18 | "Merge a branch to its trunk" |
---|
| 19 | results = {} |
---|
| 20 | logfile = self.logfilename() |
---|
| 21 | checkoutdir = self.work_dir |
---|
| 22 | # Delete the working directory so we get a completely clean working |
---|
| 23 | # copy. |
---|
| 24 | if os.path.exists(checkoutdir): |
---|
| 25 | shutil.rmtree(checkoutdir) |
---|
[16] | 26 | |
---|
[17] | 27 | # Make sure the various urls we require do exist |
---|
| 28 | if not SvnOps.get_branch_info(self.local_url(), logfile): |
---|
| 29 | comment = 'Component %s does not exist in the repository.' \ |
---|
| 30 | % self.component |
---|
| 31 | return results, comment, False |
---|
| 32 | if not SvnOps.get_branch_info(self.local_url() + '/branches', logfile): |
---|
| 33 | comment = 'No directory in which to create branches for component %s in the repository.' % self.component |
---|
| 34 | return results, comment, False |
---|
| 35 | if not SvnOps.get_branch_info(self.baseline_local_url(), logfile): |
---|
| 36 | comment = 'Version %s for component %s does not exist in the repository.' % (self.version, self.component) |
---|
| 37 | return results, comment, False |
---|
[16] | 38 | |
---|
[17] | 39 | rev_info = SvnOps.get_branch_info(self.branch_local_url(), logfile) |
---|
| 40 | if not rev_info: |
---|
| 41 | comment = 'Branch for ticket %s does not exist in the repository.' % (self.ticket) |
---|
| 42 | return results, comment, False |
---|
| 43 | startrev, endrev = rev_info |
---|
[16] | 44 | |
---|
[17] | 45 | SvnOps.checkout(self.baseline_local_url(), checkoutdir, logfile) |
---|
| 46 | # FIXME: check return code |
---|
| 47 | merge_results = SvnOps.merge(self.branch_local_url(), checkoutdir, |
---|
| 48 | (startrev, endrev), logfile) |
---|
| 49 | conflicts = SvnOps.conflicts_from_merge_results(merge_results) |
---|
| 50 | if conflicts: |
---|
| 51 | comment = "\n".join([ |
---|
| 52 | "Found %s conflicts in attempt to merge %s:%s to %s for %s." % \ |
---|
| 53 | (len(conflicts), startrev, endrev, self.version, |
---|
| 54 | self.requestor), |
---|
| 55 | "Files in conflict:", |
---|
| 56 | "{{{", |
---|
| 57 | "\n".join(conflicts), |
---|
| 58 | "}}}", |
---|
| 59 | "A rebranch will be needed before this can be merged.", |
---|
[16] | 60 | ]) |
---|
[17] | 61 | results['mergebotstate'] = 'conflicts' |
---|
| 62 | success = False |
---|
| 63 | else: |
---|
| 64 | # The merge worked, so commit the changes. |
---|
| 65 | commitmessage = "\n".join([ |
---|
| 66 | "Ticket #%s: %s" % (self.ticket, self.summary), |
---|
| 67 | " Merge of %s:%s to %s for %s." % (startrev, endrev, |
---|
| 68 | self.version, self.requestor), |
---|
[16] | 69 | ]) |
---|
[17] | 70 | committedrev = SvnOps.commit(checkoutdir, commitmessage, logfile) |
---|
| 71 | # Sed message and endstatus |
---|
| 72 | if committedrev == None: |
---|
| 73 | # Apparently nothing to commit. |
---|
| 74 | comment = "\n".join([ |
---|
| 75 | "Merged %s:%s to %s for %s." % (startrev, endrev, |
---|
| 76 | self.version, self.requestor), |
---|
| 77 | "No changes to commit.", |
---|
| 78 | ]) |
---|
| 79 | results['mergebotstate'] = 'merged' |
---|
| 80 | success = True |
---|
| 81 | elif committedrev >= 0: |
---|
| 82 | # The commit succeeded. |
---|
| 83 | comment = "\n".join([ |
---|
| 84 | "Merged %s:%s to %s for %s." % (startrev, endrev, |
---|
| 85 | self.version, self.requestor), |
---|
| 86 | "Changeset [%s]. [source:%s/%s@%s]" % (committedrev, |
---|
| 87 | self.component, self.version_subdir(), committedrev), |
---|
| 88 | ]) |
---|
| 89 | results['mergebotstate'] = 'merged' |
---|
| 90 | success = True |
---|
| 91 | else: |
---|
| 92 | # The commit for the merge failed. |
---|
| 93 | comment = \ |
---|
| 94 | "Commit failed in attempt to merge %s:%s to %s for %s." \ |
---|
| 95 | % (startrev, endrev, self.version, self.requestor) |
---|
| 96 | #results['mergebotstate'] = 'mergefailed' |
---|
| 97 | success = False |
---|
[16] | 98 | |
---|
[17] | 99 | # Clean up the work area |
---|
| 100 | if os.path.exists(checkoutdir): |
---|
| 101 | shutil.rmtree(checkoutdir) |
---|
[16] | 102 | |
---|
[17] | 103 | return results, comment, success |
---|
[16] | 104 | |
---|
| 105 | # vim:foldcolumn=4 foldmethod=indent |
---|
| 106 | # vim:tabstop=4 shiftwidth=4 softtabstop=4 expandtab |
---|