Macros for SAS Application Developers
https://github.com/sasjs/core
mp_gitlog.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates a dataset with the commit history of a local repository
4  @details Returns the commit history from a local repository. The name of the
5  branch is also returned.
6 
7  More details here:
8 https://documentation.sas.com/doc/ko/pgmsascdc/v_033/lefunctionsref/n1qo5miyvry1nen111js203hlwrh.htm
9 
10  Usage:
11 
12  %let gitdir=%sysfunc(pathname(work))/core;
13  %let repo=https://github.com/sasjs/core;
14  %put source clone rc=%sysfunc(GITFN_CLONE(&repo,&dir));
15 
16  %mp_gitlog(&gitdir,outds=work.mp_gitlog)
17 
18  @param [in] gitdir The directory containing the GIT repository
19  @param [in] filter= (BRANCHONLY) To return only the commits for the current
20  branch, use BRANCHONLY (the default). Anything else will return the entire
21  commit history.
22  @param [out] outds= (work.mp_gitlog) The output dataset to create.
23  All vars are $128 except `message` which is $4000.
24  @li author returns the author who submitted the commit.
25  @li children_ids returns a list of the children commit IDs
26  @li committer returns the name of the committer.
27  @li committer_email returns the email of the committer.
28  @li email returns the email of the commit author.
29  @li id returns the commit ID of the commit object.
30  @li in_current_branch returns "TRUE" or "FALSE" to indicate if the commit is
31  in the current branch.
32  @li message returns the commit message.
33  @li parent_ids returns a list of the parent commit IDs.
34  @li stash returns "TRUE" or "FALSE" to indicate if the commit is a stash
35  commit.
36  @li time returns the time of the commit as numeric string
37  @li commit_time_num time of the commit as numeric SAS datetime
38  @li commit_time_str the commit_time_num variable cast as string
39 
40  @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
41  @param [in] nobs= (0) Set to an integer greater than 0 to restrict the number
42  of rows returned
43 
44  <h4> SAS Macros </h4>
45  @li mf_getgitbranch.sas
46 
47  <h4> Related Files </h4>
48  @li mp_gitadd.sas
49  @li mp_gitreleaseinfo.sas
50  @li mp_gitstatus.sas
51 
52 **/
53 
54 %macro mp_gitlog(gitdir,outds=work.mp_gitlog,mdebug=0,filter=BRANCHONLY,nobs=0);
55 
56 %local varlist i var;
57 %let varlist=author children_ids committer committer_email email id
58  in_current_branch parent_ids stash time ;
59 
60 data &outds;
61  LENGTH gitdir branch $ 1024 message $4000 &varlist $128 commit_time_num 8.
62  commit_time_str $32;
63  call missing (of _all_);
64  branch="%mf_getgitbranch(&gitdir)";
65  gitdir=symget('gitdir');
66  rc=git_status_free(trim(gitdir));
67  if rc=-1 then do;
68  put "The libgit2 library is unavailable and no Git operations can be used.";
69  put "See: https://stackoverflow.com/questions/74082874";
70  stop;
71  end;
72  else if rc=-2 then do;
73  put "The libgit2 library is available, but the status function failed.";
74  put "See the log for details.";
75  stop;
76  end;
77  entries=git_commit_log(trim(gitdir));
78  do n=1 to entries;
79 
80  %do i=1 %to %sysfunc(countw(&varlist message));
81  %let var=%scan(&varlist message,&i,%str( ));
82  rc=git_commit_get(n,trim(gitdir),"&var",&var);
83  %end;
84  /* convert unix time to SAS time - https://4gl.uk/corelink0 */
85  /* Number of seconds between 01JAN1960 and 01JAN1970: 315619200 */
86  format commit_time_num datetime19.;
87  commit_time_num=sum(input(cats(time),best.),315619200);
88  commit_time_str=put(commit_time_num,datetime19.);
89  %if &mdebug=1 %then %do;
90  putlog (_all_)(=);
91  %end;
92  if "&filter"="BRANCHONLY" then do;
93  if cats(in_current_branch)='TRUE' then output;
94  end;
95  else output;
96  %if &nobs>0 %then %do;
97  if n ge &nobs then stop;
98  %end;
99  end;
100  rc=git_commit_free(trim(gitdir));
101  keep gitdir branch &varlist message time commit_time_num commit_time_str;
102 run;
103 
104 %mend mp_gitlog;