I would like to be able to check what branch is checked out ("main" / "master" vs. "dev", "development" etc.).
If it's the "main" / "master", run the production file locations, servers, etc.
If it's "dev" / "development", run the development file locations, servers, etc.
I know in git, I can use the following:
git rev-parse --abbrev-ref HEAD
In python, there's a git library where I can easily grab the active branch:
from git import Repo repo = "C:\Users\Project Folder" branch = repo.active_branch
I checked the SAS documentation, but didn't see any function that checked the active branch.
SAS Help Center: Using Git Functions in SAS
I am using SAS 9.4 M6.
Is there a way to check the active branch in SAS?
I ended up running the command prompt to get the desired output.
/* Get local directory*/ %let rc = %sysfunc(filename(fr,.)); %let curdir = %sysfunc(pathname(&fr)); %put &curdir; /* Run git command via command prompt */ DATA _null_; call system("cd /d &curdir & git rev-parse --abbrev-ref HEAD > &curdir\branch_name.txt & exit"); RUN; /* Import text file with branch name */ PROC IMPORT FILE="&curdir\branch_name.txt" out = work.branch dbms=dlm replace; getnames=no; RUN; /* Create macro variable holding branch name */ DATA _null_; set work.branch end=last_obs; if last_obs then call symput("branch_name", VAR1); RUN; %PUT &branch_name;
Hello @narnia649
I am not aware of such a function.
Rather you can checkout the branch desired. using gitfn_co_branch (https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p1evluypahwf0un1w7u74rd26n5n.h...)
This should work with SAS 9.4M6.
I ended up running the command prompt to get the desired output.
/* Get local directory*/ %let rc = %sysfunc(filename(fr,.)); %let curdir = %sysfunc(pathname(&fr)); %put &curdir; /* Run git command via command prompt */ DATA _null_; call system("cd /d &curdir & git rev-parse --abbrev-ref HEAD > &curdir\branch_name.txt & exit"); RUN; /* Import text file with branch name */ PROC IMPORT FILE="&curdir\branch_name.txt" out = work.branch dbms=dlm replace; getnames=no; RUN; /* Create macro variable holding branch name */ DATA _null_; set work.branch end=last_obs; if last_obs then call symput("branch_name", VAR1); RUN; %PUT &branch_name;
Thanks @narnia649 for sharing your code. This will hopefully help many others in the days to come.
I believe the accepted solution only works from BASE SAS. In EG, this spawns a process executed on the EG Server. That server would have no way to process and end user session local repo. Right?
If the local location is accessible from the serve, then it would work otherwise no.
Thanks for the reply!
There are two issues with the solution:
If EG had a GITFN function that would return the currently checked out branch, that would be a good alternative.
Are you able to get the code shown in the solution to run in EG?
Thank you!
I am afraid I don't know much about your environment.
However these are some general observation about how git is implemented.
1. In a typical corporate environment, by design, the production server is mapped to the prod branch, the QA, branch to the QA server, and Dev branch to the dev server. each of these servers has git installed.
2.In my experience in corporate scenarios a commercial distribution of Git such Bitbucket, Github etc are used which have their own GUI's for managing Devops process
3.There are set of rules about what a user can do and not do.
4.Generally there is a Devops Team that manages the process and Git is a part of the Devops process.
And I don't see a need for what you are looking for.
Thank you, I'm quite familiar with the corporate implementation and we have much of what you describe. The requirement is for a sas program to conditionally execute when called depending on the currently checked out branch. If the current branch is master, then proceed, if not, stop execution. In either case, write to the log the name of the currently checked out branch. This is simple if code is executed from BASE, but I am looking for a means to at least ascertain the name of the current branch from EG. Installing GIT (we do not use GitHub for obvious reasons) on the EG server may work if it can resolve the UNC path. Mapped drives on the server are not an option. Thanks. I'll open a Track with SAS Support.
Git needs to be installed here.
The target location should be available for the code to run on. If this is on a remote location then it should be mapped to the server.
May be SAS can help, otherwise I don't see any easier way to do this.
It's not a SAS GIT function but it is a SAS Macro Function: https://core.sasjs.io/mf__getgitbranch_8sas.html
This reads in the contents of the HEAD file in the .git folder, which - unless your repo is in "Detached Head State" - will always contain a link to the current ref (branch) of the repo.
Use as follows:
%put my branch is %mf_getgitbranch(/path/to/git/repo);
No XCMD or fancy tricks needed. Under the hood it just runs;
%scan(%mf_readfile(&gitdir/.git/HEAD),-1)
Thanks very much for this post Allan! I have been conditioned to "DO NOT LOOK IN .GIT"... Simply reading the HEAD never occurred to me. Exactly what I was looking for as an end result. Great content on the CORE site!!
Perry
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.