@@ -391,6 +391,53 @@ def _check_connection(self, check_db=True) -> None:
391391 "No database is connected. Please either connect to a database or create a new database."
392392 )
393393
394+ def log (self ,
395+ team : Optional [str ] = None ,
396+ db : Optional [str ] = None ,
397+ start : int = 0 ,
398+ count : int = - 1 ):
399+ """Get commit history of a database
400+ Parameters
401+ ----------
402+ team: str, optional
403+ The team from which the database is. Defaults to the class property.
404+ db: str, optional
405+ The database. Defaults to the class property.
406+ start: int, optional
407+ Commit index to start from. Defaults to 0.
408+ count: int, optional
409+ Amount of commits to get. Defaults to -1 which gets all.
410+
411+ Returns
412+ -------
413+ list
414+
415+ List of the following commit objects:
416+ {
417+ "@id":"InitialCommit/hpl18q42dbnab4vzq8me4bg1xn8p2a0",
418+ "@type":"InitialCommit",
419+ "author":"system",
420+ "identifier":"hpl18q42dbnab4vzq8me4bg1xn8p2a0",
421+ "message":"create initial schema",
422+ "schema":"layer_data:Layer_4234adfe377fa9563a17ad764ac37f5dcb14de13668ea725ef0748248229a91b",
423+ "timestamp":1660919664.9129035
424+ }
425+ """
426+ self ._check_connection (check_db = (not team or not db ))
427+ team = team if team else self .team
428+ db = db if db else self .db
429+ result = requests .get (
430+ f"{ self .api } /log/{ team } /{ db } " ,
431+ params = {'start' : start , 'count' : count },
432+ headers = self ._default_headers ,
433+ auth = self ._auth (),
434+ )
435+ commits = json .loads (_finish_response (result ))
436+ for commit in commits :
437+ commit ['timestamp' ] = datetime .fromtimestamp (commit ['timestamp' ])
438+ commit ['commit' ] = commit ['identifier' ] # For backwards compat.
439+ return commits
440+
394441 def get_commit_history (self , max_history : int = 500 ) -> list :
395442 """Get the whole commit history.
396443 Commit history - Commit id, author of the commit, commit message and the commit time, in the current branch from the current commit, ordered backwards in time, will be returned in a dictionary in the follow format:
@@ -403,7 +450,7 @@ def get_commit_history(self, max_history: int = 500) -> list:
403450 Parameters
404451 ----------
405452 max_history: int, optional
406- maximum number of commit that would return, counting backwards from your current commit. Default is set to 500. It need to be nop-negitive , if input is 0 it will still give the last commit.
453+ maximum number of commit that would return, counting backwards from your current commit. Default is set to 500. It needs to be nop-negative , if input is 0 it will still give the last commit.
407454
408455 Example
409456 -------
@@ -429,70 +476,21 @@ def get_commit_history(self, max_history: int = 500) -> list:
429476 """
430477 if max_history < 0 :
431478 raise ValueError ("max_history needs to be non-negative." )
432- if max_history > 1 :
433- limit_history = max_history - 1
434- else :
435- limit_history = 1
436- woql_query = (
437- WOQLQuery ()
438- .using ("_commits" )
439- .limit (limit_history )
440- .triple ("v:branch" , "name" , WOQLQuery ().string (self .branch ))
441- .triple ("v:branch" , "head" , "v:commit" )
442- .path ("v:commit" , "parent*" , "v:target_commit" )
443- .triple ("v:target_commit" , "identifier" , "v:cid" )
444- .triple ("v:target_commit" , "author" , "v:author" )
445- .triple ("v:target_commit" , "message" , "v:message" )
446- .triple ("v:target_commit" , "timestamp" , "v:timestamp" )
447- )
448- result = self .query (woql_query ).get ("bindings" )
449- if not result :
450- return result
451- else :
452- result_list = []
453- for result_item in result :
454- result_list .append (
455- {
456- "commit" : result_item ["cid" ]["@value" ],
457- "author" : result_item ["author" ]["@value" ],
458- "message" : result_item ["message" ]["@value" ],
459- "timestamp" : datetime .fromtimestamp (
460- int (result_item ["timestamp" ]["@value" ])
461- ),
462- }
463- )
464- return result_list
479+ return self .log (count = max_history )
465480
466481 def _get_current_commit (self ):
467- woql_query = (
468- WOQLQuery ()
469- .using ("_commits" )
470- .triple ("v:branch" , "name" , WOQLQuery ().string (self .branch ))
471- .triple ("v:branch" , "head" , "v:commit" )
472- .triple ("v:commit" , "identifier" , "v:cid" )
473- )
474- result = self .query (woql_query )
475- if not result :
476- return None
477- current_commit = result .get ("bindings" )[0 ].get ("cid" ).get ("@value" )
478- return current_commit
482+ descriptor = self .db
483+ if self .branch :
484+ descriptor = f'{ descriptor } /local/branch/{ self .branch } '
485+ commit = self .log (team = self .team , db = descriptor , count = 1 )[0 ]
486+ return commit ['identifier' ]
479487
480488 def _get_target_commit (self , step ):
481- woql_query = (
482- WOQLQuery ()
483- .using ("_commits" )
484- .path (
485- "v:commit" ,
486- f"parent{{{ step } ,{ step } }}" ,
487- "v:target_commit" ,
488- )
489- .triple ("v:branch" , "name" , WOQLQuery ().string (self .branch ))
490- .triple ("v:branch" , "head" , "v:commit" )
491- .triple ("v:target_commit" , "identifier" , "v:cid" )
492- )
493- result = self .query (woql_query )
494- target_commit = result .get ("bindings" )[0 ].get ("cid" ).get ("@value" )
495- return target_commit
489+ descriptor = self .db
490+ if self .branch :
491+ descriptor = f'{ descriptor } /local/branch/{ self .branch } '
492+ commit = self .log (team = self .team , db = descriptor , count = 1 , start = step )[0 ]
493+ return commit ['identifier' ]
496494
497495 def get_all_branches (self , get_data_version = False ):
498496 """Get all the branches available in the database."""
0 commit comments