find oldest line of code in git repository?
Asked Answered
git
K

2

9

Is there any way to find the oldest line of code currently in the codebase? Out of curiosity, I'd like to be able to find the line or lines that have been unchanged for the longest time. I can imagine there is some way of running git blame on each file in the repository and processing each of them, but I was wondering if there is a simpler option.

Kwon answered 11/7, 2021 at 23:53 Comment(4)
I'd try a git diff against the very first checkin, and work my way upwards (I'd be very suprised if nothing survived). That can be get confused by files being renamed, lines copied from a file to another one, and so on.Text
The first commit(s) contain the oldest line(s). That's not what you want: you want the oldest surviving lines. But that's a hard problem: is a line that matches, today, a line in the first ever commit, but that was created anew without referring to that commit, the same line? Is the duplicate Ship of Theseus the same ship as the original?Mannerly
You'll need to define precisely what "oldest surviving line" really means. Blank lines generally survive forever, but are not useful metrics.Mannerly
@Mannerly yeah, I'd like to exclude blank lines. I wrote a short Ruby script for this and discovered that there are quite a few blank lines from the very first commit. So that's not as fun.Kwon
N
0

My solution to this: Using bash/git bash

git ls-files | xargs -L 1 git --no-pager blame | sed -E "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2})/\1/" | sort | head -n 1

This lists all files, blames each of them, removes everything up to the date, then sorts them alphabetically (which now is synonymous with chronologically) and displays the first (oldest) one.

You may wish to modify the format of git-blame so it presents more nicely / shows filename + line / doesn't discard the information before the timestamp - if you were to put the date first, it would make "sed" redundant.

Neuroglia answered 5/8 at 11:52 Comment(0)
V
-5

You can use git blam that will tell you who and when single line updated in a file and you can easily find oldest update line Here is command

git blame triple_welcome.rb

Victoriavictorian answered 12/7, 2021 at 0:1 Comment(1)
I'm talking about the oldest line in the entire repository, not just in a file.Kwon

© 2022 - 2024 — McMap. All rights reserved.