Skip to content

Allow POD with Perl shebangs - #3735

Merged
kivikakk merged 5 commits into
masterfrom
kivikakk/pod-with-perl-shebang
Jul 31, 2017
Merged

kivikakk merged 5 commits into
masterfrom
kivikakk/pod-with-perl-shebang

Conversation

@kivikakk

Copy link
Copy Markdown
Contributor

Right now a POD-extension file with a Perl shebang will only get recognised as Perl. This lets the Shebang classifier not narrow too much.

@kivikakk
kivikakk force-pushed the kivikakk/pod-with-perl-shebang branch from 07760c1 to 779250d Compare July 25, 2017 05:25
@Alhadis

Alhadis commented Jul 25, 2017 •

Copy link
Copy Markdown
Collaborator

Well, I'll be. Linguist allows more than one language to use the same interpreter.

Guess my last comment regarding TypeScript was kinda nonsensical...

@pchaigno

pchaigno commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

We currently don't have any sample Pod file with a shebang.
Maybe we should add one so that this gets tested?

@pchaigno

Copy link
Copy Markdown
Contributor

On second through, looking at the code, I don't think we support having several languages with the same interpreter. Interpreters are indexed in an associative array, and the next strategy, Linguist::Strategy::Extension, doesn't take candidate languages from the previous strategy as input anyway.

@Alhadis I guess you were right 😜

@Alhadis

Alhadis commented Jul 25, 2017

Copy link
Copy Markdown
Collaborator

We currently don't have any sample Pod file with a shebang. Maybe we should add one so that this gets tested?

Uhm... yeah, that's the other thing. About that...

daphuq

@pchaigno

Copy link
Copy Markdown
Contributor

@Alhadis I'm not sure I understand... There was initially a sample in this pull request?

@Alhadis

Alhadis commented Jul 25, 2017

Copy link
Copy Markdown
Collaborator

If it was deleted, it would've appeared in the changes... :\

@kivikakk This looks to be a bug with GitHub's diff-displaying.

@kivikakk

Copy link
Copy Markdown
Contributor Author

You might've missed this:

screen shot 2017-07-25 at 5 31 52 pm

I removed it as it wasn't actually appropriate, as it's a test for the classifier (only), not the whole detection process.

the next strategy, Linguist::Strategy::Extension, doesn't take candidate languages from the previous strategy as input anyway.

When a strategy doesn't return any results, results from the previous strategy are retained. This fix is for cases where a script has a #!/usr/bin/perl shebang but contains POD documentation. The original filename often will have no extension when a shebang is used, in which case Linguist::Strategy::Extension returns nothing, and the two results from Shebang are passed onto Heuristics (no result for no extension) and Classifier.

(If there is an extension, then .pl or .pod will be detected and selected as appropriate.)

iow, I think this does what we want, but I'll add some test cases to demonstrate.

@Alhadis

Alhadis commented Jul 25, 2017

Copy link
Copy Markdown
Collaborator

You might've missed this:

Nope, it missed me. I can't see that listed anywhere, and trying to open 07760c1 gives me this:

NIGGA, THEY'RE NOT GONNA BE IN OUTER SPACE

Anyway, doesn't matter. =)

@kivikakk

Copy link
Copy Markdown
Contributor Author

(This in fact won't do what I want it to, after reviewing what I wrote above, because the heuristic for .pod won't fire without a filename. Grrr.)

@kivikakk

Copy link
Copy Markdown
Contributor Author

Okay — new sample added, and some changes to heuristics so we can accept a list of candidates as input. In other words, if filename extension disambiguation fails, we can optionally disambiguate the list of candidates previous strategies have given us. (So far this just adds the one case for disambiguating Perl/Pod.)

Anyone got 👀 for this?

@Alhadis

Alhadis commented Jul 29, 2017

Copy link
Copy Markdown
Collaborator

LGTM. 👍

Also, please tell me Kommunikatsioonimoodul isn't a real Estonian word. 😨 Holy hell.

@pchaigno pchaigno left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The heuristic strategy modification feels a bit hackish to me, but I can't find a better way (unless we rewrite the whole ordering of strategies...) :-/

We can always change it latter if we find another approach. LGTM 👍

@heuristics.each do |heuristic|
if heuristic.matches?(blob.name)
languages = Array(heuristic.call(data))
return languages if languages.any? || languages.all? { |l| candidates.include?(l) }

@pchaigno pchaigno Jul 29, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you removed the second check (languages.all? { |l| candidates.include?(l) })? This is in place to make sure heuristic rule don't return new languages we've already crossed off the list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

languages.any? || languages.all? { … } is always true; if languages.any? is false, then languages.all? { … } will always return true (because .any? on an empty list is always true). So the if was always happening.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oo 😱 You're right, that should've been an AND. And I'm the one who switched it to an OR 😓

This was for cases where we'd use the same set of heuristic rules for two extensions with different candidates. For example if we use heuristic rules on C, C++, and Objective-C for both .h (the three languages are candidates) and .inc (only C and C++ are candidates). In that case, the extra check would ensure Objective-C is never returned for the .inc extension.

We currently don't have any such case, so we're probably fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, I see now. Feel free to reintroduce it in a PR if you like!

Comment thread lib/linguist/heuristics.rb Outdated
@@ -50,14 +50,19 @@ def self.disambiguate(*extensions, &heuristic)

# Internal
def initialize(extensions, &heuristic)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably rename extensions to something more appropriate...?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

@kivikakk
kivikakk merged commit 7451424 into master Jul 31, 2017
@kivikakk
kivikakk deleted the kivikakk/pod-with-perl-shebang branch July 31, 2017 03:39
@lildude lildude mentioned this pull request Aug 9, 2017
@mattn

mattn commented Aug 29, 2017

Copy link
Copy Markdown
Contributor

I'm not sure it is caused by this change but perl script with out file extention seems to be recognized as pod. So no way to look the code.

https://github.com/kazuho/kaztools/blob/master/cronlog

@dgholz

dgholz commented Sep 8, 2017

Copy link
Copy Markdown

This change recognizes files with no extension and a perl shebang and POD contents as POD files, but I'd expect them to be shown as syntax-highlighted perl

e.g. https://github.com/sitaramc/gitolite/blob/5c2fe87019dadbecb1883bedd075cd07b07b2078/src/gitolite previously displayed as a highlighted perl script (good), but now displays as the embedded POD (requiring me to use the Raw view to be able to see the code).

Please fix it to correctly detect these kinds of files as perl (not POD) or revert it, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants