From aa381344fa8637eaf1f4bb8dbe41fe786f78b56b Mon Sep 17 00:00:00 2001 From: Andrew Chilton Date: Tue, 1 Jul 2008 23:17:26 +1200 Subject: [PATCH] Added ability to use a shortened hash for issues et al (closes #bf9badb4). --- bin/cil | 93 ++++++++++++++++++++++++++++++++----------- issues/c_95e81a14.cil | 16 ++++++++ issues/i_bf9badb4.cil | 5 ++- lib/CIL.pm | 24 ++++++++++- 4 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 issues/c_95e81a14.cil diff --git a/bin/cil b/bin/cil index 8cbb57e..15d8eac 100755 --- a/bin/cil +++ b/bin/cil @@ -199,10 +199,7 @@ sub cmd_show { my ($cil, undef, $issue_name) = @_; # firstly, read the issue in - my $issue = CIL::Issue->new_from_name($cil, $issue_name); - unless ( defined $issue ) { - fatal("Couldn't load issue '$issue_name'"); - } + my $issue = load_issue_fuzzy( $cil, $issue_name ); display_issue_full($cil, $issue); } @@ -214,10 +211,7 @@ sub cmd_status { } # firstly, read the issue in - my $issue = CIL::Issue->new_from_name($cil, $issue_name); - unless ( defined $issue ) { - fatal("Couldn't load issue '$issue_name'"); - } + my $issue = load_issue_fuzzy( $cil, $issue_name ); # set the status for this issue $issue->Status( $status ); @@ -243,10 +237,7 @@ sub cmd_add { sub cmd_edit { my ($cil, undef, $issue_name) = @_; - my $issue = CIL::Issue->new_from_name($cil, $issue_name); - unless ( defined $issue ) { - fatal("Couldn't load issue '$issue_name'"); - } + my $issue = load_issue_fuzzy( $cil, $issue_name ); CIL::Utils->ensure_interactive(); @@ -279,10 +270,7 @@ sub cmd_edit { sub cmd_comment { my ($cil, undef, $issue_name) = @_; - my $issue = CIL::Issue->new_from_name($cil, $issue_name); - unless ( defined $issue ) { - fatal("couldn't load issue '$issue_name'"); - } + my $issue = load_issue_fuzzy( $cil, $issue_name ); CIL::Utils->ensure_interactive(); @@ -298,10 +286,7 @@ sub cmd_comment { sub cmd_attach { my ($cil, undef, $issue_name, $filename) = @_; - my $issue = CIL::Issue->new_from_name($cil, $issue_name); - unless ( defined $issue ) { - fatal("couldn't load issue '$issue_name'"); - } + my $issue = load_issue_fuzzy( $cil, $issue_name ); # check to see if the file exists unless ( -r $filename ) { @@ -351,10 +336,7 @@ EOF sub cmd_extract { my ($cil, $args, $attachment_name) = @_; - my $attachment = CIL::Attachment->new_from_name($cil, $attachment_name); - unless ( defined $attachment ) { - fatal("Couldn't load attachment '$attachment_name'"); - } + my $attachment = load_attachment_fuzzy($cil, $attachment_name); my $filename = $args->{f} || $attachment->Filename(); write_file( $filename, $attachment->as_binary ); @@ -570,6 +552,69 @@ sub cmd_am { ## ---------------------------------------------------------------------------- # helpers +sub load_issue_fuzzy { + my ($cil, $partial_name) = @_; + + my $issues = $cil->list_issues_fuzzy( $partial_name ); + unless ( defined $issues ) { + fatal("Couldn't find any issues using '$partial_name'"); + } + + if ( @$issues > 1 ) { + fatal('found multiple issues which match that name: ' . join(' ', map { $_->{name} } @$issues)); + } + + my $issue_name = $issues->[0]->{name}; + my $issue = CIL::Issue->new_from_name($cil, $issue_name); + unless ( defined $issue ) { + fatal("Couldn't load issue '$issue_name'"); + } + + return $issue; +} + +sub load_comment_fuzzy { + my ($cil, $partial_name) = @_; + + my $comments = $cil->list_comments_fuzzy( $partial_name ); + unless ( defined $comments ) { + fatal("Couldn't find any comments using '$partial_name'"); + } + + if ( @$comments > 1 ) { + fatal('found multiple comments which match that name: ' . join(' ', map { $_->{name} } @$comments)); + } + + my $comment_name = $comments->[0]->{name}; + my $comment = CIL::comment->new_from_name($cil, $comment_name); + unless ( defined $comment ) { + fatal("Couldn't load comment '$comment_name'"); + } + + return $comment; +} + +sub load_attachment_fuzzy { + my ($cil, $partial_name) = @_; + + my $attachments = $cil->list_attachments_fuzzy( $partial_name ); + unless ( defined $attachments ) { + fatal("Couldn't find any attachments using '$partial_name'"); + } + + if ( @$attachments > 1 ) { + fatal('found multiple attachments which match that name: ' . join(' ', map { $_->{name} } @$attachments)); + } + + my $attachment_name = $attachments->[0]->{name}; + my $attachment = CIL::Attachment->new_from_name($cil, $attachment_name); + unless ( defined $attachment ) { + fatal("Couldn't load attachment '$partial_name'"); + } + + return $attachment; +} + sub ans { my ($msg) = @_; print $msg; diff --git a/issues/c_95e81a14.cil b/issues/c_95e81a14.cil new file mode 100644 index 0000000..27bdf26 --- /dev/null +++ b/issues/c_95e81a14.cil @@ -0,0 +1,16 @@ +Issue: bf9badb4 +CreatedBy: Andrew Chilton +Inserted: 2008-07-01T10:59:42 +Updated: 2008-07-01T11:00:46 + +Now allows you to put in issue names as shortened versions of their full names. +Also attachment names too. Provisions have already been added in case we want +to do comment names too. + +e.g. + + cil show cafe + cil edit cafe + cil extract dead + +and the like. diff --git a/issues/i_bf9badb4.cil b/issues/i_bf9badb4.cil index d837628..065c8d0 100644 --- a/issues/i_bf9badb4.cil +++ b/issues/i_bf9badb4.cil @@ -1,11 +1,12 @@ Summary: Allow shortened hash names -Status: New +Status: Finished CreatedBy: Francois Marier AssignedTo: Andrew Chilton Label: Milestone-v0.4 Label: Type-Enhancement +Comment: 95e81a14 Inserted: 2008-06-29T13:20:11 -Updated: 2008-06-29T12:33:18 +Updated: 2008-07-01T11:15:35 How about doing the same thing that git is doing for commit hashes: if you only type the first few characters of the hash, then as long as it's not diff --git a/lib/CIL.pm b/lib/CIL.pm index bd01a88..f64d67a 100644 --- a/lib/CIL.pm +++ b/lib/CIL.pm @@ -59,9 +59,11 @@ sub new { } sub list_entities { - my ($self, $prefix) = @_; + my ($self, $prefix, $base) = @_; - my $globpath = $self->IssueDir . "/${prefix}_*.cil"; + $base = '' unless defined $base; + + my $globpath = $self->IssueDir . "/${prefix}_${base}*.cil"; my @filenames = bsd_glob($globpath); my @entities; @@ -93,6 +95,24 @@ sub list_attachments { return $self->list_entities('a'); } +sub list_issues_fuzzy { + my ($self, $partial_name) = @_; + + return $self->list_entities('i', $partial_name); +} + +sub list_comments_fuzzy { + my ($self, $partial_name) = @_; + + return $self->list_entities('c', $partial_name); +} + +sub list_attachments_fuzzy { + my ($self, $partial_name) = @_; + + return $self->list_entities('a', $partial_name); +} + sub get_issues { my ($self) = @_; -- 2.39.5