Skip to main content

No project description provided

Project description

Gossiphs = Gossip Graphs

Crates.io Version RealWorld Test

An experimental Rust library for general code file relationship analysis. 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

As a command line tool

You can find pre-compiled files for your platform on Our Release Page. After extraction, you can use gossiphs --help to find the corresponding help.

(👍Recommended) Export file relation matrix to csv

gossiphs relation
gossiphs relation --csv scores.csv --symbol-csv symbols.csv

And you can use something like pandas to handle this matrix and apply further analysis without accessing the rust part.

scores.csv

shows the relations between files by int score.

examples/mini.rs src/extractor.rs src/graph.rs src/lib.rs src/main.rs src/rule.rs src/server.rs src/symbol.rs
examples/mini.rs
src/extractor.rs 8 1
src/graph.rs 9 23 5
src/lib.rs
src/main.rs 5 1
src/rule.rs 18
src/server.rs 2
src/symbol.rs 1 28 64 32 13
  • By Column: src/graph.rs and src/symbol.rs have been used by example/mini.rs.
  • By Row: src/rule.rs has only been used by src/extractor.rs.
symbols.csv

shows the relations between files by real reference names.

examples/mini.rs src/extractor.rs src/graph.rs src/lib.rs src/main.rs src/rule.rs src/server.rs src/symbol.rs
examples/mini.rs
src/extractor.rs extract extract
src/graph.rs file_metadata|default|related_files|related_symbols|files related_files|files file_metadata|files|empty|related_files
src/lib.rs
src/main.rs default main
src/rule.rs get_rule
src/server.rs server_main
src/symbol.rs from new|id|new_ref|from|new_def list_definitions|list_symbols|id|link_symbol_to_symbol|link_file_to_symbol|list_references_by_definition|enhance_symbol_to_symbol|get_symbol|from|new|add_symbol|add_file|list_references|pairs_between_files|list_definitions_by_reference new|id|from|pairs_between_files list_references_by_definition|new|from|list_definitions_by_reference|get_symbol|id
  • By column: example/mini.rs using file_metadata/related_files ... from src/graph.rs.
Other functions ...

Diff with context

# diff between HEAD and HEAD~1
gossiphs diff

# custom diff
gossiphs diff --target HEAD~5
gossiphs diff --target d18a5db39752d244664a23f74e174448b66b5b7e

# output json
gossiphs diff --json

output:

src/services/user-info/index.ts
├── src/background-script/driveUploader.ts (ADDED)
├── src/background-script/task.ts (DELETED)
├── scripts/download-config.js (DELETED)
├── src/background-script/sdk.ts
├── src/services/user-info/listener.ts
├── src/services/config/index.ts
├── src/content-script/modal.ts
├── src/background-script/help-center.ts
  • ADDED: Refers to file relationships added in this diff
  • DELETED: Refers to file relationships deleted in this diff
  • Others: Refers to file relationships that were not affected by this diff and originally existed

Obsidian Graph

For example, you can use this command to generate an obsidian vault:

gossiphs obsidian --project-path . --vault-dir ./target_vault

and get a code relation graph:

image

As a rust library

Please refer to examples for usage.

fn main() {
    let config = GraphConfig::default();
    let g = Graph::from(config);

    // done! just try it
    let all_files = g.files();
    for file in &all_files {
        // related file search
        let related_files = g.related_files(file);
        for each_related in &related_files {
            println!("{} -> {}: {}", file, each_related.name, each_related.score);
        }

        // file details
        if !related_files.is_empty() {
            let random_file = related_files[0].name.clone();
            let meta = g.file_metadata(&random_file);
            println!("symbols in {}: {:?}", random_file, meta.symbols.len());

            // and query the symbol infos
        }
    }
}

As a local server

Starting a local server similar to LSP for other clients to use may be a reasonable approach, which is what we are currently doing.

./gossiphs server --project-path ./your/project --strict

API desc can be found here.

Goal & Motivation

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, but in strict mode, our calculation accuracy is still quite considerable. In normal mode, you can decide whether to adopt the relationship based on the weight returned.

Repo Precision (Strict Mode) Graph Generated Time
https://github.com/williamfzc/srctx 80/80 = 100 % 83.139791ms
https://github.com/gin-gonic/gin 160/167 = 95.80838 % 310.6805ms

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

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.9.16.tar.gz (46.4 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

gossiphs-0.9.16-cp38-abi3-manylinux_2_34_x86_64.whl (4.8 MB view details)

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

gossiphs-0.9.16-cp38-abi3-manylinux_2_34_i686.whl (4.8 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.34+ i686

gossiphs-0.9.16-cp38-abi3-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

gossiphs-0.9.16-cp38-abi3-macosx_10_12_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.8+ macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for gossiphs-0.9.16.tar.gz
Algorithm Hash digest
SHA256 c8deaafd09af189b473d388d76cb894a03115788a04025f6c5c19eae250e5c2d
MD5 8671352d0a2c68bd0b51a7213b7146ae
BLAKE2b-256 b2aed6b2b61340aaae5ae75ea7fd0edf265baef4aece246568bced1b9358314a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gossiphs-0.9.16-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 94897fe8edc24a016c238b5d264185d5235ab38e02f724fe21f1fa87e2bf4392
MD5 038b49eb5ecb4a0f1de56c975713fc50
BLAKE2b-256 a4acf82f73efd8da9cf4b930da7cd2e00c01408fb76e67c7e90d83d0e48a2173

See more details on using hashes here.

File details

Details for the file gossiphs-0.9.16-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for gossiphs-0.9.16-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f51c36eecd58960e48ed224988f0632f71893585051e7bc21b7a6c6bdb5f1fdd
MD5 941a9839b23c07714ff0c80654cf171e
BLAKE2b-256 78182243cb16f52daac7772f24ead929179da5e9d5c9d204a55f6cb1985dbe8e

See more details on using hashes here.

File details

Details for the file gossiphs-0.9.16-cp38-abi3-manylinux_2_34_i686.whl.

File metadata

File hashes

Hashes for gossiphs-0.9.16-cp38-abi3-manylinux_2_34_i686.whl
Algorithm Hash digest
SHA256 d4f174caa9ab5eaf9b01e1ec3dd8df05bca92683431a69cdb514357c73328f4a
MD5 955f996821eb14e2d8d1afd2d53a620e
BLAKE2b-256 53ec4ff58ddf73a229a7a9f2403a4b8bd02f779ee3d6cbbb4c272508800c7c55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gossiphs-0.9.16-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b79a5b2fe0cc909995ec9dcc5f7b066b2832c880c344ac4a736c02944273d3b6
MD5 41f2c3aff73cc4d8b413dbe6a88bf616
BLAKE2b-256 42d00c0ca75ce900c2fa39d32d02a6eba0a338354d07ce68d84781c9fdafb066

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gossiphs-0.9.16-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7c84f323b29b377762e0566a3b288c5a49ae952681afa4d946fa9fb5e15d052a
MD5 7b22ae49ab684f8f04566cde22ac6f45
BLAKE2b-256 6ad4a948c90c20ddc17045aaf8fd76bfa2cdb7f3463541c14ce11e81ea59fad8

See more details on using hashes here.

Supported by

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