Skip to main content

No project description provided

Project description

Gossiphs = Gossip Graphs

Crates.io Version RealWorld Test

"Zero setup" general code file relationship analysis. With Python & Rust. Based on tree-sitter and git analysis.

What's it

Gossiphs can analyze the history of commits and the relationships between variable declarations and references in your codebase to obtain a relationship diagram of the code files.

It also allows developers to query the content declared in each file, thereby enabling free search for its references throughout the entire codebase to achieve more complex analysis.

graph TD
    A[main.py] --- S1[func_main] --- B[module_a.py]
    A --- S2[Handler] --- C[module_b.py]
    B --- S3[func_util] --- D[utils.py]
    C --- S3[func_util] --- D
    A --- S4[func_init] --- E[module_c.py]
    E --- S5[process] --- F[module_d.py]
    E --- S6[Processor] --- H[module_e.py]
    H --- S7[transform] --- I[module_f.py]
    I --- S3[func_util] --- D

Supported Languages

We are expanding language support based on Tree-Sitter Query, which isn't too costly. If you're interested, you can check out the contribution section.

Language Status
Rust
Python
TypeScript
JavaScript
Golang
Java
Kotlin
Swift

You can see the rule files here.

Usage

Python

pip install gossiphs

Analyze your codebase with networkx within 30 lines:

import networkx as nx
from gossiphs import GraphConfig, create_graph, Graph

config = GraphConfig()
config.project_path = "../.."
graph: Graph = create_graph(config)

nx_graph = nx.DiGraph()

for each_file in graph.files():
    nx_graph.add_node(each_file, metadata=graph.file_metadata(each_file))

    related_files = graph.related_files(each_file)
    for each_related_file in related_files:
        related_symbols = set(each_symbol.symbol.name for each_symbol in each_related_file.related_symbols)

        nx_graph.add_edge(
            each_file,
            each_related_file.name,
            related_symbols=list(related_symbols)
        )

print(f"NetworkX graph created with {nx_graph.number_of_nodes()} nodes and {nx_graph.number_of_edges()} edges.")

for src, dest, data in nx_graph.edges(data=True):
    print(f"{src} -> {dest}, related symbols: {data['related_symbols']}")

Output:

NetworkX graph created with 13 nodes and 27 edges.
src/server.rs -> src/main.rs, related symbols: ['server_main']
src/main.rs -> src/graph.rs, related symbols: ['default']
src/main.rs -> examples/mini.rs, related symbols: ['default']
src/main.rs -> src/server.rs, related symbols: ['main']
src/symbol.rs -> src/graph.rs, related symbols: ['link_file_to_symbol', 'list_references', 'list_references_by_definition', 'id', 'enhance_symbol_to_symbol', 'add_file', 'add_symbol', 'list_definitions', 'list_symbols', 'new', 'link_symbol_to_symbol', 'get_symbol']
...

More examples can be found here.

Others

We also provide a CLI and additional usage options, making it easy to directly export CSV files or start an HTTP service.

See usage page.

Goal & Motivation

[!TIP] Create a file relationship index with:

  • low cost
  • acceptable accuracy
  • high versatility for nearly any code repository

Code navigation is a fascinating subject that plays a pivotal role in various domains, such as:

  • Guiding the context during the development process within an IDE.
  • Facilitating more convenient code browsing on websites.
  • Analyzing the impact of code changes in Continuous Integration (CI) systems.
  • ...

In the past, I endeavored to apply LSP/LSIF technologies and techniques like Github's Stack-Graphs to impact analysis, encountering different challenges along the way. For our needs, a method akin to Stack-Graphs aligns most closely with our expectations. However, the challenges are evident: it requires crafting highly language-specific rules, which is a considerable investment for us, given that we do not require such high precision data.

We attempt to make some trade-offs on the challenges currently faced by stack-graphs to achieve our expected goals to a certain extent:

  • Zero repo-specific configuration: It can be applied to most languages and repositories without additional configuration.
  • Low extension cost: adding rules for languages is not high.
  • Acceptable precision: We have sacrificed a certain level of precision, but we also hope that it remains at an acceptable level.

How it works

Gossiphs constructs a graph that interconnects symbols of definitions and references.

  1. Extract imports and exports: Identify the imports and exports of each file.
  2. Connect nodes: Establish connections between potential definition and reference nodes.
  3. Refine edges with commit histories: Utilize commit histories to refine the relationships between nodes.

Unlike stack-graphs, we have omitted the highly complex scope analysis and instead opted to refine our edges using commit histories. This approach significantly reduces the complexity of rule writing, as the rules only need to specify which types of symbols should be exported or imported for each file.

While there is undoubtedly a trade-off in precision, the benefits are clear:

  1. Minimal impact on accuracy: In practical scenarios, the loss of precision is not as significant as one might expect.
  2. Commit history relevance: The use of commit history to reflect the influence between code segments aligns well with our objectives.
  3. Language support: We can easily support the vast majority of programming languages, meeting the analysis needs of various types of repositories.

Precision

Static analysis has its limits, such as dynamic binding. Therefore, it is unlikely to achieve the level of accuracy provided by LSP, but it can offer sufficient accuracy in the areas where it is primarily used.

The method we use to demonstrate accuracy is to compare the results with those of LSP/LSIF. It must be admitted that static inference is almost impossible to obtain all reference relationships like LSP.

You can further combine your own needs and use other methods such as tfidf to process the results to meet more complex requirements.

Repo Coverage of LSP Edges by Gossiphs
https://github.com/go-gorm/gorm 442/499 = 88.5 %
https://github.com/gin-gonic/gin 238/252 = 94.4%

Contribution

The project is still in a very early and experimental stage. If you are interested, please leave your thoughts through an issue. In the short term, we hope to build better support for more languages.

You just need to:

  1. Edit rules in src/rule.rs
  2. Test it in src/extractor.rs
  3. Try it with your repo in src/graph.rs

Tree-sitter Playground is a good helper.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gossiphs-0.11.4.tar.gz (48.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

gossiphs-0.11.4-cp38-abi3-win_amd64.whl (2.9 MB view details)

Uploaded CPython 3.8+Windows x86-64

gossiphs-0.11.4-cp38-abi3-manylinux_2_38_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.38+ x86-64

gossiphs-0.11.4-cp38-abi3-manylinux_2_38_i686.whl (4.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.38+ i686

gossiphs-0.11.4-cp38-abi3-macosx_11_0_arm64.whl (4.2 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

gossiphs-0.11.4-cp38-abi3-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file gossiphs-0.11.4.tar.gz.

File metadata

  • Download URL: gossiphs-0.11.4.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.1

File hashes

Hashes for gossiphs-0.11.4.tar.gz
Algorithm Hash digest
SHA256 3d1e8e74f86238827fb15fcf5f7f372b720ddbc7f2587761a5a22850a62c9270
MD5 b322da8cfe8cb7987cf2b49a75e9107e
BLAKE2b-256 12a11db8c82fbfb5b0ac4a94628ebf8975a29befb47b888d5277c07c913090ed

See more details on using hashes here.

File details

Details for the file gossiphs-0.11.4-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for gossiphs-0.11.4-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7363b8ced6cd91c1be04faa45510194d3b9b7be123d575a85b6a39c6cfca44d0
MD5 7ec066de61bac2b0e7526fbef5e63586
BLAKE2b-256 61e8f573f33f147e625d97c21b123829b3f05dea47cf3b0b70a8a2c2b1c74e0a

See more details on using hashes here.

File details

Details for the file gossiphs-0.11.4-cp38-abi3-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for gossiphs-0.11.4-cp38-abi3-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 515219834b0a25d70fc5426a97c99578a7b4c6c937d9f4ee7aa2c0d70d493709
MD5 e37205f11a8082e215f4a6a94d6bf0b8
BLAKE2b-256 1b32a634cade6ddd32de5f7a2621cff38fcc3f443cfc8655147cd4787e2a48dd

See more details on using hashes here.

File details

Details for the file gossiphs-0.11.4-cp38-abi3-manylinux_2_38_i686.whl.

File metadata

File hashes

Hashes for gossiphs-0.11.4-cp38-abi3-manylinux_2_38_i686.whl
Algorithm Hash digest
SHA256 27c91b928ea73d82b2926281d36e2932877f63a2413738d0b3ff1e06937b7be4
MD5 3eb3cfc72e950abf933b927d7fa7adea
BLAKE2b-256 e63de7229f9bf0691cdcd059540537d0d7a81f6413edb3f4a90a460fa92d56e3

See more details on using hashes here.

File details

Details for the file gossiphs-0.11.4-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gossiphs-0.11.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f9aa4abe257276054bc7e6bda278c768c9b0aa5ab7247d4bc56a8587122fc97
MD5 33bcfc71a60879920cb04efd0718e1ef
BLAKE2b-256 4bb1fe6c369a21b5e50c859cf0be9498108ab07041cb003e0e98512fc208e67d

See more details on using hashes here.

File details

Details for the file gossiphs-0.11.4-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for gossiphs-0.11.4-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ee5214fc731062ab346b4fe2ac8d2f49f3dfab4738da982306c8fe10d5cf6886
MD5 3f380380595d759d1794ad895bd9ae0d
BLAKE2b-256 94abaa575f8fbae8cfbde5feafd9594ec10ff179bee53c14f81582785aadabdf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page