This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathgithub_opener.py
53 lines (40 loc) · 1.87 KB
/
github_opener.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import sys
import pyperclip
# Install pyperclip module. Linux OS : sudo apt install python3-pyperclip
# otherwise : pip3 install pyperclip
def github_opener (filename, line_no):
"""
github_opener( filename, line_no ) :
Generates a perma link to the code inside your Github repo which is remote for your current git project.
And copies it to clipboard.
Parameters:
github_opener ( filename, line_no ) : takes two arguments.
Filename in which the code is and line no which you want to highlight.
eg. github_opener("hello.py", 8)
Returns:
Perma link copied to clipboard
"""
stream = os.popen('git remote -v | grep "origin"')
cmd_output = stream.readline() #cmd_output => origin git@github:username/proj/name.git (fetch)
info = cmd_output.split(":")[1].split(".")[0] #info => username/proj_name
curr_dir = os.getcwd()
proj_name = info.split("/")[1] #proj_name
index = curr_dir.find(proj_name) #start index of proj name in current path
perma_link = "https://github.com/" + info + "/blob/master" + curr_dir[(index+len(proj_name)):] + "/" + filename + "/#L" + line_no
pyperclip.copy(perma_link)
print("Link copied!")
if __name__ == "__main__":
"""
It takes two command line arguments.
Name of the file (Not complete path with directory) in which the desired line of code is present and
the line number to high light.
Eg.
python github_opener.py mode.py 9
output:
Link copied!
(Link => https://github.com/username/proj_name/blob/master/directory/mode.py/#L9)
In terminal, navigate to the directory in which the file is present.
Run command : python github_opener.py filename.xyz 23
"""
github_opener(sys.argv[1], sys.argv[2])