Skip to content

Commit

Permalink
Merge pull request #159 from SeongjunJo/enhance_git_clone
Browse files Browse the repository at this point in the history
Add functions to clone private repository and ssh url
  • Loading branch information
soimkim authored May 8, 2024
2 parents eca2f93 + a4075a0 commit 441b03d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ If you give a link, the source is downloaded to the target directory through git

#### How it works
1. Try git clone.
1-1. If the link is ssh-url, convert to https-url.
2. If git clone fails, download it with wget and extract the compressed file.
3. After extracting the compressed file, delete the compressed file.

Expand All @@ -127,7 +128,11 @@ If you give a link, the source is downloaded to the target directory through git

#### How to run
```
$ fosslight_download -s "https://github.com/LGE-OSS/example" -t target_dir/
$ fosslight_download -s "https://github.com/LGE-OSS/example" -t target_dir/
```
If you want to try with private repository, set your github token like below.
```
$ fosslight_download -s "https://[email protected]/Foo/private_repo -t target_dir/"
```

## 👏 How to report issue
Expand Down
25 changes: 23 additions & 2 deletions src/fosslight_util/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ def change_src_link_to_https(src_link):
return src_link


def change_ssh_link_to_https(src_link):
src_link = src_link.replace("[email protected]:", "https://github.com/")
return src_link


def parse_src_link(src_link):
src_info = {"url": src_link}
src_link_changed = ""
if src_link.startswith("git://") or src_link.startswith("https://") or src_link.startswith("http://"):
if src_link.startswith("git://") or src_link.startswith("git@") \
or src_link.startswith("https://") or src_link.startswith("http://"):
src_link_split = src_link.split(';')
if src_link.startswith("git://github.com/"):
src_link_changed = change_src_link_to_https(src_link_split[0])
elif src_link.startswith("[email protected]:"):
src_link_changed = change_ssh_link_to_https(src_link_split[0])
else:
if "rubygems.org" in src_link:
src_info["rubygems"] = True
Expand Down Expand Up @@ -205,11 +213,24 @@ def get_github_ossname(link):
return oss_name


def get_github_token(git_url):
github_token = ""
pattern = r'https://(.*?)@'
search = re.search(pattern, git_url)
if search:
github_token = search.group(1)
return github_token


def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
ref_to_checkout = decide_checkout(checkout_to, tag, branch)
msg = ""
oss_name = get_github_ossname(git_url)
oss_version = ""
github_token = get_github_token(git_url)
callbacks = None
if github_token != "":
callbacks = git.RemoteCallbacks(credentials=git.UserPass("foo", github_token)) # username is not used, so set to dummy

if platform.system() != "Windows":
signal.signal(signal.SIGALRM, alarm_handler)
Expand All @@ -221,7 +242,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
Path(target_dir).mkdir(parents=True, exist_ok=True)
repo = git.clone_repository(git_url, target_dir,
bare=False, repository=None,
remote=None, callbacks=None)
remote=None, callbacks=callbacks)
if platform.system() != "Windows":
signal.alarm(0)
else:
Expand Down

0 comments on commit 441b03d

Please sign in to comment.