chiark / gitweb /
Discard exitcode of subprocess in a better way
[stgit] / stgit / commands / log.py
CommitLineData
64354a2d
CM
1__copyright__ = """
2Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
18import sys, os, time
19from optparse import OptionParser, make_option
20from pydoc import pager
21from stgit.commands.common import *
22from stgit import stack, git
5e888f30 23from stgit.out import *
edddb249 24from stgit.run import Run
64354a2d
CM
25
26help = 'display the patch changelog'
27usage = """%prog [options] [patch]
28
29List all the current and past commit ids of the given patch. The
30--graphical option invokes gitk instead of printing. The changelog
31commit messages have the form '<action> <new-patch-id>'. The <action>
32can be one of the following:
33
34 new - new patch created
35 refresh - local changes were added to the patch
36 push - the patch was cleanly pushed onto the stack
37 push(m) - the patch was pushed onto the stack with a three-way merge
38 push(f) - the patch was fast-forwarded
39 undo - the patch boundaries were restored to the old values
40
06848fab
CM
41Note that only the diffs shown in the 'refresh', 'undo' and 'sync'
42actions are meaningful for the patch changes. The 'push' actions
43represent the changes to the entire base of the current
44patch. Conflicts reset the patch content and a subsequent 'refresh'
45will show the entire patch."""
64354a2d 46
6dd8fafa 47directory = DirectoryHasRepository()
64354a2d
CM
48options = [make_option('-b', '--branch',
49 help = 'use BRANCH instead of the default one'),
50 make_option('-p', '--patch',
51 help = 'show the refresh diffs',
52 action = 'store_true'),
380fde12
CM
53 make_option('-n', '--number', type = 'int',
54 help = 'limit the output to NUMBER commits'),
eff17c6b
CM
55 make_option('-f', '--full',
56 help = 'show the full commit ids',
57 action = 'store_true'),
64354a2d
CM
58 make_option('-g', '--graphical',
59 help = 'run gitk instead of printing',
60 action = 'store_true')]
61
eff17c6b 62def show_log(log, options):
64354a2d
CM
63 """List the patch changelog
64 """
65 commit = git.get_commit(log)
380fde12
CM
66 if options.number != None:
67 n = options.number
68 else:
69 n = -1
70 diff_list = []
64354a2d 71 while commit:
380fde12
CM
72 if n == 0:
73 # limit the output
74 break
75
eff17c6b
CM
76 log = commit.get_log().split('\n')
77
78 cmd_rev = log[0].split()
79 if len(cmd_rev) >= 2:
80 cmd = cmd_rev[0]
81 rev = cmd_rev[1]
82 elif len(cmd_rev) == 1:
83 cmd = cmd_rev[0]
84 rev = ''
85 else:
86 cmd = rev = ''
64354a2d 87
eff17c6b 88 if options.patch:
8f6ad921 89 if cmd in ['refresh', 'undo', 'sync', 'edit']:
380fde12
CM
90 diff_list.append(git.pretty_commit(commit.get_id_hash()))
91
92 # limiter decrement
93 n -= 1
64354a2d 94 else:
eff17c6b
CM
95 if len(log) >= 3:
96 notes = log[2]
97 else:
98 notes = ''
64354a2d
CM
99 author_name, author_email, author_date = \
100 name_email_date(commit.get_author())
101 secs, tz = author_date.split()
102 date = '%s %s' % (time.ctime(int(secs)), tz)
103
eff17c6b
CM
104 if options.full:
105 out.stdout('%-7s %-40s %s' % (cmd[:7], rev[:40], date))
106 else:
107 out.stdout('%-8s [%-7s] %-28s %s' % \
108 (rev[:8], cmd[:7], notes[:28], date))
64354a2d 109
380fde12
CM
110 # limiter decrement
111 n -= 1
112
64354a2d
CM
113 parent = commit.get_parent()
114 if parent:
115 commit = git.get_commit(parent)
116 else:
117 commit = None
118
380fde12
CM
119 if options.patch and diff_list:
120 pager('\n'.join(diff_list).rstrip())
64354a2d
CM
121
122def func(parser, options, args):
123 """Show the patch changelog
124 """
125 if len(args) == 0:
126 name = crt_series.get_current()
127 if not name:
128 raise CmdException, 'No patches applied'
129 elif len(args) == 1:
130 name = args[0]
cc9888a8
YD
131 if not name in crt_series.get_applied() + crt_series.get_unapplied() + \
132 crt_series.get_hidden():
64354a2d
CM
133 raise CmdException, 'Unknown patch "%s"' % name
134 else:
135 parser.error('incorrect number of arguments')
136
137 patch = crt_series.get_patch(name)
138
139 log = patch.get_log()
140 if not log:
141 raise CmdException, 'No changelog for patch "%s"' % name
142
143 if options.graphical:
289687b4 144 Run('gitk', log).discard_exitcode().run()
64354a2d 145 else:
eff17c6b 146 show_log(log, options)