i need to write a python script to pull the repository details of mercurial for given 2 dates

462 views Asked by At

i have referred a program which prints all the repository details. Now i want to provide 2 dates i.e. from date and to date as parameters and the repository details between these 2 dates needs to be pulled out. How can this be done. I am not sure which mercurial api to be used.

import sys
import hglib

# repo path 

# figure out what repo path to use
repo = "."
if len(sys.argv) > 3:
    repo = sys.argv[1],
    from_date = sys.argv[2],
    to_date = sys.argv[3]


# connect to hg
client = hglib.open(repo)

# gather some stats
revs = int(client.tip().rev)
files = len(list(client.manifest()))
heads = len(client.heads())
branches = len(client.branches())
tags = len(client.tags()) - 1 # don't count tip


authors = {}
for e in client.log():
    authors[e.author] = True

description = {}
for e in client.log():
    description[e.desc] = True 


merges = 0
for e in client.log(onlymerges=True):
    merges += 1

print "%d revisions" % revs
print "%d merges" % merges
print "%d files" % files
print "%d heads" % heads
print "%d branches" % branches
print "%d tags" % tags
print "%d authors" % len(authors)
print "%s authors name" % authors.keys()
print "%d desc" % len(description)

This prints out everything in the repository, i need to pull the details between the two given dates lie 2015-07-13 (from date) and 2015-08-20(todate)

Updated code not working

import sys
import hglib
import datetime as dt


# repo path 

# figure out what repo path to use
repo = "."
if len(sys.argv) > 1:
    repo = sys.argv[1]
#from_date = sys.argv[2],
#to_date = sys.argv[3]

# connect to hg
client = hglib.open(repo)       

# define time ranges
d1 = dt.datetime(2015,7,7)
d2 = dt.datetime(2015,8,31)


#if "date('>05/07/07') and date('<06/08/8')"

#logdetails = client.log()


description = {}
for e in client.log():
    if (description[e.date] >= d1 and  description[e.date] <=d2):  
        description[e.desc] = True    
    print "%s desc" % description
1

There are 1 answers

1
planetmaker On BEST ANSWER

You can use revsets to limit the changesets. I'm not exactly sure how it translates to the hglib API, but it has an interface for revsets as well. From the normal CLI you can do like:

hg log -r"date('>2015-01-01') and date('<2015-03-25')"

Checkout hg help revsets and hg help dates.

By the way: the output of of the numeric revision count revs = int(client.tip().rev) will be wrong (too big) if there are pruned or obsolete changesets which are for instance easily created by means of hg commit --amend.