├── LICENSE ├── README.md └── git-treesame-commit /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 JT Olio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-treesame-commit 2 | 3 | Utility to make new commits that are 4 | [treesame](https://git-scm.com/docs/git-log#_history_simplification) 5 | with others. 6 | 7 | Every commit has a _treehash_, or, a specific identifier that represents 8 | the state of the file tree. Two commits that have the exact same file 9 | tree and thus the exact same treehash are called _treesame_. 10 | 11 | This tool allows you to create a new commit on your current branch that 12 | is treesame with any arbitrary commit anywhere else. It's basically like 13 | being able to cherry-pick the entire state of a file tree. 14 | 15 | This tool has transformed how quickly I am able to figure out and 16 | debug git situations and I can't understand how this isn't built in to 17 | git already. 18 | 19 | ``` 20 | usage: git-treesame-commit [-h] [-a] [-p] [-m MESSAGE] commit 21 | 22 | Make treesame commits 23 | 24 | positional arguments: 25 | commit the commit to match 26 | 27 | optional arguments: 28 | -h, --help show this help message and exit 29 | -a, --keep-author if set, keep the commit's original author 30 | -p, --both-parents if set, commit will have both parents listed 31 | -m MESSAGE, --message MESSAGE 32 | the new commit message 33 | ``` 34 | 35 | ### License 36 | 37 | ``` 38 | MIT License 39 | 40 | Copyright (c) 2018 JT Olio 41 | 42 | Permission is hereby granted, free of charge, to any person obtaining a copy 43 | of this software and associated documentation files (the "Software"), to deal 44 | in the Software without restriction, including without limitation the rights 45 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 46 | copies of the Software, and to permit persons to whom the Software is 47 | furnished to do so, subject to the following conditions: 48 | 49 | The above copyright notice and this permission notice shall be included in all 50 | copies or substantial portions of the Software. 51 | 52 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 53 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 54 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 55 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 56 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 57 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 58 | SOFTWARE. 59 | ``` 60 | -------------------------------------------------------------------------------- /git-treesame-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | 3 | import argparse 4 | import subprocess 5 | import textwrap 6 | 7 | __copyright__ = "Copyright (C) 2018 JT Olio " 8 | 9 | 10 | def run(cmd, input=""): 11 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 12 | out, _ = proc.communicate(input) 13 | rv = proc.poll() 14 | if rv: 15 | raise subprocess.CalledProcessError(rv, cmd, out) 16 | return out 17 | 18 | 19 | def parse_commit(commit): 20 | d = {} 21 | for line in commit.rstrip().split("\n"): 22 | line = line.rstrip() 23 | if not line: 24 | return d 25 | header, value = line.split(" ", 1) 26 | if header in ("tree", "author", "committer"): 27 | d[header] = value 28 | return d 29 | 30 | 31 | def main(): 32 | parser = argparse.ArgumentParser(description="Make treesame commits") 33 | parser.add_argument("commit", help="the commit to match") 34 | parser.add_argument("-a", "--keep-author", action="store_true", 35 | help="if set, keep the commit's original author") 36 | parser.add_argument("-p", "--both-parents", action="store_true", 37 | help="if set, commit will have both parents listed") 38 | parser.add_argument("-m", "--message", help="the new commit message") 39 | args = parser.parse_args() 40 | 41 | newparent = run(["git", "log", "--format=%H", "-1", "HEAD"]).strip() 42 | commithash = run(["git", "log", "--format=%H", "-1", args.commit]).strip() 43 | commit = parse_commit(run(["git", "cat-file", "commit", commithash])) 44 | 45 | parents = ["parent %s" % newparent] 46 | if args.both_parents: 47 | parents.append("parent %s" % commithash) 48 | 49 | newcommit = textwrap.dedent("""\ 50 | tree {tree} 51 | {parents} 52 | author {author} 53 | committer {committer} 54 | 55 | Treesame commit: {tree} 56 | """).format(parents="\n".join(parents), **commit) 57 | 58 | newhash = run(["git", "hash-object", "-t", "commit", "--stdin", "-w"], 59 | newcommit).strip() 60 | run(["git", "reset", "--hard", newhash]) 61 | amend = ["git", "commit", "--amend", "--allow-empty"] 62 | if not args.keep_author: 63 | amend.append("--reset-author") 64 | message = args.message or ("Treesame commit: %s" % commit["tree"]) 65 | run(amend + ["-m", message]) 66 | 67 | 68 | if __name__ == "__main__": 69 | main() 70 | --------------------------------------------------------------------------------