Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt before overwriting files, some error handling, and refactoring #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

alichtman
Copy link

Fix #33

/tmp via 🐍 v3.12.0 via πŸ’Ž v3.2.2 took 10ms
01:49:51 AM ➜ mkdir test_env

/tmp via 🐍 v3.12.0 via πŸ’Ž v3.2.2 took 2ms
01:49:56 AM ➜ cd test_env/

/tmp/test_env via πŸ’Ž v3.2.2 took 20ms
01:49:58 AM ➜ ls

/tmp/test_env via πŸ’Ž v3.2.2 took 4ms
01:49:58 AM ➜ gitdir https://github.com/sdushantha/gitdir/tree/master/gitdir
Downloaded: __init__.py to gitdir/__init__.py
Downloaded: __main__.py to gitdir/__main__.py
Downloaded: gitdir.py to gitdir/gitdir.py
βœ” Download complete

/tmp/test_env via πŸ’Ž v3.2.2 took 2s
01:50:40 AM ➜ gitdir https://github.com/sdushantha/gitdir/tree/master/gitdir
✘ File gitdir/__init__.py already exists. Overwrite? [y/N] y
Downloading (overwriting): __init__.py to /tmp/test_env/gitdir/__init__.py
✘ File gitdir/__main__.py already exists. Overwrite? [y/N] y
Downloading (overwriting): __main__.py to /tmp/test_env/gitdir/__main__.py
✘ File gitdir/gitdir.py already exists. Overwrite? [y/N] N
Skipped: gitdir.py
βœ” Download complete

/tmp/test_env via πŸ’Ž v3.2.2 took 6s
01:50:49 AM ➜ ls
ο„• gitdir

/tmp/test_env via πŸ’Ž v3.2.2 took 4ms
01:51:01 AM ➜ ll gitdir
.rw-rw-r-- alichtman alichtman  16 B  Sat Jan 13 2024 01:50:46 AM ξ˜† __init__.py
.rw-rw-r-- alichtman alichtman  33 B  Sat Jan 13 2024 01:50:47 AM ξ˜† __main__.py
.rw-rw-r-- alichtman alichtman 6.8 KB Sat Jan 13 2024 01:50:40 AM ξ˜† gitdir.py

Single file downloads still work:

/tmp/test_env via πŸ’Ž v3.2.2 took 4ms
01:51:16 AM ➜ gitdir https://github.com/sdushantha/gitdir/blob/master/setup.py
Single file download
Downloaded: setup.py to setup.py
βœ” Download complete

/tmp/test_env via 🐍 v3.12.0 via πŸ’Ž v3.2.2 took 657ms
01:53:41 AM ➜ head setup.py
import setuptools

with open('README.md', 'r') as fh:
    long_description = fh.read()

setuptools.setup(
    name='gitdir',
    version='1.2.7',
    author='Siddharth Dushantha',
    author_email='siddharth.dushantha@gmail.com',

Current behavior:

```
gitdir https://github.com/folke/dot/
Traceback (most recent call last):
  File "/home/alichtman/.local/bin/gitdir", line 8, in <module>
    sys.exit(main())
    β”‚        β”” <function main at 0x7eff15ecd260>
    β”” <module 'sys' (built-in)>
  File "/home/alichtman/.local/lib/python3.12/site-packages/gitdir/gitdir.py", line 172, in main
    total_files = download(url, flatten, args.output_dir)
                  β”‚        β”‚    β”‚        β”” Namespace(urls=['https://github.com/folke/dot/'], output_dir='./', flatten=False)
                  β”‚        β”‚    β”” False
                  β”‚        β”” 'https://github.com/folke/dot/'
                  β”” <function download at 0x7eff15eccfe0>
  File "/home/alichtman/.local/lib/python3.12/site-packages/gitdir/gitdir.py", line 60, in download
    api_url, download_dirs = create_url(repo_url)
                             β”‚          β”” 'https://github.com/folke/dot/'
                             β”” <function create_url at 0x7eff15eccf40>
  File "/home/alichtman/.local/lib/python3.12/site-packages/gitdir/gitdir.py", line 49, in create_url
    download_dirs = url[branch.end():]
                    β”‚   β”” None
                    β”” 'https://github.com/folke/dot/'
AttributeError: 'NoneType' object has no attribute 'end'
```

New behavior:

```
python3 gitdir.py https://github.com/folke/dot/
✘ Could not find branch name in the given url
```
And a bunch of refactoring
try:
opener = urllib.request.build_opener()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These only need to be set once, globally.

@alichtman
Copy link
Author

I left the commits separated for easier reviewing, but I recommend squashing this on merge.

"/contents/" + download_dirs + "?ref=" + branch.group(2))
if branch is None:
print_text(
"✘ Could not find branch name in the given url", "red", in_place=True
Copy link
Author

@alichtman alichtman Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimally, I think this would try common branch names like main and master, but that's out of scope for this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gitdir overwrites files without confirmation
1 participant