Skip to content

Commit

Permalink
Keep comments close to the original line
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Jan 28, 2021
1 parent 6f1611d commit 59701f5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 28 deletions.
75 changes: 61 additions & 14 deletions prepare_spec
Expand Up @@ -122,7 +122,7 @@ our $base_package = "";

sub new {
my ($class, $name, $line) = @_;
my $self = {name => $name, lines => [], before_lines => 0, after_lines => 0};
my $self = {name => $name, lines => [], before_lines => 0, after_lines => 0, comment => undef};
push(@{$self->{lines}}, $line) if defined $line;

bless $self, $class;
Expand All @@ -138,12 +138,16 @@ our $base_package = "";
$self->{before_lines} = $nr;
}


sub set_after_lines {
my ($self, $nr) = @_;
$self->{after_lines} = $nr;
}

sub print_comment {
my $self = shift;
$self->{comment}->print() if $self->{comment};
}

sub print_before_lines {
my $self = shift;
for (my $i = 0; $i < $self->{before_lines}; $i++) { print "\n"; }
Expand All @@ -169,6 +173,15 @@ our $base_package = "";
}
}

sub set_comment {
my ($self, $comment) = @_;
die "No 2 comments" if $self->{comment};
$self->{comment} = $comment;
die "unmergable comment" if $self->{before_lines};
$self->{before_lines} = $comment->{before_lines};
$comment->{before_lines} = 0;
}

sub merge_with_empty_after {
my ($self, $section) = @_;
$self->{after_lines} += scalar(@{$section->{lines}});
Expand All @@ -185,12 +198,19 @@ our $base_package = "";
my $self = shift;
if ($debug) {
print "Section $self->{name} B:$self->{before_lines} A:$self->{after_lines}\n";
if ($self->{comment}) {
print("Has comment:\n");
for my $line (@{$self->{comment}->{lines}}) {
print " $line\n";
}
}
for my $line (@{$self->{lines}}) {
print " $line\n";
}
}
else {
$self->print_before_lines();
$self->print_comment();
for my $line (@{$self->{lines}}) {
print "$line\n";
}
Expand All @@ -214,6 +234,7 @@ our $base_package = "";
printf("TAG '%s' = '%s'\n", $self->{name}, $self->value());
}
else {
$self->print_comment();
printf("%-16s%s\n", $self->{name} . ":", $self->value());
}

Expand Down Expand Up @@ -253,6 +274,8 @@ our $base_package = "";
print "PREAMBLE B:$self->{before_lines} A:$self->{after_lines}\n";
return;
}
print "$self->{vim_mode}\n" if $self->{vim_mode};

my $thisyear = localtime->year() + 1900;
my @copyrights = @{$self->{copyright} || []};
unshift @copyrights, "# Copyright (c) $thisyear SUSE LLC";
Expand Down Expand Up @@ -293,6 +316,11 @@ EOF
push(@{$self->{copyright}}, $copyright);
}

sub set_vim_mode {
my ($self, $line) = @_;
$self->{vim_mode} = $line;
}

sub add_footer {
my ($self, $footer) = @_;
if (!defined $self->{footer}) {
Expand Down Expand Up @@ -380,6 +408,7 @@ EOF
$tag =~ s/patch/Patch/i;
$tag =~ s/source/Source/i;
$tag =~ s/req/Req/i;
$tag =~ s/pre/Pre/;
$tag =~ s/confl/Confl/i;
$tag =~ s/prov/Prov/i;
$tag =~ s/url/URL/i;
Expand Down Expand Up @@ -434,15 +463,15 @@ EOF
chomp @readspec;

my @global_tags_list = (
'Autoreq', 'Autoreqprov', 'BuildArch', 'BuildArchitectures',
'BuildRequires', 'Conflicts', 'DocDir', 'Enhances',
'Enhances', 'EssentialFor', 'ExcludeArch', 'ExclusiveArch',
'Freshens', 'Group', 'Name', 'NoPatch',
'NoSource', 'Obsoletes', 'Patch\d*', 'Prefix',
'PreReq', 'Provides', 'Recommends', 'Requires',
'Source\d*', 'Suggests', 'Summary', 'Supplements',
'URL', 'Version', 'License', 'Release',
'BuildConflicts'
'Autoreq', 'Autoreqprov', 'BuildArch', 'BuildArchitectures',
'BuildRequires', 'Conflicts', 'DocDir', 'Enhances',
'Enhances', 'EssentialFor', 'ExcludeArch', 'ExclusiveArch',
'Freshens', 'Group', 'Name', 'NoPatch',
'NoSource', 'Obsoletes', 'Patch\d*', 'Prefix',
'PreReq', 'Provides', 'Recommends', 'Requires',
'Source\d*', 'Suggests', 'Summary', 'Supplements',
'URL', 'Version', 'License', 'Release',
'BuildConflicts', 'BuildPreReq', 'RemovePathPostfixes'
);

my $global_tags_re = '^\s*(' . join("|", @global_tags_list) . ')\s*:';
Expand Down Expand Up @@ -684,8 +713,8 @@ EOF
$self->create_tag_section("BuildRoot", "%{_tmppath}/%{name}-%{version}-build");
next;
}
if (m/^(Requires\((post|pre)\))\s*:\s*(.*)/oi) {
$self->create_tag_section($1, $3);
if (m/^(Requires\([^:\s]*\))\s*:\s*(.*)/oi) {
$self->create_tag_section($1, $2);
next;
}

Expand Down Expand Up @@ -750,6 +779,23 @@ EOF

}

# make comments a subsection
sub merge_comments() {
my $self = shift;

my $lastsection;
for my $section (@{$self->{sections}}) {
if ($lastsection && $lastsection->name() eq 'comment') {
next if $lastsection->{after_lines} + $section->{before_lines};
$section->set_comment($lastsection);
$self->delete_section($lastsection);
return $self->merge_comments();

}
$lastsection = $section;
}
}

sub merge_empty_sections {
my $self = shift;

Expand Down Expand Up @@ -785,7 +831,7 @@ EOF
my $lastsection;
for my $i (0 .. scalar(@{$self->{sections}}) - 1) {
my $section = $self->{sections}->[$i];
if ($section->name() eq $tag) {
if ($section->name() eq $tag && !$section->{comment}) {
if ($lastsection) {
if (($section->value() cmp $lastsection->value()) < 0) {
$self->{sections}->[$i] = $lastsection;
Expand Down Expand Up @@ -883,6 +929,7 @@ if (!stat((glob("$specdir/$base_package*.spec"))[0] || "")) {
my $parser = Parser->new;
$parser->read_and_parse_old_spec($specfile, $base_package);
$parser->merge_empty_sections();
$parser->merge_comments();
$parser->reorder_tags();

my $mapper = SPDXMapper->new;
Expand Down
2 changes: 1 addition & 1 deletion testing/codeblock.spec.out
Expand Up @@ -22,8 +22,8 @@ BuildRequires: aaa
BuildRequires: bbb
# /MANUAL
# SECTION the testing dependencies
BuildRequires: python-mock
BuildRequires: python-nose
BuildRequires: python-mock
# /SECTION
### COMMON-PATCH-BEGIN ###
# implement "--record-rpm" option for distutils installations
Expand Down
1 change: 1 addition & 0 deletions testing/langpackage.spec.out
Expand Up @@ -33,5 +33,6 @@ Group: Whatever
Summary: Evolution Plugin for RSS Feeds Support
Group: Productivity/Networking/Email/Clients
Recommends: %{_name}-lang
Provides: %{name} = %{version}

%changelog
2 changes: 1 addition & 1 deletion testing/multilinecomment.spec.out
Expand Up @@ -21,8 +21,8 @@ BuildRequires: pkg5
# this is comment
# and another one
# to see that all apply to the value bellow
BuildRequires: aaaa
BuildRequires: pkg3
BuildRequires: aaaa
Requires: zypper

%changelog
4 changes: 2 additions & 2 deletions testing/prereq.spec.out
Expand Up @@ -18,11 +18,11 @@

Name: prereq
Requires(post): something
Requires(pre): test1
Requires(pre): test1
Requires(post): asomething bsomething
PreReq: deprecatedrequires
PreReq: uglyline1 uglyline2
PreReq: /bin/rm /bin/mkdir /usr/bin/chroot %fillup_prereq %insserv_prereq
Requires(postun): somethingcrazy
Requires(postun):somethingcrazy

%changelog
16 changes: 8 additions & 8 deletions testing/replace_pwdutils.spec.out
Expand Up @@ -30,16 +30,16 @@ BuildRequires: pam-devel
BuildRequires: pwdutils
Requires(post): pwdutils
Requires(post): update-alternatives
Requires(posttrans): pwdutils
Requires(posttrans): systemd
Requires(postun): pwdutils
Requires(postun): update-alternatives
Requires(posttrans):pwdutils
Requires(posttrans):systemd
Requires(postun):pwdutils
Requires(postun):update-alternatives
Requires(pre): pwdutils
Requires(pre): apache2
Requires(pretrans): pwdutils
Requires(pretrans): update-alternatives
Requires(preun): pwdutils
Requires(preun): chkconfig
Requires(pretrans):pwdutils
Requires(pretrans):update-alternatives
Requires(preun):pwdutils
Requires(preun):chkconfig
Requires: libmariadb
Requires: pwdutils
Requires: pwdutils-util
Expand Down
4 changes: 2 additions & 2 deletions testing/rpmpreamble.spec.out
Expand Up @@ -38,10 +38,10 @@ BuildRequires: libtool
BuildRequires: makeinfo
BuildRequires: openssl-devel
BuildRequires: pkg-config
BuildRequires: socat
BuildRequires: pkgconfig(gnutls)
BuildRequires: pkgconfig(libcurl)
BuildRequires: pkgconfig(libtasn1)
BuildRequires: socat
%if 0%{?suse_version} < 1230
BuildRoot: %{_tmppath}/%{name}-%{version}-build
%endif
Expand Down Expand Up @@ -85,7 +85,7 @@ Group: Development/Libraries/C and C++
Requires: %{libmicrohttpd} = %{version}
Requires: pkg-config
Requires: pkgconfig(gnutls)
Requires(post,preun): info
Requires(post,preun):info

%description devel
Headers, pkg-config files, so link and other development files for %{name}
Expand Down

0 comments on commit 59701f5

Please sign in to comment.