git/list[1] front-page[2] threads[3] people[4] search[5] about
 

History cleanup/rewriting script for git

From
Jan Harkes <jaharkes@cs.cmu.edu>
Date
Apr 20, 2007, 15:54 UTC
Message-ID
<20070420155446.GA11506@delft.aura.cs.cmu.edu>
In-Reply-To
<alpine.LFD.0.98.0704190940330.9964@woody.linux-foundation.org>
On Thu, Apr 19, 2007 at 09:43:50AM -0700, Linus Torvalds wrote:
Show 9 quoted lines
> On Thu, 19 Apr 2007, Johannes Schindelin wrote:
> > Hmm. However, I have to say that cogito serves/d another purpose quite 
> > well: Look at what came from cogito into git. Loads of useful 
> > enhancements. So, I really have to point to "at this stage", because that 
> > sure was not true 18 months ago.
> 
> Absolutely. I think there are still some pieces of cogito that we might 
> want to migrate into git too, although they're fairly esoteric (ie the 
> whole history rewriting thing). And I think we still have some places 

I actually have a fairly simple history rewriting script (written in python) that I used when I converted some CVS archives to git. It is really intended for such an initial import and history cleanup case so it doesn't deal with reflogs and such.

Basic workflow I used is,
- Import CVS archive into a git repository
- Use gitk + the grafts file to clean up history as much as feasible
- Run git-rewrite-history.py which will
    - write out new commit objects with the corrected set of parents
    - copy existing refs to .git/newrefs, pointing them at the new commits.
- start gitk --all to see the tree before the rewrite.
- mv .git/refs .git/oldrefs ; mv .git/newrefs .git/refs
- start a second gitk --all to see the tree after the rewrite.
- compare gitk output to check if everything matches up.
- run git repack/prune/gc to get rid of the old commits, or clone the repo.
Jan
--8<-----------------------------------------------------------------------
#!/usr/bin/python
import os, sys
def git_write_object(type, blob):
    stdin, stdout = os.popen2("git-hash-object -t %s -w --stdin" % type)
    stdin.write(blob)
    stdin.close()
    return stdout.readline().strip()
def git_commits(branch):
    f = os.popen('git-rev-list --parents --header --topo-order %s' % branch)
    buf = ''
    while 1:
	buf = buf + f.read(4096)
	if not buf: break
	if not '\0' in buf: continue
	commit, buf = buf.split('\0', 1)
	yield Commit(commit)
def git_update_ref(name, hash):
    os.system('git-update-ref "%s" "%s"' % (name, hash))
grafts = []
pending = []
rewriteable = []
remap = {}
todo = 0
class Commit:
    def __init__(self, commit):
	global grafts
	lines = commit.split('\n')
	parts = lines.pop(0).split()
	self.hash, self.parents = parts[0], parts[1:]
	self.tree = lines.pop(0)
	parents = []
	while lines[0][:7] == 'parent ':
	    parents = parents + lines.pop(0).split()[1:]
	if parents != self.parents:
	    grafts.append(self.hash)
	commit = []
	while 1:
	    line = lines.pop(0)
	    commit.append(line)
	    if not line: break
	for line in lines:
	    commit.append(line[4:])
	self.commit = '\n'.join(commit)
	self.wait = 0
	self.children = []
    def mark(self):
	global todo, pending
	self.wait = self.wait + 1
	if self.wait == 1:
	    todo = todo + 1
	    for child in self.children:
		pending.append(child.hash)
    def pick(self):
	global rewriteable
	self.wait = self.wait - 1
	if not self.wait:
	    rewriteable.append(self)
    def fixup(self, old_hash, new_hash):
	i = self.parents.index(old_hash)
	self.parents[i] = new_hash
	self.pick()
    def rehash(self):
	global todo, remap
	todo = todo - 1
	blob = self.tree + '\n'
	for parent in self.parents:
	    blob = blob + 'parent %s\n' % parent
	blob = blob + self.commit
	new_hash = git_write_object('commit', blob)
	remap[self.hash] = new_hash
	for child in self.children:
	    child.fixup(self.hash, new_hash)
print "Reading commits... ",
commits = {}
for commit in git_commits('--all'):
    commits[commit.hash] = commit
print "read %d commits, found %d grafts" % (len(commits), len(grafts))
print "Setting up reverse linkage"
for commit in commits.values():
    for parent in commit.parents:
	commits[parent].children.append(commit)
print "Propagating graft information... ",
# first mark all commits that will have to be rewritten.
for commit in grafts:
    commits[commit].mark()
for commit in pending:
    commits[commit].mark()
# pick those commits that do not depend on any earlier rewrites
for commit in grafts:
    commits[commit].pick()
print "%d commits need to be rewritten" % todo
print "Rewriting commits... "
while rewriteable:
    print "\rrewriting %5d/%5d commits" % (len(rewriteable), todo),
    rewriteable.pop().rehash()
print "done..."
print "Rewriting refs..."
for ref in os.popen('git-for-each-ref'):
    hash, type, name = ref.split()
    if type != 'commit': continue
    if remap.has_key(hash):
	hash = remap[hash]
    # write updated refs to .git/newrefs
    git_update_ref('new' + name, hash)
print "done..."
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 74 of 120 in “GIT vs Other: Need argument”
  1. Pietro MascagniApr 17, 2007
  2. Matthieu MoyApr 17, 2007
  3. Andy ParkinsApr 17, 2007
  4. Alex RiesenApr 17, 2007
  5. Martin LanghoffApr 17, 2007
  6. Linus TorvaldsApr 17, 2007
  7. Matthieu MoyApr 17, 2007
  8. Martin LanghoffApr 17, 2007
  9. Alex RiesenApr 17, 2007
  10. Dana HowApr 25, 2007
  11. Alex RiesenApr 25, 2007
  12. Tomash BrechkoApr 17, 2007
  13. Guilhem BonnefilleApr 17, 2007
  14. Andy ParkinsApr 17, 2007
  15. Shawn O. PearceApr 17, 2007
  16. Marcin KasperskiApr 17, 2007
  17. Johannes SchindelinApr 18, 2007
  18. Linus TorvaldsApr 18, 2007
  19. Nicolas PitreApr 18, 2007
  20. Bill LearApr 18, 2007
  21. Matthieu MoyApr 18, 2007
  22. Nicolas PitreApr 18, 2007
  23. Matthieu MoyApr 19, 2007
  24. Petr BaudisApr 19, 2007
  25. Matthieu MoyApr 20, 2007
  26. Theodore TsoApr 18, 2007
  27. Guilhem BonnefilleApr 18, 2007
  28. Linus TorvaldsApr 18, 2007
  29. Daniel BarkalowApr 18, 2007
  30. Michael K. EdwardsApr 18, 2007
  31. Johannes SchindelinApr 19, 2007
  32. Matthieu MoyApr 19, 2007
  33. Johannes SchindelinApr 19, 2007
  34. Alex RiesenApr 19, 2007
  35. Christian MICHONApr 19, 2007
  36. Johannes SchindelinApr 19, 2007
  37. Christian MICHONApr 19, 2007
  38. Linus TorvaldsApr 19, 2007
  39. Marcin KasperskiApr 19, 2007
  40. Linus TorvaldsApr 19, 2007
  41. Carl WorthApr 23, 2007
  42. Josef WeidendorferApr 23, 2007
  43. Carl WorthApr 23, 2007
  44. Junio C HamanoApr 23, 2007
  45. Carl WorthApr 23, 2007
  46. Linus TorvaldsApr 23, 2007
  47. Brian GernhardtApr 23, 2007
  48. Daniel BarkalowApr 24, 2007
  49. Junio C HamanoApr 24, 2007
  50. J. Bruce FieldsApr 24, 2007
  51. Linus TorvaldsApr 24, 2007
  52. J. Bruce FieldsApr 30, 2007
  53. Making git disappear when talking about my code (was: Re: GIT vs Other: Need argument)Carl Worth, Apr 25, 2007
  54. Carl WorthApr 25, 2007
  55. Linus TorvaldsApr 25, 2007
  56. Carl WorthApr 25, 2007
  57. Nicolas PitreApr 25, 2007
  58. Carl WorthApr 25, 2007
  59. Junio C HamanoApr 25, 2007
  60. Nicolas PitreApr 25, 2007
  61. Carl WorthApr 25, 2007
  62. Nicolas PitreApr 25, 2007
  63. Linus TorvaldsApr 25, 2007
  64. Daniel BarkalowApr 25, 2007
  65. Junio C HamanoApr 25, 2007
  66. Linus TorvaldsApr 25, 2007
  67. Nicolas PitreApr 25, 2007
  68. Daniel BarkalowApr 25, 2007
  69. Carl WorthApr 25, 2007
  70. Daniel BarkalowApr 25, 2007
  71. Nicolas PitreApr 25, 2007
  72. Junio C HamanoApr 23, 2007
  73. Johannes SchindelinApr 19, 2007
  74. History cleanup/rewriting script for gitJan Harkes, Apr 20, 2007
  75. Johannes SchindelinApr 20, 2007
  76. Petr BaudisApr 20, 2007
  77. Jan HarkesApr 20, 2007
  78. Marcin KasperskiApr 19, 2007
  79. Johannes SchindelinApr 19, 2007
  80. Marcin KasperskiApr 19, 2007
  81. Johannes SchindelinApr 19, 2007
  82. J. Bruce FieldsApr 19, 2007
  83. Theodore TsoApr 19, 2007
  84. [ANNOUNCE] Cogito is for salePetr Baudis, Apr 19, 2007
  85. Matthieu MoyApr 19, 2007
  86. Junio C HamanoApr 19, 2007
  87. Johannes SchindelinApr 19, 2007
  88. Guilhem BonnefilleApr 18, 2007
  89. Andy ParkinsApr 18, 2007
  90. Steven GrimmApr 18, 2007
  91. Jakub NarebskiApr 19, 2007
  92. Steven GrimmApr 19, 2007
  93. Jakub NarebskiApr 19, 2007
  94. Johannes SchindelinApr 19, 2007
  95. Julian PhillipsApr 19, 2007
  96. Steven GrimmApr 19, 2007
  97. Johannes SchindelinApr 19, 2007
  98. Junio C HamanoApr 19, 2007
  99. Junio C HamanoApr 19, 2007
  100. Steven GrimmApr 19, 2007
  101. Junio C HamanoApr 19, 2007
  102. Shawn O. PearceApr 20, 2007
  103. Jakub NarebskiApr 20, 2007
  104. Karl HasselströmApr 20, 2007
  105. Junio C HamanoApr 20, 2007
  106. Petr BaudisApr 20, 2007
  107. Junio C HamanoApr 20, 2007
  108. Steven GrimmApr 20, 2007
  109. Yann DirsonApr 18, 2007
  110. Sam VilainApr 18, 2007
  111. Yann DirsonApr 18, 2007
  112. Dana HowApr 25, 2007
  113. Marcin KasperskiApr 19, 2007
  114. Alex RiesenApr 19, 2007
  115. Andy ParkinsApr 19, 2007
  116. Shawn O. PearceApr 20, 2007
  117. Eric BlakeApr 20, 2007
  118. Johannes SchindelinApr 19, 2007
  119. Marcin KasperskiApr 19, 2007
  120. Johannes SchindelinApr 19, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.