]> git.donarmstrong.com Git - bin.git/blob - stodo
remove convert_to_xls and .sa_bin
[bin.git] / stodo
1 #! /usr/bin/perl
2 # stodo commits the changes made to a subversion backed todo db, and
3 # is released under the terms of the GPL version 2, or any later
4 # version, at your option. See the file README and COPYING for more
5 # information. Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
6 # $Id$
7
8
9 use warnings;
10 use strict;
11
12
13 use Getopt::Long;
14 use Pod::Usage;
15
16 =head1 NAME
17
18 stodo - devtodo event script which attempts to commit the changes made to a .todo file after a change
19
20 =head1 SYNOPSIS
21
22 stodo [options]
23
24  Options:
25   --simulate, -s simulate: don't actually do anything
26   --quiet, -q don't output to STDOUT
27   --debug, -d debugging level (Default 0)
28   --help, -h display this help
29   --man, -m display manual
30
31 =head1 OPTIONS
32
33 =over
34
35 =item B<--simulate, -s>
36
37 Simulation mode, don't actually commit anything.
38
39 =item B<--quiet, -q>
40
41 Don't output anything to STDOUT
42
43 =item B<--debug, -d>
44
45 Debug verbosity. (Default 0)
46
47 =item B<--help, -h>
48
49 Display brief useage information.
50
51 =item B<--man, -m>
52
53 Display this manual.
54
55 =back
56
57 =head1 ENVIRONMENTAL VARIABLES
58
59 =over
60
61 =item B<TODODB>
62
63 Todo databse location, set by devtodo
64
65 =item B<STODO_NO_COMMIT>
66
67 If set, stodo assumes that there is no network, and doesn't commit
68
69 =back
70
71 =head1 EXAMPLES
72
73  on save {
74     exec stodo
75  }
76
77
78 =cut
79
80
81
82 use Cwd qw(cwd);
83 use IO::File;
84 use XML::Simple;
85 use IO::Handle;
86 use Data::Diff qw(Diff);
87 use File::Basename qw(dirname basename);
88
89 use vars qw($DEBUG);
90
91 # XXX parse config file
92
93 my %options = (quiet           => 0,
94                debug           => 0,
95                help            => 0,
96                man             => 0,
97                simulate        => 0,
98               );
99
100 GetOptions(\%options,'debug|d+','help|h|?','man|m','quiet|q+','simulate|s+');
101
102 pod2usage() if $options{help};
103 pod2usage({verbose=>2}) if $options{man};
104
105 $DEBUG = $options{debug};
106
107 # Figure out what we're doing
108 my $tododb = exists $ENV{TODODB} ? $ENV{TODODB} : '.todo';
109 if ($tododb !~ m#^/#) {
110      $tododb = cwd().'/'.$tododb;
111 }
112 if (not -r $tododb) {
113      print STDERR "Todo DB '$tododb' does not exist or is not readable\n";
114      exit 1;
115 }
116 $tododb = full_readlink($tododb);
117 my $base_dir = dirname($tododb);
118 my $tododb_name = basename($tododb);
119
120 if (not -e "$base_dir/.svn") {
121      print STDERR "$base_dir/.svn doesn't exist, not commiting\n" unless $options{quiet};
122      exit 0;
123 }
124
125 if (not -e "$base_dir/.svn/text-base/${tododb_name}.svn-base") {
126      print STDERR "$base_dir/.svn/text-base/${tododb_name}.svn-base doesn't exist, not committing\n" unless $options{quiet};
127      exit 0;
128 }
129
130 # Make sure that we can ping the subversion server
131 my $svn_entries_fh = new IO::File "$base_dir/.svn/entries",'r'
132      or die "unable to open $base_dir/.svn/entries: $!";
133 my $url_type;
134 my $svn_host;
135 while (<$svn_entries_fh>) {
136      next unless m#^(?:\s+url=\")?(http|svn\+ssh|file):///?([^\/]+)#;
137      $url_type = $1;
138      last if $url_type eq 'file';
139      $svn_host = $2;
140      last;
141 }
142 if ($ENV{STODO_NO_COMMIT}) {
143      print STDERR "Exiting because of STODO_NO_COMMIT env variable\n" unless $options{quiet};
144      exit 0;
145 }
146 if (not defined $svn_host and (not defined $url_type or $url_type ne 'file')) {
147      die "Was unable to find which host the svn repository is located on";
148 }
149 if ($url_type ne 'file') {
150      qx(ping -q -c 3 $svn_host >/dev/null 2>&1);
151      if ($? != 0) {
152           print "Unable to ping $svn_host\n" unless $options{quiet};
153           exit 0 unless $options{simulate};
154      }
155 }
156
157
158 # See what has changed
159 #my $svn_fh = new IO::Handle;
160 #open($svn_fh,'-|','svn','diff',$tododb) or die "Unable to execute svn diff $tododb; $!";
161 #while (<$svn_fh>) {
162 #     next unless /^[-+]/;
163 #}
164
165 my $todo_db_old = fix_content(XMLin("$base_dir/.svn/text-base/${tododb_name}.svn-base",KeyAttr=>'time')->{note});
166 my $todo_db_new = fix_content(XMLin($tododb,KeyAttr=>'time')->{note});
167
168 my @commit_message;
169
170 my $diff = Diff($todo_db_new,$todo_db_old);
171
172 #use Data::Dumper;
173 #print Dumper($diff);
174
175 if (exists $diff->{uniq_a}) {
176      foreach (values %{$diff->{uniq_a}}) {
177           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$_}{qw(content priority)};
178           push @commit_message,qq( * Added todo: [$priority] $content);
179      }
180 }
181
182 if (exists $diff->{uniq_b}) {
183      foreach (values %{$diff->{uniq_b}}) {
184           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$_}{qw(content priority)};
185           push @commit_message,qq( * Deleted todo: [$priority] $content);
186      }
187 }
188
189 if (exists $diff->{diff}) {
190      # This entry has either been edited, completed, or uncompleted
191      foreach my $entry_key (keys %{$diff->{diff}}) {
192           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$todo_db_new->{$entry_key}}{qw(content priority)};
193           my $diff_entry = $diff->{diff}{$entry_key};
194           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_a}{done}) {
195                push @commit_message,qq( * Finished todo: [$priority] $content);
196           }
197           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_b}{done}) {
198                push @commit_message,qq( * Marked as undone: [$priority] $content);
199           }
200           if (exists $diff_entry->{diff}) {
201                my @what_changed;
202                if (exists $diff_entry->{diff}{content}) {
203                     push @what_changed,'content';
204                }
205                if (exists $diff_entry->{diff}{priority}) {
206                     push @what_changed,'priority';
207                }
208                if (not @what_changed) {
209                     @what_changed = keys %{$diff_entry->{diff}};
210                }
211                push @commit_message,qq( * Todo changed ).join(' and ',@what_changed).qq( [$priority] $content);
212           }
213      }
214 }
215
216 if (exists $diff->{same}) {
217      foreach my $entry_key (keys %{$diff->{same}}) {
218           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$todo_db_new->{$entry_key}}{qw(content priority)};
219           my $diff_entry = $diff->{same}{$entry_key};
220           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_a}{done}) {
221                push @commit_message,qq( * Finished todo: [$priority] $content);
222           }
223           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_b}{done}) {
224                push @commit_message,qq( * Marked as undone: [$priority] $content);
225           }
226           if (exists $diff_entry->{diff}) {
227                my @what_changed;
228                if (exists $diff_entry->{diff}{content}) {
229                     push @what_changed,'content';
230                }
231                if (exists $diff_entry->{diff}{priority}) {
232                     push @what_changed,'priority';
233                }
234                if (not @what_changed) {
235                     @what_changed = keys %{$diff_entry->{diff}};
236                }
237                push @commit_message,qq( * Todo changed ).join(' and ',@what_changed).qq( [$priority] $content);
238           }
239      }
240 }
241
242
243 if (not @commit_message) {
244      print "No commit message\n" if $DEBUG or $options{simulate};
245      exit 0;
246 }
247
248 my $commit_message = join('', map {qq($_\n)} @commit_message);
249 if ($options{simulate}) {
250      print "svn commit -F - $tododb <<EOF\n";
251      print $commit_message;
252      print "EOF\n";
253 }
254 else {
255      system('svn','commit','-m',$commit_message,$tododb);
256 }
257
258
259 # Commit the changes with an appropriate commit message
260
261
262 sub fix_content {
263      my ($s) = @_;
264      if (not ref(${[values %{$s}]}[0]) eq 'HASH') {
265           $s = {$s->{time} => $s};
266      }
267      foreach my $entry (values %{$s}) {
268           if (exists $entry->{content}) {
269                $entry->{content} =~ s/^\s*|\s*$//g;
270           }
271           if (exists $entry->{comment}) {
272                $entry->{comment} =~ s/^\s*|\s*$//g;
273           }
274      }
275      return $s;
276 }
277
278
279 sub full_readlink {
280      my ($file) = @_;
281      while (-l $file) {
282           $file = readlink $file;
283      }
284
285      return $file;
286 }
287
288 __END__