Sunday, August 6, 2017

script to show file diff between period of time

*** gitshow.py ****
import git
import re
import datetime
import sys
import os

path_to_repo="./gitdir"
r = git.Repo(path_to_repo)
logs = r.git.log()

logsl = logs.split("\n")
commitdict = {}
n = 1
typed  = sys.argv[1]
dates = sys.argv[2]
files = sys.argv[3]
for i in logsl:
    if re.search("commit ", i):
        hexcommit = i.split()[1]
        datecommit = re.sub("Date:", "",logsl[n+1]).strip()

        dcom = datetime.datetime.strptime(datecommit, "%a %b %d %H:%M:%S %Y %z")
        dcomfloat = float((dcom.strftime('%s')))

        commitdict[dcomfloat] = hexcommit

    n += 1
# res = r.git.show("%s:%s" % (hexsha, file_path))

# def gitshowfile(dicts,)
now = float(datetime.datetime.now().strftime('%s'))
if typed == "last":
    numday = float(dates) * 24 * 60 * 60
    if files == "all":
        for i in commitdict:
            if i > now - numday:
                # shows = r.git.show("%s:%s" % (hexsha, file_path))
                shows = r.git.show(commitdict[i])
                print(shows)
    else:

        hdate  =datetime.datetime.fromtimestamp(now - numday)
        hdates = hdate.strftime('%a %b %d %H:%M:%S %Y')
        os.chdir(path_to_repo)
        # print (os.getcwd())
        os.system("git log --follow -p --after '" + hdates + "' " + files)

if typed == "between":
    # numday = float(dates) * 24 * 60 * 60
    datesl = dates.split("-")
    d1 = float(datetime.datetime.strptime(datesl[0], "%d/%m/%y").strftime('%s'))
    d2 = float(datetime.datetime.strptime(datesl[1], "%d/%m/%y").strftime('%s'))
    if files == "all":
        for i in commitdict:
            if i < d2:
                if i > d1:
                # shows = r.git.show("%s:%s" % (hexsha, file_path))
                    shows = r.git.show(commitdict[i])
                    print(shows)
    else:

        hdate2  =datetime.datetime.fromtimestamp(d2)
        hdates2 = hdate2.strftime('%a %b %d %H:%M:%S %Y')
        hdate1  =datetime.datetime.fromtimestamp(d1)
        hdates1 = hdate1.strftime('%a %b %d %H:%M:%S %Y')

        os.chdir(path_to_repo)
        # print (os.getcwd())
        os.system("git log --follow -p --after '" + hdates1 + "' --until '" +hdates2 +"' " + files)




** usages ***
$ python3 gitshow.py between '25/06/17-30/06/17' file1
$ python3 gitshow.py last 10 file1

$ python3 gitshow.py between '25/06/17-30/06/17' all
$ python3 gitshow.py last 10 all

No comments:

Post a Comment