From c52a6236307fcb5609ebf911d2ccf18b0daac5ff Mon Sep 17 00:00:00 2001 From: munja Date: Wed, 30 Nov 2022 20:15:10 +0100 Subject: [PATCH] feat: new mf_getgitbranch macro (and test) --- base/mf_getgitbranch.sas | 37 +++++++++++++++++++++++++++++ tests/base/mf_getgitbranch.test.sas | 20 ++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 base/mf_getgitbranch.sas create mode 100644 tests/base/mf_getgitbranch.test.sas diff --git a/base/mf_getgitbranch.sas b/base/mf_getgitbranch.sas new file mode 100644 index 0000000..b46b8c3 --- /dev/null +++ b/base/mf_getgitbranch.sas @@ -0,0 +1,37 @@ +/** + @file + @brief Retrieves the current branch from a local GIT repo + @details In a local git repository, the current branch is always available in + the `.git/HEAD` file in a format like this: `ref: refs/heads/master` + + This macro simply reads the file and returns the last word (eg `master`). + + Example usage: + + %let gitdir=%sysfunc(pathname(work))/core; + %let repo=https://github.com/sasjs/core; + %put source clone rc=%sysfunc(GITFN_CLONE(&repo,&gitdir)); + + %put The current branch is %mf_getgitbranch(&gitdir); + + @param [in] gitdir The directory containing the GIT repository + +

SAS Macros

+ @li mf_readfile.sas + +

Related Macros

+ @li mp_gitadd.sas + @li mp_gitlog.sas + @li mp_gitreleaseinfo.sas + @li mp_gitstatus.sas + + @version 9.2 + @author Allan Bowe +**/ + +%macro mf_getgitbranch(gitdir +)/*/STORE SOURCE*/; + + %scan(%mf_readfile(&gitdir/.git/HEAD),-1) + +%mend mf_getgitbranch; diff --git a/tests/base/mf_getgitbranch.test.sas b/tests/base/mf_getgitbranch.test.sas new file mode 100644 index 0000000..d1225c9 --- /dev/null +++ b/tests/base/mf_getgitbranch.test.sas @@ -0,0 +1,20 @@ +/** + @file + @brief Testing mf_getgitbranch.sas macro + +

SAS Macros

+ @li mf_getgitbranch.sas + @li mp_assert.sas + +**/ + +/* grab core repo */ +%let gitdir=%sysfunc(pathname(work))/core; +%let repo=https://github.com/sasjs/core; +%put source clone rc=%sysfunc(GITFN_CLONE(&repo,&gitdir)); + +%mp_assert( + iftrue=(%mf_getgitbranch(&gitdir)=main), + desc=Checking correct branch was obtained, + outds=work.test_results +)