How can I use a link to source code in GitHub Issue? - github

I want to reference certain lines from a file in a GitHub repository in an issue. How can I do this without copying and pasting manually? Can I use a link to the source code?
To clarify, here is the file that I want to reference code from:
mysource.py
1: def func1():
2: for i in range(10);
3: print 'hello world'
4:
5: def func2():
6: for i in range(10);
7: print 'hello world'
I want to quote lines 5 to 7 of the file in my issue like this:
5: def func2():
6: for i in range(10);
7: print 'hello world'

You can now do this by selecting a range of lines and using the create issue menu option:
Example
...resulting in:
Steps
Go to the source file
Select the range of lines (e.g., click the first line of interest, then shift + click the last line)
From the ... menu on the left, select create an issue
An issue page will appear with a link to the lines, which renders as view of the source code.

You can add the following hash-fragment.
https://github.com/username/repo/mysource.py#L5-L7
Just tried it without even knowing it was possible.

The key is to get a permalink to the code, that will be automatically converted to the code snippet by GitHub Markdown (GFM). After completing the steps below, the whole thing should look like this after you create an issue or comment:
Live example
Follow these steps:
Go to the page with source code that needs to be referenced. For example: https://github.com/roots/trellis-cli/blob/master/cmd/venv_hook.go
Click on the line number next to the respective code. The selected line should now be colored with yellow, and the three dots button should appear next to the line number, like that:
Tip: if you want to reference multiple lines of code, click on the first line number, then hold Shift and press the second line number.
Click on the three dots button, select "Copy permalink":
Go to the issue where you want to reference the code. Paste the permalink there. The link should look like that:
https://github.com/roots/trellis-cli/blob/a389115eb35e3c322e084183dc41497ffd67e7e0/cmd/venv_hook.go#L25-L27
Notice that the link to the source code now has a commit SHA in the URL instead of the branch name master.

No, this is not possible. However, you can create line comments on the Files Changed tab of pull requests which can reference issues through the standard # referencing.

Related

Force GitHub pages to render numerical lists sequentially instead of restarting?

I noticed that when GitHub pages with mkdocs have text or images in between numbers it ignores the numbering in the source document and instead restarts numbering. See here for an example of this.
Is there a way to force mkdocs to render the numbers in the original document instead of inventing its own?
If you add them all as 1. space and text, then sub item as image with a tab, it should work. As an example:
1. Option 1
imgage or next text
1. Option 2
You can check this documentation built using mkdocs as an example.
As Zoran mentioned above that was part of it. Things I figured out:
Code must be double tabbed - you cannot use ```
Indented lists must also be double tabbed - I wasn't able to get a single tab to work
Images do not have to be tabbed - they can be in line with the above text
There should not be an empty line when an image follows a numbered list item BUT FOR CODE there must.
I still haven't completely figured out. mkdoc's behavior is not as predictable as regular markdown

How to select/comment on a range of lines in github pull request?

There is an option to comment on a range of lines in file on github, see How to refer to a specific line or range of lines in github?
But is there similar option to comment on range of lines inside pull request?
When I click on line, only single line is highlighted yellow and R### (e.g. R446) is appended to url, clicking another line with shift pressed doesn't do anything. I tried to change url to R446-450 but it didn't do anything. Changing it to #L450-458 also didn't do anything. Also even that single highlighted line doesn't seem to affect anything.
Clicking blue plus that appears on hover creates comment window, but it only commenting on a single line.
Commenting on single line results in this
Comment on pull-request page shows only 4 lines above selected/commented line, but I'm interested in showing 7 lines, not 4
Since Oct. 2019, Nat Friedman (CEO of GitHub) declares that feature available
(And, see below, since Feb. 2020, multi-lines comment reference is possible)
🔥
Multi-line comments are here!
Click and drag to comment on multiple lines in a pull request diff. ✨
These little quality-of-life improvements are at the heart of what we love doing at GitHub.
🥰
Thanks to:
John Caine
Mike Skalnik
Pat Nakajima
Mike Chlipala
Joel Califa
Matt Colyer
Melanie Gilman
Nick Craver immediately asks:
Follow-up question: are there plans to support suggestions?
It seems to apply to the last line at the moment:
That would be, according to Nat, "Coming early next year".
Update Feb. 2020: "A new interaction for multi-line pull request comments "
To leave a comment referencing multiple lines, you can either:
click on a line number in the diff view, hold Shift, click on a second line number and click the "+" button next to the second line number; or
click and hold to the right of a line number, drag and then release the mouse when you’ve reached the desired line.
This was announced by Nat Friedman
Shipping today on GitHub: multi-line suggestions!
With, again, special thanks to Melanie Gilman, Pat Nakajima, Mike Chlipala, Joel Califa, John Caine, Matt Colyer and
, and Kelly Arwine.
GitHub Changelog also references this.
Henry adds an observation
A smaller side effect, but I assume being able to share a multi-line diff in the PR is new too!
Example: babel/babel PR 10511 diff-L261-L263
But that was available since July 2019
It isn't possible to comment on multiple lines in a pull request review at GitHub. I hope they will create a new feature where this is possible.
But what you can do, there is a little (time consuming) workaround:
If you go in to the code, in the branch with the changes, you can select multiple lines and then copy a permalink for those lines. When you paste this link into your review comment, it will be shown as a code snippet.
For more, read this: https://help.github.com/articles/creating-a-permanent-link-to-a-code-snippet/
Update Github has released a new feature where this is possible. See VonC's answer :-)
To reference multiple lines click on a line number in the diff view, hold shift, and click on a second line number. The URL of the page will update and can be shared with fellow contributors.
To leave a comment referencing multiple lines click and hold to the right of a line number, drag, and then release the mouse when you’ve reached the desired line.
The multiline comment feature is now available on GitHub. Enjoy!
Multi-line comments have arrived! Git has introduced new feature today for multiline comment please have look
By click on + icon and then drag to multiple lines
https://twitter.com/github/status/1179101186437324801

Single line break in Github flavoured markdown without any extension

I am writing README.md for my project that I have created on GitHub. I am having issues with the line breaks. I have gone through the official documentation, but I am not sure how to make a single line break, as single Enter is not recognized as a line break and two Enters make it a new paragraph.
From documentation:
"My basic recommendation for learning how line breaks work is to experiment and discover -- hit once (i.e., insert one newline), then hit it twice (i.e., insert two new lines), see what happens. You'll soon learn to get what you want. "Markdown Toggle" is your friend."
It mentions about "Markdown Toggle", I googled it but it shows me "Markdown Here" (Markdown Toggle is renamed maybe). But I want to do it without any extension.
Is it possible?
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#lines
Ending a line with two or more spaces should work.
Use two or more spaces <-- 2 or more spaces
to create a line break.

Github - link to function in source

I know I can have anchors to a certain line, but if the source changes that line might become irrelevant. Example:
...source.php#L33 < line 33 may become line 40 later :(
Is there some way to tell GitHub to link to a certain function or property from the source, without specifying the line?
(The source is written in PHP code.)
AFAIK, no, but you can link to a certain revision of the file, and thus, you can know for sure that line will always point at the start of the function.
The URL looks like /{user}/{repo}/blob/{hash}/{file}#L{line}
To get it, click "Commits", select the last commit, click "Browse code", and find your file and line as usual.
documentation
Yes you can. Newer browsers support highlighting a portion of text and scrolling to it.
Example: common.c#:~:text=static%20ssize_t%20led_current_store
All you have to do is to add this to the end of your URL:
#:~:text=function_name
Some browsers do not support this, though: https://caniuse.com/url-scroll-to-text-fragment
Enjoy!

Copy code example from Zend Site

How would you copy the code from this example page
http://framework.zend.com/manual/en/learning.quickstart.create-layout.html
(Below the text "Now that we've initialized Zend_Layout and set the Doctype, let's create our site-wide layout:" )
A simple copy paste will also copy the line number or #. Any tricks ?
I hate these line numbers as well. The way I do it:
Copy the text to your favorite text editor (notepad won't do, Word for example would) and block-select using the ALT Key.
Let me give you an example with Notepad++. here I copied text from the said site with all line numbers included. Now I just hold the ALT button and select normally with my mouse (or using SHIFT+arrow keys). The result of me selecting only the code sans the line numbers you see in the following screenshot:
Now I could just copy this code to a new editor.