-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_file_permalink.py
42 lines (35 loc) · 1.6 KB
/
generate_file_permalink.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
import os
import sys
from pygit2 import Repository
from utils import get_git_https_url
def get_command_result(command_string, cwd):
import subprocess
process = subprocess.Popen(command_string, cwd=cwd, shell=True, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret = process.wait()
return "".join((process.stdout if ret == 0 else process.stderr).readlines()).strip()
def get_project_path(filepath):
import os
dir_path = os.path.dirname(filepath)
if get_command_result("git rev-parse --is-inside-work-tree", dir_path) == "true":
return get_command_result("git rev-parse --show-toplevel", dir_path)
else:
return filepath
def generate_file_permalink(file, start_line, end_line):
repo = Repository(file)
remote_default = next(repo.config.get_multivar("remote.pushdefault"), "origin")
origin_url = get_git_https_url(repo.remotes[remote_default].url)
head_tree = str(repo.head.target)
repo_root = get_project_path(file)
repo_info_list = file.split(repo_root)
permalink_file = ""
if len(repo_info_list) == 1:
permalink_file = os.path.basename(repo_info_list[0])
elif len(repo_info_list) == 2:
permalink_file = repo_info_list[1].lstrip("/")
if end_line == "":
return "{}/blob/{}/{}#L{}".format(origin_url, head_tree, permalink_file, start_line)
else:
return "{}/blob/{}/{}#L{}-#L{}".format(origin_url, head_tree, permalink_file, start_line, end_line)
if __name__ == "__main__":
print(generate_file_permalink(*sys.argv[1:]), end='', flush=True)