Giter Site home page Giter Site logo

hashcommit's Introduction

hashcommit

A tool to generate a Git commit with a specific hash part.

Prerequisites

Ensure you have Git installed on your system.

Installation

You can install the package using pip:

pip install hashcommit

Demo

This repository's history was rewritten using the rewrite_the_history.sh script and each new commit is added by using the hashcommit command. You can check the result here.

Usage

Creating a New Commit

To create a new commit with a specific hash part:

hashcommit --hash <desired_hash_part> --message "<commit_message>"

Overwriting the Last Commit

To overwrite the last commit with a specific hash part:

hashcommit --hash <desired_hash_part> --message "<commit_message>" --overwrite

Match Type

You can also specify if the hash must begin with, contain, or end with the desired string using the --match-type option. The default is to match the beginning of the hash:

hashcommit --hash <desired_hash_part> --message "<commit_message>" --match-type <begin|contain|end>

Example Usage

To find and use a specific commit hash:

hashcommit --hash fff --match-type contain --message "foobar" ; git log -1 | cat

Example output:

Found matching commit hash: 93fffe4756192c250a7234c7c5fd81752c747091
commit 93fffe4756192c250a7234c7c5fd81752c747091
Author: Your Name <[email protected]>
Date:   Thu May 23 17:06:24 2024 +0000

    foobar

Author Preservation

By default, the author is preserved when overwriting. To overwrite the author, use the --no-preserve-author option:

hashcommit --hash <desired_hash_part> --overwrite --no-preserve-author

Overwriting Commits in the Past

You can overwrite the existing commit that has other commits on top of it. To do this, use the --commit option:

hashcommit --hash <desired_hash_part> --overwrite --commit <commit_hash>

Rewriting the History

You can rewrite the history of the current branch using the rewrite_the_history.sh script. This script will recreate the commit history, ensuring that each commit's hash conforms to a sequence specified by the -d argument, which sets the number of digits for the sequence number.

For example, to rewrite the history with a two-digit sequence number at the beginning:

./scripts/rewrite_the_history.sh -d 2

Note: The default value for -d is 3. As the number of commits increases, consider adjusting the digit value accordingly to balance performance and the required hash length.

Development

To develop or contribute to this project, clone the repository and install the dependencies:

git clone https://github.com/wozniakpl/hashcommit.git
cd hashcommit
pip install -e .

Running Locally

You can run tests locally using tox or act:

# Using tox
tox

# Using act
act

You can use the following command for simplicity of development. It formats the code, runs the checks, and the tests on one Python version:

tox -e format && tox -e checks && tox -e py312 --

To run tox tests under docker (not using your git):

docker compose up

To set up an environment with hashcommit installed and a git repository initialized in the /repo directory, use Docker Compose:

docker compose run --rm --workdir /repo test bash

License

This project is licensed under the MIT License.

hashcommit's People

Contributors

wozniakpl avatar

Stargazers

Marek Biczysko avatar  avatar  avatar

Watchers

 avatar

hashcommit's Issues

Overwrite could preserve author and committer data

Before using the tool author and committer data looks like this:

$ git log -1 --pretty=format:"%an / %ae / %ad" && git log -1 --pretty=format:"%cn / %ce / %cd"
Bartosz Woźniak / [email protected] / Thu May 23 19:08:08 2024 +0200
Bartosz Woźniak / [email protected] / Thu May 23 19:08:08 2024 +0200

After calling python3 ./hashcommit/main.py --overwrite --hash 123 --message "update readme" --match-type begin it transforms into:

git log -1 --pretty=format:"%an / %ae / %ad" && git log -1 --pretty=format:"%cn / %ce / %cd"    
Blank Spruce / [email protected] / Thu May 23 18:07:53 2024 +0000
Blank Spruce / [email protected] / Thu May 23 18:07:53 2024 +0000

I'd be nice to preserve original author and committer data.

It'd be also nice if overwrite mode didn't require providing commit message since user can leverage commit --amend to do that in two-step solution

  1. Amend the commit message or commit content however you like as usual.
  2. Improve commit hash with this tool.

The following feature request may or may not be used for forging stuff.

Excellent tool idea. 😆

Commit history could be great proof of concept

Current history of this repository looks like this:

241636c0ac691b7e7eb63604354623ec7d5622f4
93fffe4756192c250a7234c7c5fd81752c747091
6ff152f29ea562c3ada62b173374ccc2f2c899f7
9e0ab1f3cfcf1d059d61b79ceb1dac2307cecaa5

I propose updating history so that commits are numbered in commit* order with pattern <commit-number>aa<rest-of-commit-hash>. Example:

1aa636c0ac691b7e7eb63604354623ec7d5622f4
2aaffe4756192c250a7234c7c5fd81752c747091
3aa152f29ea562c3ada62b173374ccc2f2c899f7
4aaab1f3cfcf1d059d61b79ceb1dac2307cecaa5
... in some far future ...
2137aaf3cfcf1d059d61b79ceb1dac2307cecaa5
2138aaf3cfcf1d059d61b79ceb1dac2307cecaa5

You might need #1 to achieve that.

* - I'd have called it "chronological" but git doesn't require that commits have in fact chronological timestamps.

`git fsck` shows unreachable objects

This is due to a bug in implementation that does not clean after itself, when looking for a desired hash.

After running hashcommit, script should clean up the unreachable commits if the hash did not match.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.