Merged in my git.py changed fixing Hubert Chathi's git set-root bug (0cad).
[be.git] / libbe / diff.py
1 # Copyright (C) 2005 Aaron Bentley and Panometrics, Inc.
2 # <abentley@panoramicfeedback.com>
3 #
4 #    This program is free software; you can redistribute it and/or modify
5 #    it under the terms of the GNU General Public License as published by
6 #    the Free Software Foundation; either version 2 of the License, or
7 #    (at your option) any later version.
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program; if not, write to the Free Software
16 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 """Compare two bug trees"""
18 from libbe import cmdutil, bugdir
19 from libbe.utility import time_to_str
20 from libbe.bug import cmp_severity
21
22 def diff(old_tree, new_tree):
23     old_bug_map = old_tree.bug_map()
24     new_bug_map = new_tree.bug_map()
25     added = []
26     removed = []
27     modified = []
28     for old_bug in old_bug_map.itervalues():
29         new_bug = new_bug_map.get(old_bug.uuid)
30         if new_bug is None :
31             removed.append(old_bug)
32         else:
33             if old_bug != new_bug:
34                 modified.append((old_bug, new_bug))
35     for new_bug in new_bug_map.itervalues():
36         if not old_bug_map.has_key(new_bug.uuid):
37             added.append(new_bug)
38     return (removed, modified, added)
39
40
41 def reference_diff(bugdir, spec=None):
42     return diff(bugdir.get_reference_bugdir(spec), bugdir)
43     
44 def diff_report(diff_data, bug_dir):
45     (removed, modified, added) = diff_data
46     bugs = list(bug_dir.list())
47     def modified_cmp(left, right):
48         return cmp_severity(left[1], right[1])
49
50     added.sort(cmp_severity)
51     removed.sort(cmp_severity)
52     modified.sort(modified_cmp)
53
54     if len(added) > 0: 
55         print "New bug reports:"
56         for bug in added:
57             print bug.string(shortlist=True)
58
59     if len(modified) > 0:
60         printed = False
61         for old_bug, new_bug in modified:
62             change_str = bug_changes(old_bug, new_bug, bugs)
63             if change_str is None:
64                 continue
65             if not printed:
66                 printed = True
67                 print "Modified bug reports:"
68             print change_str
69
70     if len(removed) > 0: 
71         print "Removed bug reports:"
72         for bug in removed:
73             print bug.string(bugs, shortlist=True)
74    
75 def change_lines(old, new, attributes):
76     change_list = []    
77     for attr in attributes:
78         old_attr = getattr(old, attr)
79         new_attr = getattr(new, attr)
80         if old_attr != new_attr:
81             change_list.append((attr, old_attr, new_attr))
82     if len(change_list) >= 0:
83         return change_list
84     else:
85         return None
86
87 def bug_changes(old, new, bugs):
88     change_list = change_lines(old, new, ("time", "creator", "severity",
89     "target", "summary", "status", "assigned"))
90
91     old_comment_ids = list(old.iter_comment_ids())
92     new_comment_ids = list(new.iter_comment_ids())
93     change_strings = ["%s: %s -> %s" % f for f in change_list]
94     for comment_id in new_comment_ids:
95         if comment_id not in old_comment_ids:
96             summary = comment_summary(new.get_comment(comment_id), "new")
97             change_strings.append(summary)
98     for comment_id in old_comment_ids:
99         if comment_id not in new_comment_ids:
100             summary = comment_summary(new.get_comment(comment_id), "removed")
101             change_strings.append(summary)
102
103     if len(change_strings) == 0:
104         return None
105     return "%s%s\n" % (new.string(bugs, shortlist=True), 
106                        "\n".join(change_strings))
107
108
109 def comment_summary(comment, status):
110     return "%8s comment from %s on %s" % (status, comment.From, 
111                                           time_to_str(comment.time))