@@ -563,6 +563,108 @@ def get_commit_history(self, max_history: int = 500) -> list:
563563 raise ValueError ("max_history needs to be non-negative." )
564564 return self .log (count = max_history )
565565
566+ def get_document_history (
567+ self ,
568+ doc_id : str ,
569+ team : Optional [str ] = None ,
570+ db : Optional [str ] = None ,
571+ start : int = 0 ,
572+ count : int = 10 ,
573+ created : bool = False ,
574+ updated : bool = False ,
575+ ) -> list :
576+ """Get the commit history for a specific document
577+
578+ Returns the history of changes made to a document, ordered backwards
579+ in time from the most recent change. Only commits where the specified
580+ document was created, modified, or deleted are included.
581+
582+ Parameters
583+ ----------
584+ doc_id : str
585+ The document ID (IRI) to retrieve history for (e.g., "Person/alice")
586+ team : str, optional
587+ The team from which the database is. Defaults to the class property.
588+ db : str, optional
589+ The database. Defaults to the class property.
590+ start : int, optional
591+ Starting index for pagination. Defaults to 0.
592+ count : int, optional
593+ Maximum number of history entries to return. Defaults to 10.
594+ created : bool, optional
595+ If True, return only the creation time. Defaults to False.
596+ updated : bool, optional
597+ If True, return only the last update time. Defaults to False.
598+
599+ Raises
600+ ------
601+ InterfaceError
602+ If the client is not connected to a database
603+ DatabaseError
604+ If the API request fails or document is not found
605+
606+ Returns
607+ -------
608+ list
609+ List of history entry dictionaries containing commit information
610+ for the specified document:
611+ ```
612+ [
613+ {
614+ "author": "admin",
615+ "identifier": "tbn15yq6rw1l4e9bgboyu3vwcoxgri5",
616+ "message": "Updated document",
617+ "timestamp": datetime.datetime(2023, 4, 6, 19, 1, 14, 324928)
618+ },
619+ {
620+ "author": "admin",
621+ "identifier": "3v3naa8jrt8612dg5zryu4vjqwa2w9s",
622+ "message": "Created document",
623+ "timestamp": datetime.datetime(2023, 4, 6, 19, 0, 47, 406387)
624+ }
625+ ]
626+ ```
627+
628+ Example
629+ -------
630+ >>> from terminusdb_client import Client
631+ >>> client = Client("http://127.0.0.1:6363")
632+ >>> client.connect(db="example_db")
633+ >>> history = client.get_document_history("Person/Jane")
634+ >>> print(f"Document modified {len(history)} times")
635+ >>> print(f"Last change by: {history[0]['author']}")
636+ """
637+ self ._check_connection (check_db = (not team or not db ))
638+ team = team if team else self .team
639+ db = db if db else self .db
640+
641+ params = {
642+ 'id' : doc_id ,
643+ 'start' : start ,
644+ 'count' : count ,
645+ }
646+ if created :
647+ params ['created' ] = created
648+ if updated :
649+ params ['updated' ] = updated
650+
651+ result = self ._session .get (
652+ f"{ self .api } /history/{ team } /{ db } " ,
653+ params = params ,
654+ headers = self ._default_headers ,
655+ auth = self ._auth (),
656+ )
657+
658+ history = json .loads (_finish_response (result ))
659+
660+ # Post-process timestamps from Unix timestamp to datetime objects
661+ if isinstance (history , list ):
662+ for entry in history :
663+ if 'timestamp' in entry and isinstance (entry ['timestamp' ], (int , float )):
664+ entry ['timestamp' ] = datetime .fromtimestamp (entry ['timestamp' ])
665+
666+ return history
667+
566668 def _get_current_commit (self ):
567669 descriptor = self .db
568670 if self .branch :
0 commit comments