Published on :
System Utility CREATION_INTERNE

Macro mp_gitstatus: Retrieving the status of a Git repository

This code is also available in: Deutsch Español Français
Awaiting validation
The macro uses native SAS© Git interface functions (`git_status`, `git_status_get`) to get the number of modified files in a target directory. It then iterates over these files to extract their path, their state (staged), and their status (modified, new, deleted), also handling error cases related to the libgit2 library.
Data Analysis

Type : CREATION_INTERNE


Data is generated directly by calls to SAS Git system functions from the provided directory path.

1 Code Block
DATA STEP Data
Explanation :
Initializes the output table, checks Git availability via `git_status`, and loops through detected files to extract their metadata via `git_status_get`.
Copied!
1DATA &outds;
2 LENGTH gitdir path $ 1024 STATUS $ 64 STAGED $ 32;
3 call missing (of _all_);
4 gitdir=symget('gitdir');
5 cnt=git_status(trim(gitdir));
6 IF cnt=-1 THEN DO;
7 put "The libgit2 library is unavailable...";
8 END;
9 ELSE IF cnt=-2 THEN DO;
10 put "The libgit2 library is available, but...";
11 END;
12 ELSE DO n=1 to cnt;
13 rc=GIT_STATUS_GET(n,gitdir,'PATH',path);
14 rc=GIT_STATUS_GET(n,gitdir,'STAGED',staged);
15 rc=GIT_STATUS_GET(n,gitdir,'STATUS',STATUS);
16 OUTPUT;
17 /* Debug log si activé */
18 END;
19 rc=git_status_free(trim(gitdir));
20 drop rc cnt;
21RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« Integrating Git directly into the SAS DATA step marks a significant shift toward modern DevOps for Data Engineering. By utilizing native libgit2 functions rather than external command-line calls, this macro allows for a seamless, secure, and programmatic way to audit your codebase's state without leaving the SAS environment.

Technical Pillars of this Integration:
Native Interface Efficiency: The use of git_status and git_status_get ensures high-performance interaction with the Git index. Unlike X commands, these functions return structured data directly into the Program Data Vector (PDV), allowing for immediate logical branching based on whether files are New, Modified, or Deleted.

Granular Metadata Extraction: By iterating through the repository's status count, the macro captures the crucial "Staged" flag. This is vital for automation scripts that need to distinguish between files simply edited on disk and those specifically marked for the next commit.

Resource Management & Stability: A hallmark of professional SAS programming is the inclusion of git_status_free. Freeing the memory allocated by the Git functions prevents memory leaks and file locks, ensuring that long-running server sessions remain stable even after multiple repository checks.

Error Handling and Diagnostics: The logic specifically traps libgit2 availability codes (-1, -2). This is essential for cross-platform deployments where the Git plugin might not be configured identically across Development, Test, and Production environments. »