1Search code across public repositories using Sourcegraph's GraphQL API.
  2
  3WHEN TO USE THIS TOOL:
  4
  5- Use when you need to find code examples or implementations across public repositories
  6- Helpful for researching how others have solved similar problems
  7- Useful for discovering patterns and best practices in open source code
  8
  9HOW TO USE:
 10
 11- Provide a search query using Sourcegraph's query syntax
 12- Optionally specify the number of results to return (default: 10)
 13- Optionally set a timeout for the request
 14
 15QUERY SYNTAX:
 16
 17- Basic search: "fmt.Println" searches for exact matches
 18- File filters: "file:.go fmt.Println" limits to Go files
 19- Repository filters: "repo:^github\.com/golang/go$ fmt.Println" limits to specific repos
 20- Language filters: "lang:go fmt.Println" limits to Go code
 21- Boolean operators: "fmt.Println AND log.Fatal" for combined terms
 22- Regular expressions: "fmt\.(Print|Printf|Println)" for pattern matching
 23- Quoted strings: "\"exact phrase\"" for exact phrase matching
 24- Exclude filters: "-file:test" or "-repo:forks" to exclude matches
 25
 26ADVANCED FILTERS:
 27
 28- Repository filters:
 29  - "repo:name" - Match repositories with name containing "name"
 30  - "repo:^github\.com/org/repo$" - Exact repository match
 31  - "repo:org/repo@branch" - Search specific branch
 32  - "repo:org/repo rev:branch" - Alternative branch syntax
 33  - "-repo:name" - Exclude repositories
 34  - "fork:yes" or "fork:only" - Include or only show forks
 35  - "archived:yes" or "archived:only" - Include or only show archived repos
 36  - "visibility:public" or "visibility:private" - Filter by visibility
 37
 38- File filters:
 39  - "file:\.js$" - Files with .js extension
 40  - "file:internal/" - Files in internal directory
 41  - "-file:test" - Exclude test files
 42  - "file:has.content(Copyright)" - Files containing "Copyright"
 43  - "file:has.contributor([email protected])" - Files with specific contributor
 44
 45- Content filters:
 46  - "content:\"exact string\"" - Search for exact string
 47  - "-content:\"unwanted\"" - Exclude files with unwanted content
 48  - "case:yes" - Case-sensitive search
 49
 50- Type filters:
 51  - "type:symbol" - Search for symbols (functions, classes, etc.)
 52  - "type:file" - Search file content only
 53  - "type:path" - Search filenames only
 54  - "type:diff" - Search code changes
 55  - "type:commit" - Search commit messages
 56
 57- Commit/diff search:
 58  - "after:\"1 month ago\"" - Commits after date
 59  - "before:\"2023-01-01\"" - Commits before date
 60  - "author:name" - Commits by author
 61  - "message:\"fix bug\"" - Commits with message
 62
 63- Result selection:
 64  - "select:repo" - Show only repository names
 65  - "select:file" - Show only file paths
 66  - "select:content" - Show only matching content
 67  - "select:symbol" - Show only matching symbols
 68
 69- Result control:
 70  - "count:100" - Return up to 100 results
 71  - "count:all" - Return all results
 72  - "timeout:30s" - Set search timeout
 73
 74EXAMPLES:
 75
 76- "file:.go context.WithTimeout" - Find Go code using context.WithTimeout
 77- "lang:typescript useState type:symbol" - Find TypeScript React useState hooks
 78- "repo:^github\.com/kubernetes/kubernetes$ pod list type:file" - Find Kubernetes files related to pod listing
 79- "repo:sourcegraph/sourcegraph$ after:\"3 months ago\" type:diff database" - Recent changes to database code
 80- "file:Dockerfile (alpine OR ubuntu) -content:alpine:latest" - Dockerfiles with specific base images
 81- "repo:has.path(\.py) file:requirements.txt tensorflow" - Python projects using TensorFlow
 82
 83BOOLEAN OPERATORS:
 84
 85- "term1 AND term2" - Results containing both terms
 86- "term1 OR term2" - Results containing either term
 87- "term1 NOT term2" - Results with term1 but not term2
 88- "term1 and (term2 or term3)" - Grouping with parentheses
 89
 90LIMITATIONS:
 91
 92- Only searches public repositories
 93- Rate limits may apply
 94- Complex queries may take longer to execute
 95- Maximum of 20 results per query
 96
 97TIPS:
 98
 99- Use specific file extensions to narrow results
100- Add repo: filters for more targeted searches
101- Use type:symbol to find function/method definitions
102- Use type:file to find relevant files