2929from sys import exit
3030from docopt import docopt
3131
32- __version__ = '1.1 .0'
32+ __version__ = '1.2 .0'
3333GH_API_BASE_URL = 'https://api.github.com'
3434GH_REPO_CONTENTS_ENDPOINT = GH_API_BASE_URL + '/repos/{}/{}/contents'
3535BASE_NORMALIZE_REGEX = re .compile (r'.*github\.com\/' )
@@ -61,7 +61,7 @@ def clone_file(download_url, file_path):
6161 try :
6262 r .raise_for_status ()
6363 except Exception as e :
64- exit_with_m ('Failed to clone ' + download_url , e )
64+ exit_with_m ('Failed to clone ' + download_url )
6565
6666 with open (file_path , 'wb' ) as fd :
6767 for chunk in r .iter_content (chunk_size = 128 ):
@@ -72,14 +72,14 @@ def clone(base_url, rel_url=None, path=None, ref=None):
7272 """
7373 Recursively clones the path
7474 """
75- req_url = os . path . join ( base_url , rel_url ) if rel_url else base_url
75+ req_url = base_url + '/' + rel_url if rel_url else base_url
7676
7777 # Get path metadata
7878 r = req .get (req_url ) if not ref else req .get (req_url , params = {'ref' : ref })
7979 try :
8080 r .raise_for_status ()
8181 except Exception as e :
82- exit_with_m ('Failed to fetch metadata for ' + path , e )
82+ exit_with_m ('Failed to fetch metadata for ' + path )
8383 repo_data = r .json ()
8484
8585 # Recursively clone content
@@ -101,9 +101,9 @@ def clone(base_url, rel_url=None, path=None, ref=None):
101101def resolve_path (path , dir ):
102102 index = path .find (dir )
103103 if index is - 1 :
104- return os .path .join (dir , path )
104+ return os .path .abspath ( os . path . join (dir , path ) )
105105 else :
106- return path [index :]
106+ return os . path . abspath ( path [index :])
107107
108108
109109###
@@ -121,21 +121,28 @@ def main():
121121 if token :
122122 req .headers .update ({'Authorization' : 'token ' + token [0 ]})
123123 # Normalize & parse input
124- normal_gh_url = re .sub (BASE_NORMALIZE_REGEX , '' ,
125- gh_url ).replace ('/tree' , '' )
126- gh_args = normal_gh_url .split ('/' )
124+ normal_gh_url = re .sub (BASE_NORMALIZE_REGEX , '' , gh_url )
125+ gh_args = normal_gh_url .replace ('/tree' , '' ).split ('/' )
126+
127+ if len (gh_args ) < 2 or normal_gh_url == gh_url :
128+ exit_with_m ('Invalid GitHub URI' )
129+
127130 user , repo = gh_args [:2 ]
131+ ref = None
132+ rel_url = None
128133
129- if len (gh_args ) > 2 :
130- # Clone subdirectory
134+ if len (gh_args ) >= 2 :
135+ # Clone entire repo
136+ path = repo
137+
138+ if len (gh_args ) >= 3 :
139+ # Clone entire repo at the branch
131140 ref = gh_args [2 ]
141+
142+ if len (gh_args ) >= 4 :
143+ # Clone subdirectory
132144 rel_url = os .path .join (* gh_args [3 :])
133145 path = gh_args [- 1 ]
134- else :
135- # Clone entire repo
136- ref = None
137- rel_url = None
138- path = repo
139146
140147 api_req_url = GH_REPO_CONTENTS_ENDPOINT .format (user , repo )
141148
0 commit comments