1#!/usr/bin/env python
2#
3# Copyright (c) 2014, Craig Rodrigues <rodrigc@FreeBSD.org>
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice unmodified, this list of conditions, and the following
11#    disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#     documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# $FreeBSD$
28
29# Display SVN log entries for changesets which have files which were
30# Added or Deleted.
31# This script takes arguments which would normally be
32# passed to the "svn log" command.
33#
34# Examples:
35#
36#  (1) Display all new changesets in stable/10 branch:
37#
38#        list-new-changesets.py --stop-on-copy \
39#                        svn://svn.freebsd.org/base/stable/10
40#
41#  (2) Display all new changesets between r254153 and r261794 in
42#      stable/9 branch:
43#
44#        list-new-changesets.py  -r254153:261794 \
45#                        svn://svn.freebsd.org/base/stable/9
46
47from __future__ import print_function
48import os
49import subprocess
50import sys
51import xml.etree.ElementTree
52
53def print_logentry(logentry):
54    """Print an SVN log entry.
55
56    Take an SVN log entry formatted in XML, and print it out in
57    plain text.
58    """
59    rev = logentry.attrib['revision']
60    author = logentry.find('author').text
61    date = logentry.find('date').text
62    msg = logentry.find('msg').text
63
64    print("-" * 71)
65    print("%s | %s | %s" % (rev, author, date))
66    print("Changed paths:")
67    for paths in logentry.findall('paths'):
68        for path in paths.findall('path'):
69            print("   %s %s" % (path.attrib['action'], path.text))
70
71    print()
72    print(msg.encode('utf-8'))
73
74def main(args):
75    """Main function.
76
77    Take command-line arguments which would be passed to 'svn log'.
78    Prepend '-v --xml' to get verbose XML formatted output.
79    Only display entries which have Added or Deleted files.
80    """
81    cmd = ["svn", "log", "-v", "--xml"]
82    cmd += args[1:]
83
84    print(" ".join(cmd))
85
86    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
87    (out, err) = proc.communicate()
88
89    if proc.returncode != 0:
90        print(err)
91        sys.exit(proc.returncode)
92
93    displayed_entries = 0
94    root = xml.etree.ElementTree.fromstring(out)
95
96    for logentry in root.findall('logentry'):
97       show_logentry = False
98
99       for paths in logentry.findall('paths'):
100           for path in paths.findall('path'):
101               if path.attrib['action'] == 'A':
102                  show_logentry = True
103               elif path.attrib['action'] == 'D':
104                  show_logentry = True
105
106       if show_logentry == True :
107           print_logentry(logentry)
108           displayed_entries += 1
109
110    if displayed_entries == 0:
111        print("No changesets with Added or Deleted files")
112
113    if displayed_entries > 0:
114        print("-" * 71)
115
116
117if __name__ == "__main__":
118    main(sys.argv)
119