Aseem Kishore
Aseem Kishore

Reputation: 10868

Git filter-branch: possible to update commit messages to reference old commit IDs?

I can successfully run git filter-branch on some code to e.g. merge another repo into a subdirectory [1]:

git filter-branch --index-filter '
  git ls-files -s |
  perl -pe "s{\t\"?}{$&helper/}" |
    GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
  mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE
' remotes/helper/master

But now, I'd also love to expand each commit's message to include its former ID (SHA hash), since those commits may have had comments/conversation on GitHub which aren't getting transferred over.

E.g. add an extra line like this (where bob/helpers is hardcoded/known in advance):

[COPIED FROM bob/helpers@76c7c080b3bd2f93dc78e4864899d668a57cd9f9]

As far as I can tell, Git's msg-filter only gives me the original message, and Git's env-filter doesn't contain the commit ID (SHA has) as an input variable. Is this possible then, or no?

Thanks!

[1] Via this great article, with the Perl filter from this email thread to workaround a bug on Mac OS X.

Upvotes: 9

Views: 790

Answers (1)

torek
torek

Reputation: 488103

--msg-filter would be where to do it, and the original commit ID is in the environment variable $GIT_COMMIT, so (untested) make the message filter simply: cat; echo "[COPIED FROM bob/helpers@$GIT_COMMIT]".

Upvotes: 5

Related Questions