debian-mirror-gitlab/doc/user/snippets.md

231 lines
9.2 KiB
Markdown
Raw Normal View History

2020-10-24 23:57:45 +05:30
---
stage: Create
group: Editor
2021-02-22 17:27:13 +05:30
info: "To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments"
2020-10-24 23:57:45 +05:30
type: reference
---
2017-08-17 22:00:37 +05:30
# Snippets
2018-11-08 19:23:39 +05:30
With GitLab Snippets you can store and share bits of code and text with other users.
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
![GitLab Snippet](img/gitlab_snippet_v13_0.png)
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Snippets can be maintained using [snippets API](../api/snippets.md).
There are two types of snippets:
- Personal snippets.
- Project snippets.
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
## Personal snippets
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
Personal snippets are not related to any project and can be created completely
independently. There are 3 visibility levels that can be set, public, internal
and private. See [Public access](../public_access/public_access.md) for more information.
2017-08-17 22:00:37 +05:30
## Project snippets
2018-11-08 19:23:39 +05:30
Project snippets are always related to a specific project.
2019-07-07 11:18:12 +05:30
See [Project features](project/index.md#project-features) for more information.
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
## Create a snippet
To create a personal snippet, click the plus icon (**{plus-square-o}**)
on the top navigation and select **New snippet** from the dropdown menu:
![New personal snippet from non-project pages](img/new_personal_snippet_v12_10.png)
If you're on a project's page but you want to create a new personal snippet,
click the plus icon (**{plus-square-o}**) and select **New snippet** from the
lower part of the dropdown (**GitLab** on GitLab.com; **Your Instance** on
self-managed instances):
![New personal snippet from project pages](img/new_personal_snippet_from_project_v12_10.png)
To create a project snippet, navigate to your project's page and click the
plus icon (**{plus-square-o}**), then select **New snippet** from the upper
part of the dropdown (**This project**).
![New personal snippet from project pages](img/new_project_snippet_from_project_v12_10.png)
From there, add the **Title**, **Description**, and a **File** name with the
appropriate extension (for example, `example.rb`, `index.html`).
2021-02-22 17:27:13 +05:30
WARNING:
2021-03-08 18:12:59 +05:30
Make sure to add the filename to get code highlighting and to avoid this
2020-04-22 19:07:51 +05:30
[copy-pasting bug](https://gitlab.com/gitlab-org/gitlab/-/issues/22870).
2020-05-24 23:13:21 +05:30
## Versioned Snippets
> [Introduced](https://gitlab.com/groups/gitlab-org/-/epics/239) in GitLab 13.0.
Starting in 13.0, snippets (both personal and project snippets)
have version control enabled by default.
This means that all snippets get their own underlying repository initialized with
a `master` branch at the moment the snippet is created. Whenever a change to the snippet is saved, a
2021-03-11 19:13:27 +05:30
new commit to the `master` branch is recorded. Commit messages are automatically
generated. The snippet's repository has only one branch (`master`) by default, deleting
2020-05-24 23:13:21 +05:30
it or creating other branches is not supported.
2021-03-08 18:12:59 +05:30
Existing snippets are automatically migrated in 13.0. Their current
content is saved as the initial commit to the snippets' repository.
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
### Filenames
2020-05-24 23:13:21 +05:30
Snippets support syntax highlighting based on the filename and
2021-03-11 19:13:27 +05:30
extension provided for them. While you can submit a snippet
2020-05-24 23:13:21 +05:30
without specifying a filename and extension, it needs a valid name so the
content can be created as a file in the snippet's repository.
2021-03-11 19:13:27 +05:30
If you don't attribute a filename and extension to a snippet,
GitLab adds a filename in the format `snippetfile<x>.txt`
2020-05-24 23:13:21 +05:30
where `<x>` represents a number added to the file, starting with 1. This
2021-03-11 19:13:27 +05:30
number increments when more snippets without an attributed
2020-05-24 23:13:21 +05:30
filename are added.
When upgrading from an earlier version of GitLab to 13.0, existing snippets
2021-03-08 18:12:59 +05:30
without a supported filename are renamed to a compatible format. For
example, if the snippet's filename is `http://a-weird-filename.me` it is
changed to `http-a-weird-filename-me` to be included in the snippet's
repository. As snippets are stored by ID, changing their filenames breaks
2020-05-24 23:13:21 +05:30
direct or embedded links to the snippet.
2021-01-03 14:25:43 +05:30
### Multiple files by Snippet
> [Introduced](https://gitlab.com/groups/gitlab-org/-/epics/2829) in GitLab 13.5.
2021-03-11 19:13:27 +05:30
GitLab Snippets support multiple files in one single snippet. This is helpful
2021-01-03 14:25:43 +05:30
when your code snippet is composed of multiple parts or when they relate
to a certain context. For example:
- A snippet that includes a script and its output.
- A snippet that includes HTML, CSS, and JS code.
- A snippet with a `docker-compose.yml` file and its associated `.env` file.
2021-03-11 19:13:27 +05:30
- A `gulpfile.js` file coupled with a `package.json` file, which together can be
used to bootstrap a project and manage its dependencies.
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
Snippets support between 1 and 10 files. They can be managed via Git (because they're [versioned](#versioned-snippets)
by a Git repository), through the [Snippets API](../api/snippets.md), or in the GitLab UI.
2021-01-03 14:25:43 +05:30
![Multi-file Snippet](img/gitlab_snippet_v13_5.png)
To add a new file to your snippet through the GitLab UI:
1. Go to your snippet in the GitLab UI.
1. Click **Edit** in the top right.
1. Select **Add another file**.
1. Add your content to the file in the form fields provided.
1. Click **Save changes**.
To delete a file from your snippet through the GitLab UI:
1. Go to your snippet in the GitLab UI.
1. Click **Edit** in the top right.
2021-03-08 18:12:59 +05:30
1. Select **Delete file** alongside the filename of each file
2021-01-03 14:25:43 +05:30
you wish to delete.
1. Click **Save changes**.
2020-05-24 23:13:21 +05:30
### Cloning snippets
Snippets can be cloned as a regular Git repository using SSH or HTTPS. Click the **Clone**
button above the snippet content to copy the URL of your choice.
![Clone Snippet](img/snippet_clone_button_v13_0.png)
This allows you to have a local copy of the snippet's repository and make
changes as needed. You can commit those changes and push them to the remote
2021-03-11 19:13:27 +05:30
`master` branch.
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
### Reduce snippets repository size
2021-03-08 18:12:59 +05:30
Because versioned Snippets are considered as part of the [namespace storage size](../user/admin_area/settings/account_and_limit_settings.md),
2020-07-28 23:09:34 +05:30
it's recommended to keep snippets' repositories as compact as possible.
For more information about tools to compact repositories,
see the documentation on [reducing repository size](../user/project/repository/reducing_the_repo_size_using_git.md).
2020-05-24 23:13:21 +05:30
### Limitations
- Binary files are not supported.
2021-03-11 19:13:27 +05:30
- Creating or deleting branches is not supported. Only a default `master` branch is used.
2020-05-24 23:13:21 +05:30
- Git tags are not supported in snippet repositories.
2021-01-03 14:25:43 +05:30
- Snippets' repositories are limited to 10 files. Attempting to push more
2021-03-11 19:13:27 +05:30
than 10 files results in an error.
- Revisions are not visible to the user on the GitLab UI, but this feature is planned
in future iterations. See the [revisions tab issue](https://gitlab.com/gitlab-org/gitlab/-/issues/39271)
for updates.
2020-05-24 23:13:21 +05:30
- The [maximum size for a snippet](../administration/snippets/index.md#snippets-content-size-limit)
2021-03-11 19:13:27 +05:30
is 50 MB, by default.
2021-01-03 14:25:43 +05:30
- Git LFS is not supported.
2020-05-24 23:13:21 +05:30
2018-11-08 19:23:39 +05:30
## Discover snippets
There are two main ways of how you can discover snippets in GitLab.
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
For exploring all snippets that are visible to you, you can go to the Snippets
dashboard of your GitLab instance via the top navigation. For GitLab.com you can
2021-01-03 14:25:43 +05:30
navigate to an [overview]((https://gitlab.com/dashboard/snippets)) that shows snippets
you created and allows you to explore all snippets.
2018-11-08 19:23:39 +05:30
2021-03-11 19:13:27 +05:30
To discover snippets that belong to a specific project, navigate
2018-11-08 19:23:39 +05:30
to the Snippets page via the left side navigation on the project page.
2021-03-11 19:13:27 +05:30
Project snippets are enabled and available by default. You can
disable them by navigating to your project's **Settings**, expanding
2019-12-04 20:38:33 +05:30
**Visibility, project features, permissions** and scrolling down to
**Snippets**. From there, you can toggle to disable them or select a
different visibility level from the dropdown menu.
2018-11-08 19:23:39 +05:30
## Snippet comments
2020-06-23 00:09:42 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/12910) in GitLab 9.2.
2018-11-08 19:23:39 +05:30
With GitLab Snippets you engage in a conversation about that piece of code,
2021-03-11 19:13:27 +05:30
encouraging user collaboration.
2017-08-17 22:00:37 +05:30
## Downloading snippets
You can download the raw content of a snippet.
2021-03-08 18:12:59 +05:30
By default snippets are downloaded with Linux-style line endings (`LF`). If
2018-11-08 19:23:39 +05:30
you want to preserve the original line endings you need to add a parameter `line_ending=raw`
2021-03-08 18:12:59 +05:30
(For example: `https://gitlab.com/snippets/SNIPPET_ID/raw?line_ending=raw`). In case a
2018-11-08 19:23:39 +05:30
snippet was created using the GitLab web interface the original line ending is Windows-like (`CRLF`).
2018-10-15 14:42:47 +05:30
2018-11-08 19:23:39 +05:30
## Embedded snippets
2018-10-15 14:42:47 +05:30
> Introduced in GitLab 10.8.
2020-06-23 00:09:42 +05:30
Public snippets can not only be shared, but also embedded on any website. With
this, you can reuse a GitLab snippet in multiple places and any change to the source
2018-10-15 14:42:47 +05:30
is automatically reflected in the embedded snippet.
To embed a snippet, first make sure that:
- The project is public (if it's a project snippet)
- The snippet is public
- In **Project > Settings > Permissions**, the snippets permissions are
set to **Everyone with access**
2021-03-11 19:13:27 +05:30
After the above conditions are met, the **Embed** section appears in your
snippet. Click the **Copy** button to copy a one-line
script that you can add to any website or blog post. For example:
2018-10-15 14:42:47 +05:30
```html
<script src="https://gitlab.com/namespace/project/snippets/SNIPPET_ID.js"></script>
```
2021-03-11 19:13:27 +05:30
Here's what an embedded snippet looks like:
2018-10-15 14:42:47 +05:30
2019-12-04 20:38:33 +05:30
<script src="https://gitlab.com/gitlab-org/gitlab-foss/snippets/1717978.js"></script>
2018-10-15 14:42:47 +05:30
2021-03-11 19:13:27 +05:30
Embedded snippets are displayed with a header that shows:
- The filename, if defined.
- The snippet size.
- A link to GitLab.
- The actual snippet content.
Actions in the header enable users to see the snippet in raw format, and download it.