[1+1=2]

OneAndOneIs2

« Cannot copeSome more little tweaks »

Tue, Oct 09, 2012

[Icon][Icon]My friend vic

• Post categories: Omni, Technology, My Life, Programming, Helpful

I noticed a while ago a frequent trend in my working habits: I would frequently want to edit the same files that I had worked on in a previous git commit.

The reasons were varied: I might have committed work in progress so I could switch branches, and then come back and want to start working on those same files again; I might have introduced a new method in one commit and want to change its name in another; I might want to update the history & version number of a module which always means editing the same files..

So rather than keep messing around with copy&paste, I wrote a little perl script to do it all for me. It can handle opening the most recent commit (by default), any specified commit, or all the files that you've edited but not committed.

It saves me a little bit of time, frequently enough that I can't bear not having it available any more :)

See below for code, if you're interested...

...

#!/usr/bin/env perl

use v5.12;
use warnings;

my $arg = $ARGV[0] || '';
my @files;

# If 'd' was passed in, get the files that make the current repo dirty
if ($arg eq 'd') {
# We only need the 'short' output - basically the list of files
my @gitstat = `git status -s`;
# Filter down to just the filenames we want - i.e. the second 'argument'
@files = map { s/^\s*\S*\s+(\S+)/$1/; $_; } @gitstat;
}
else {
# Get last/specified git commit
my @gitstat = `git show --stat $arg`;
# Filter down to just the filenames we want
# Filenames are in the format ' <name> | <number> +-
@files = map { s#^\s+(\S+)\s.*#$1#; $_ } grep { m#^\s\S+\s+\|\s+\d+\s+[+-]+# } @gitstat;
}

# Don't go crazy on big commits
if (my $num = scalar @files > 9) {
say "There are $num files, are you sure you want to do this?";
my $answer = <>;
exit unless $answer =~ m#y#i;
}

# Open the files in vi
my $files;
map { chomp $_; $files .= "$_ "; } @files;
exec("vi $files");

=head1 vic

Open files based on git commits/status

=head2 USAGE

=over

=item vic [d | SHA]

=back

By default, vic opens the files that were committed in the current branch's HEAD.

If it is passed a specific SHA, it opens instead the files committed to that SHA.

If it is passed 'd', it opens the files in your current checkout that you have already edited,
i.e. the files that make your checkout dirty


No feedback yet

 

[Links][icon] My links

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


August 2014
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

blogsoft