File Coverage

blib/lib/App/Git/Workflow/Command/Take.pm
Criterion Covered Total %
statement 61 64 95.3
branch 7 12 58.3
condition 1 3 33.3
subroutine 11 11 100.0
pod 2 2 100.0
total 82 92 89.1


line stmt bran cond sub pod time code
1             package App::Git::Workflow::Command::Take;
2              
3             # Created on: 2015-06-05 11:38:28
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   338115 use strict;
  2         8  
  2         58  
10 2     2   10 use warnings;
  2         5  
  2         45  
11 2     2   1089 use Pod::Usage ();
  2         75551  
  2         66  
12 2     2   1364 use Data::Dumper qw/Dumper/;
  2         12361  
  2         155  
13 2     2   1108 use English qw/ -no_match_vars /;
  2         7079  
  2         14  
14 2     2   1705 use App::Git::Workflow;
  2         101669  
  2         107  
15 2     2   982 use App::Git::Workflow::Command qw/get_options/;
  2         25016  
  2         121  
16 2     2   960 use Path::Tiny;
  2         11366  
  2         99  
17 2     2   1085 use File::Copy qw/copy/;
  2         4658  
  2         1291  
18              
19             our $VERSION = 0.7;
20             our $workflow = App::Git::Workflow->new;
21             our ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
22             our %option;
23              
24             sub run {
25 2     2 1 18658 my ($self) = @_;
26              
27 2         26 get_options(
28             \%option,
29             'quiet|q',
30             'ours|mine',
31             'theirs',
32             'add|a',
33             );
34              
35             my @conflicts = (
36 2         14 map { /^ \s+ both \s modified: \s+(.*)$/xms; $1 }
  2         26  
37 2         1433 grep { /^ \s+ both \s modified: \s+/xms }
  22         199  
38             $workflow->git->status()
39             );
40              
41 2 100       16 if (!@ARGV) {
42 1         9 @ARGV = ('./');
43             }
44              
45             CONFLICT:
46 2         10 for my $conflict (@conflicts) {
47              
48             PATH:
49 2         6 for my $path (@ARGV) {
50 2         12 $path =~ s{^[.]/}{};
51 2 50       34 next PATH unless $conflict =~ /^\Q$path\E/;
52              
53 2         20 resolve($conflict);
54              
55 2 50       13 if ( $option{add} ) {
56 0         0 $workflow->git->add($path);
57             }
58              
59 2         8 next CONFLICT;
60             }
61             }
62              
63 2         16 return;
64             }
65              
66             sub resolve {
67 2     2 1 10 my ($file) = @_;
68              
69 2         98 print "Resolving $file\n";
70              
71 2         34 my %states = (
72             ours => qr/^<<<<<<</,
73             theirs => qr/^=======/,
74             keep => qr/^>>>>>>>/,
75             );
76 2         6 my $state = 'keep';
77 2 50       8 my $side = $option{theirs} ? 'theirs' : 'ours';
78 2         22 my $read = path($file)->openr;
79 2         544 my $tmp = path($file . '.tmp');
80 2         81 my $write = $tmp->openw;
81              
82             LINE:
83 2         344 while (my $line = <$read>) {
84 110         240 for my $check (keys %states) {
85 330 50       927 if ( $line =~ /$states{$check}/ ) {
86 0         0 $state = $check;
87 0         0 next LINE;
88             }
89             }
90              
91 110 50 33     239 print {$write} $line if $state eq 'keep' || $state eq $side;
  110         342  
92             }
93              
94 2         95 close $write;
95 2         73 unlink $file;
96 2         21 copy $tmp, $file;
97 2         902 unlink $tmp;
98              
99 2         180 return;
100             }
101              
102             1;
103              
104             __DATA__
105              
106             =head1 NAME
107              
108             App::Git::Workflow::Command::Take - Resolve merge conflicts by only taking one side of each conflicted section
109              
110             =head1 VERSION
111              
112             This documentation refers to git-take-mine version 0.7
113              
114             =head1 SYNOPSIS
115              
116             git-take-mine [option] [path_or_file]
117              
118             OPTIONS:
119             -q --quiet Suppress notifying of files changed
120             --ours Take choanges from current branch throwing away other branches changes
121             --theirs Take changes from merging branch throwing away current branches changes
122              
123             -v --verbose Show more detailed option
124             --VERSION Prints the version information
125             --help Prints this help information
126             --man Prints the full documentation for git-take-mine
127              
128             =head1 DESCRIPTION
129              
130             C<git take> provides a way of quickly resolving conflicts by taking only one
131             side of the conflict. It does this differently to C<git checkout --ours> /
132             C<git checkout --theirs> as it only takes the conflicted part not the whole
133             of one side of the merge. Where this can come in handy is for merging things
134             with version number (eg pom.xml) where only the version number conflicts and
135             there may be other changes in the file that should be taken.
136              
137             =head1 SUBROUTINES/METHODS
138              
139             =head2 C<run ()>
140              
141             Finds the conflicted files to resolve
142              
143             =head2 C<resolve ($file)>
144              
145             Resolves conflicts in C<$file> in favor of C<--ours> or C<--theirs>.
146              
147             =head1 DIAGNOSTICS
148              
149             =head1 CONFIGURATION AND ENVIRONMENT
150              
151             =head1 DEPENDENCIES
152              
153             =head1 INCOMPATIBILITIES
154              
155             =head1 BUGS AND LIMITATIONS
156              
157             There are no known bugs in this module.
158              
159             Please report problems to Ivan Wills (ivan.wills@gmail.com).
160              
161             Patches are welcome.
162              
163             =head1 AUTHOR
164              
165             Ivan Wills - (ivan.wills@gmail.com)
166              
167             =head1 LICENSE AND COPYRIGHT
168              
169             Copyright (c) 2015 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
170             All rights reserved.
171              
172             This module is free software; you can redistribute it and/or modify it under
173             the same terms as Perl itself. See L<perlartistic>. This program is
174             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
175             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
176             PARTICULAR PURPOSE.
177              
178             =cut