Skip to content

Commit 2ae0437

Browse files
committed
Fixed #6
1 parent 954bdfc commit 2ae0437

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Solutions that I've found online looked at changes to files irrespective whether
1212
*Tested with Python version 3.5.3 and Git version 2.20.1*
1313

1414
# How it works
15-
This script looks at a range of commits per author. For each commit it bookkeeps the files that were changed along with the LOC for each file. LOC are kept in a sparse structure and changes per LOC are taken into account as the program loops. When a change to the same LOC is detected it updates this separately to bookkeep the true code churn.
15+
This lightweight script looks at a range of commits per author. For each commit it bookkeeps the files that were changed along with the LOC for each file. LOC are kept in a sparse structure and changes per LOC are taken into account as the program loops. When a change to the same LOC is detected it updates this separately to bookkeep the true code churn.
1616
Result is a print with aggregated contribution and churn per author for a given time period.
1717

1818
***Note:*** This includes the `--no-merges` flag as it assumes that merge commits with or without merge conflicts are not indicative of churn.

gitcodechurn.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'''
22
Author: Francis Laclé
33
License: MIT
4-
Version: 0.1
4+
Version: 1.0.1
55
66
Script to compute "true" code churn of a Git repository.
77
@@ -13,8 +13,8 @@
1313
1414
Reference: https://blog.gitprime.com/why-code-churn-matters/
1515
16-
This script looks at a range of commits per author. For each commit it
17-
book-keeps the files that were changed along with the lines of code (LOC)
16+
This lightweight script looks at a range of commits per author. For each commit
17+
it book-keeps the files that were changed along with the lines of code (LOC)
1818
for each file. LOC are kept in a sparse structure and changes per LOC are taken
1919
into account as the program loops. When a change to the same LOC is detected it
2020
updates this separately to bookkeep the true code churn.
@@ -170,8 +170,6 @@ def get_loc_change(loc_changes):
170170
else:
171171
return {left : left_dec, right: right_dec}
172172

173-
174-
175173
def is_loc_change(result, loc_changes):
176174
# search for loc changes (@@ ) and update loc_changes variable
177175
if result.startswith('@@'):
@@ -191,10 +189,37 @@ def is_new_file(result, file):
191189
def get_commits(before, after, author, dir):
192190
# note --no-merges flag (usually we coders do not overhaul contrib commits)
193191
# note --reverse flag to traverse history from past to present
192+
before = format_date(before)
193+
after = format_date(after)
194194
command = 'git log --author="'+author+'" --format="%h" --no-abbrev '
195195
command += '--before="'+before+'" --after="'+after+'" --no-merges --reverse'
196196
return get_proc_out(command, dir).splitlines()
197197

198+
# issue #6: append to date if it's missing month or day values
199+
def format_date(d):
200+
d = d[:-1] if d.endswith('-') else d
201+
if len(d) < 6:
202+
# after is interpreted as 'after the year YYYY'
203+
return d[0:4]+'-12-31'
204+
elif len(d) < 8:
205+
# here we need to check on which day a month ends
206+
dt = datetime.datetime.strptime(d, '%Y-%m')
207+
dt_day = get_month_last_day(dt)
208+
dt_month = '{:02d}'.format(dt.month).__str__()
209+
return d[0:4]+'-'+dt_month+'-'+dt_day
210+
else:
211+
dt = datetime.datetime.strptime(d, '%Y-%m-%d')
212+
dt_day = '{:02d}'.format(dt.day).__str__()
213+
dt_month = '{:02d}'.format(dt.month).__str__()
214+
return d[0:4]+'-'+dt_month+'-'+dt_day
215+
216+
# https://stackoverflow.com/a/43088
217+
def get_month_last_day(date):
218+
if date.month == 12:
219+
return date.replace(day=31)
220+
ld = date.replace(month=date.month+1, day=1)-datetime.timedelta(days=1)
221+
return ld.day.__str__()
222+
198223
# not used but still could be of value in the future
199224
def get_files(commit, dir):
200225
# this also works in case --no-merges flag is ommitted prior

0 commit comments

Comments
 (0)