Gitlab offers a convenient feature called snippet
to store code fractions, but users only have a web interface to manage them. This little tool utilizes gitlab API to simplify that process. With it, you can do the following things with one click:
- check the snippets you have
- upload new snippets (and get a link to share with others)
- download any accessible snippets to a specified location
This tool is written in python2, but you can easily make it compatible with python3.
-
Fill your git lab domain in
snippet.py
(BASE_URL
). For UM's gitlab, for instance, it's https://gitlab.eecs.umich.edu -
Generate your Access Token at {your gitlab domain}/profile/personal_access_tokens, and fill it in
snippet.py
(TOKEN
) -
Snippet visibility level is pre-set as
private
. You could change it tointernal
orpublic
insnippet.py
(VISIBLE_LEVEL
). Notice: You have to change the visibility level for others to view or download your snippet, but pay attention to privacy issues!
- upload
python snippet.py push a.cpp
# http://gitlab.eecs.umich.edu/snippets/2233
- check
python snippet.py status
# http://gitlab.eecs.umich.edu/snippets/2233 a.cpp
You will only see your snippets here
- download
python snippet.py pull 2233 # download a.cpp to this dir
python snippet.py pull 2233 -f ~ # download a.cpp to ~
python snippet.py pull 2233 -f ~/b.cpp # download a.cpp to ~ and rename as b.cpp
You can download a snippet as long as you know the id and authorized to see it
- remove
python snippet.py rm 2233
- update
python snippet.py update 2233 a.cpp
- Check existing snippets and get their ids
python snippet.py status
- Upload a new snippet
python snippet.py push [local_file_path] -t [title] -m [description]
Title and description are optional. If not specified, title is the same as the file name. local_file_path
could be relative or absolute.
- Download an existing snippet
python snippet.py pull [snippet_id|snippet_url] -f [destination]
Destination is optional. It could be a directory or a file name (if you want a different name for the downloaded file). If not specified, a new file will be created in the current working directory.
- Modify an existing snippet
python snippet.py update [snippet_id|snippet_url] [local_file_path] -t [title] -m [description]
Title and description are optional. local_file_path
could be relative or absolute.
- Delete an existing snippet
python snippet.py rm [snippet_id|snippet_url]