BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
narnia649
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
narnia649
Obsidian | Level 7

 

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;

 

View solution in original post

12 REPLIES 12
Sajid01
Meteorite | Level 14

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.

narnia649
Obsidian | Level 7
Thanks, unfortunately, checking out the branch is not the functionality that I was looking for.
narnia649
Obsidian | Level 7

 

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;

 

Sajid01
Meteorite | Level 14

Thanks @narnia649  for sharing your code. This will hopefully  help many others in the days to come.

PerryNoble
Fluorite | Level 6

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?

Sajid01
Meteorite | Level 14

If the local location is accessible from the serve, then it would work otherwise no.

PerryNoble
Fluorite | Level 6

Thanks for the reply! 

 

There are two issues with the solution:

  • When the EG session spawns the system process, it is from the perspective of the EG server. That said, the server does not have GIT installed
  • The server has permissions to the location but does not have a mapped drive. It can only access via UNC path (Windows implementation)

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!

Sajid01
Meteorite | Level 14

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.

PerryNoble
Fluorite | Level 6

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.

Sajid01
Meteorite | Level 14

 

  • When the EG session spawns the system process, it is from the perspective of the EG server. That said, the server does not have GIT installed :

            Git needs to be installed here.

 

  • The server has permissions to the location but does not have a mapped drive. It can only access via UNC path (Windows implementation)

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.

AllanBowe
Barite | Level 11

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)
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
PerryNoble
Fluorite | Level 6

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1385 views
  • 3 likes
  • 4 in conversation