Git does not track directories as such. It only tracks files that live in some directory. (See How can I add an empty directory to a Git repository?)
However, if I have certain history of commits I implicitly also have history of changes to the directory tree.
So how do I answer questions like:
The closest I could come up with is in pseudo code:
loop over all commits (git rev-list --all)
start from repo root directory
do recursively on the directory tree rebuilt so far
call git ls-tree and grep the tree lines
rebuild next level of directory tree
end
end
Obviously this could be written in your favorite scripting language.
Then I have all directory trees and I still need to search them in a smart way in order to be to answer questions of type 1 - 3. Again, doable but probably not in a couple of minutes.
The questions are:
Git file History provides information about the commit history associated with a file. To use it: Go to your project's Repository > Files. In the upper right corner, select History.
After you have created several commits, or if you have cloned a repository with an existing commit history, you'll probably want to look back to see what has happened. The most basic and powerful tool to do this is the git log command.
`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.
Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo .
For questions 1 and 2, it's quite easy:
when was directory foo/bar created?git log --oneline -- foo/bar | tail -n 1
when was directory foo/bar deleted?git log --oneline -- foo/bar | head -n 1
However, the third part is a bit tricky and I cannot answer it completely.
The two commands above give you $FIRST_REV
(created) and $LAST_REV
(deleted).
The following snippet gives you all commits where the tree was modified:
for rev in $(git rev-list FIRST_REV..LAST_REV)
do
git ls-tree -r -t $rev | grep tree | cut -f 2
done
Then, you have a list of directories that were present. But there are still duplicates. Pass that list to a sort -u
and you're done:
#!/bin/sh
for r in $(git rev-list FIRST_REV..LAST_REV)
do
git ls-tree -r -t $r | grep tree | cut -f 2
done | sort -u
However, you lose the information of the commits where these directories were affected. That's the drawback.
And, this assumes that foo/bar
was created only once and is no longer present.
gitk foo/bar
gives you a user interface for browsing the git history limited to commits that touched foo/bar
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With