importtype{Repo}from".";importtype{Path}from"../path";importRepoCommitfrom"./commit";importloggerfrom"utils/log";/** * A file we know to be in a git repository. */classRepoFile{// the path to the filepath: Path;// the repo this file is inprivaterepo: Repo;constructor(repo: Repo,path: Path){this.repo=repo;this.path=path;}staticcreate(repo: Repo,path: Path){constfile=newRepoFile(repo,path);returnfile;}/** * Get the link to this file in git history at a particular commit * @param longHash the specific hash to link to */// historyLink(longHash: string) {// return this.repo.historyLink(longHash, this.path);// }// is the file ignored by the repo?// TODO this does not work ofcisIgnored(){return[".git","node_modules"].includes(this.path.name);}// get the last commit that cared about this filegetlastLog(){// don't bother if the file is ignoredif(this.isIgnored()){returnnull;}constcommand=`git log -1 --full-history --pretty="format:%h %H %ad %ct" --date default --date=format:'%Y-%m-%d' ${this.path.toString()}`;try{conststdout=this.repo.runCmd(command);if(stdout){const[line]=stdout.split("\n");const[shortHash,longHash,commitDate,timestamp]=line.split(" ");returnRepoCommit.create({
shortHash,
longHash,
commitDate,timestamp: parseInt(timestamp,10),repo: this.repo,});}else{logger.git(`git log command failed on path ${this.path.toString()}. It's likely that no commit history was found for the path.`);returnnull;}}catch(error){throwerror;}}/** * Get the full git log of changes to this file */getlog(){if(this.isIgnored()){return[];}constcommand=`git log --all --full-history --pretty="format:%h %H %ad" --date default --date=format:'%Y-%m-%d' ${this.path.toString()}`;try{conststdout=this.repo.runCmd(command);if(stdout){returnstdout.split("\n").map((line: string)=>{const[shortHash,longHash,commitDate]=line.split(" ");returnRepoCommit.create({
shortHash,
longHash,
commitDate,timestamp: 0,repo: this.repo,});});}else{logger.git(`git log command failed on path ${this.path.toString()}. It's likely that no commit history was found for the path.`);return[];}}catch(error){throwerror;}}/** * Get the last timestamp of this file. */getlastTimestamp(){constcommand=`git log -1 --pretty=format:%ct --follow -- ${this.path.toString()}`;try{conststdout=this.repo.runCmd(command);returnparseInt(stdout,10);}catch(error){throwerror;}}}exportdefaultRepoFile;