Viktor
Viktor

Reputation: 885

Search for code or text in GitLab

Is it possible to search for code or text in GitLab inside of files? I can search for files, issues, milestones, etc., but could not find a way to search for code in source files or text in the documentation i.e .doc files.

Upvotes: 70

Views: 132359

Answers (4)

otterrisk
otterrisk

Reputation: 699

If you have advanced search enabled, you can search the content of files using the filename: syntax, e.g. to search for a "string" in all files leave the filename empty:

string -filename:

That's how it could look like in the Web-UI:

enter image description here

Upvotes: 2

nspire
nspire

Reputation: 1721

We have a paid gitlab account such that our domain is gitlab.ourcompany.com - so this answer might not apply to all. There have been a few occassions where I needed to search for a "string" across all files in a Gitlab project, but there is no clear 'search' button. The search bar only searches for matching project names - so not what I was looking for. This method below is the easiest to use and easiest to remember.

https://gitlab.xxxxx.com/search

enter image description here

Upvotes: 16

Chhavi Gupta
Chhavi Gupta

Reputation: 89

To search files on GitLab, hit URL https://gitlab.com/search?utf8=%E2%9C%93&search_code=true&repository_ref={BranchName}

Select respective group and project from the dropdown.

Enter the text to be searched and click on 'Search' button.

Upvotes: 8

Rubén Pozo
Rubén Pozo

Reputation: 1083

Alternatively you can use the python-gitlab library to search text in the projects that you need:

import gitlab


def search(gitlab_server, token, file_filter, text, group=None, project_filter=None):
    return_value = []
    gl = gitlab.Gitlab(gitlab_server, private_token=token)
    if (project_filter == '') and (group == ''):
        projects = gl.projects.list(all=True)
    else:
        group_object = gl.groups.get(group)
        group_projects = group_object.projects.list(search=project_filter)
        projects = []
        for group_project in group_projects:
            projects.append(gl.projects.get(group_project.id))
    for project in projects:
        files = []
        try:
            files = project.repository_tree(recursive=True, all=True)
        except Exception as e:
            print(str(e), "Error getting tree in project:", project.name)
        for file in files:
            if file_filter == file['name']:
                file_content = project.files.raw(file_path=file['path'], ref='master')
                if text in str(file_content):
                    return_value.append({
                        "project": project.name,
                        "file": file['path']
                    })
    return return_value

Complete example can be found here: gitlab-search

Upvotes: 0

Related Questions