]> git.donarmstrong.com Git - bin.git/blob - stodo
fix a typo in stodo
[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} or not exists $ENV{TODODB};
103 pod2usage({verbose=>2}) if $options{man};
104
105 $DEBUG = $options{debug};
106
107 # Figure out what we're doing
108 my $tododb = $ENV{TODODB};
109 if ($tododb !~ m#^/#) {
110      $tododb = cwd().'/'.$tododb;
111 }
112 $tododb = full_readlink($tododb);
113 my $base_dir = dirname($tododb);
114 my $tododb_name = basename($tododb);
115
116 if (not -e "$base_dir/.svn") {
117      print "$base_dir/.svn doesn't exist, not commiting\n" unless $options{quiet};
118      exit 0;
119 }
120
121 # Make sure that we can ping the subversion server
122 my $svn_entries_fh = new IO::File "$base_dir/.svn/entries",'r'
123      or die "unable to open $base_dir/.svn/entries: $!";
124 my $url_type;
125 my $svn_host;
126 while (<$svn_entries_fh>) {
127      next unless m#^\s+url=\"(http|svn\+ssh|file):///?([^\/]+)#;
128      $url_type = $1;
129      last if $url_type eq 'file';
130      $svn_host = $2;
131      last;
132 }
133 if ($ENV{STODO_NO_COMMIT}) {
134      print "Exiting because of STODO_NO_COMMIT env variable\n" unless $options{quiet};
135      exit 0;
136 }
137 if (not defined $svn_host and $url_type ne 'file') {
138      die "Was unable to find which host the svn repository is located on";
139 }
140 if ($url_type ne 'file') {
141      qx(ping -q -c 3 $svn_host >/dev/null 2>&1);
142      if ($? != 0) {
143           print "Unable to ping $svn_host\n" unless $options{quiet};
144           exit 0 unless $options{simulate};
145      }
146 }
147
148
149 # See what has changed
150 #my $svn_fh = new IO::Handle;
151 #open($svn_fh,'-|','svn','diff',$tododb) or die "Unable to execute svn diff $tododb; $!";
152 #while (<$svn_fh>) {
153 #     next unless /^[-+]/;
154 #}
155
156 my $todo_db_old = fix_content(XMLin("$base_dir/.svn/text-base/${tododb_name}.svn-base",KeyAttr=>'time')->{note});
157 my $todo_db_new = fix_content(XMLin($tododb,KeyAttr=>'time')->{note});
158
159 my @commit_message;
160
161 my $diff = Diff($todo_db_new,$todo_db_old);
162
163 #use Data::Dumper;
164 #print Dumper($diff);
165
166 if (exists $diff->{uniq_a}) {
167      foreach (values %{$diff->{uniq_a}}) {
168           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$_}{qw(content priority)};
169           push @commit_message,qq( * Added todo: [$priority] $content);
170      }
171 }
172
173 if (exists $diff->{uniq_b}) {
174      foreach (values %{$diff->{uniq_b}}) {
175           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$_}{qw(content priority)};
176           push @commit_message,qq( * Deleted todo: [$priority] $content);
177      }
178 }
179
180 if (exists $diff->{diff}) {
181      # This entry has either been edited, completed, or uncompleted
182      foreach my $entry_key (keys %{$diff->{diff}}) {
183           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$todo_db_new->{$entry_key}}{qw(content priority)};
184           my $diff_entry = $diff->{diff}{$entry_key};
185           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_a}{done}) {
186                push @commit_message,qq( * Finished todo: [$priority] $content);
187           }
188           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_b}{done}) {
189                push @commit_message,qq( * Marked as undone: [$priority] $content);
190           }
191           if (exists $diff_entry->{diff}) {
192                my @what_changed;
193                if (exists $diff_entry->{diff}{content}) {
194                     push @what_changed,'content';
195                }
196                if (exists $diff_entry->{diff}{priority}) {
197                     push @what_changed,'priority';
198                }
199                if (not @what_changed) {
200                     @what_changed = keys %{$diff_entry->{diff}};
201                }
202                push @commit_message,qq( * Todo changed ).join(' and ',@what_changed).qq( [$priority] $content);
203           }
204      }
205 }
206
207 if (exists $diff->{same}) {
208      foreach my $entry_key (keys %{$diff->{same}}) {
209           my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$todo_db_new->{$entry_key}}{qw(content priority)};
210           my $diff_entry = $diff->{same}{$entry_key};
211           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_a}{done}) {
212                push @commit_message,qq( * Finished todo: [$priority] $content);
213           }
214           if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_b}{done}) {
215                push @commit_message,qq( * Marked as undone: [$priority] $content);
216           }
217           if (exists $diff_entry->{diff}) {
218                my @what_changed;
219                if (exists $diff_entry->{diff}{content}) {
220                     push @what_changed,'content';
221                }
222                if (exists $diff_entry->{diff}{priority}) {
223                     push @what_changed,'priority';
224                }
225                if (not @what_changed) {
226                     @what_changed = keys %{$diff_entry->{diff}};
227                }
228                push @commit_message,qq( * Todo changed ).join(' and ',@what_changed).qq( [$priority] $content);
229           }
230      }
231 }
232
233
234 if (not @commit_message) {
235      print "No commit message\n" if $DEBUG or $options{simulate};
236      exit 0;
237 }
238
239 my $commit_message = join('', map {qq($_\n)} @commit_message);
240 if ($options{simulate}) {
241      print "svn commit -F - $tododb <<EOF\n";
242      print $commit_message;
243      print "EOF\n";
244 }
245 else {
246      system('svn','commit','-m',$commit_message,$tododb);
247 }
248
249
250 # Commit the changes with an appropriate commit message
251
252
253 sub fix_content {
254      my ($s) = @_;
255      foreach my $entry (values %{$s}) {
256           if (exists $entry->{content}) {
257                $entry->{content} =~ s/^\s*|\s*$//g;
258           }
259           if (exists $entry->{comment}) {
260                $entry->{comment} =~ s/^\s*|\s*$//g;
261           }
262      }
263      return $s;
264 }
265
266
267 sub full_readlink {
268      my ($file) = @_;
269      while (-l $file) {
270           $file = readlink $file;
271      }
272
273      return $file;
274 }
275
276 __END__