Skip to content

Instantly share code, notes, and snippets.

@jdbrice
Created July 26, 2019 14:42
Show Gist options
  • Save jdbrice/a287222475fe6d86b302efb475fcfc4b to your computer and use it in GitHub Desktop.
Save jdbrice/a287222475fe6d86b302efb475fcfc4b to your computer and use it in GitHub Desktop.
TAG your ROOT files with your codes git commit so you always know what version of the code produced output
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
// credit : https://stackoverflow.com/users/58008/waqas
// from : https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-output-of-command-within-c-using-posix
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
TString gitcommitstr() {
return TString(exec( "git rev-parse HEAD" ).c_str());
}
void WriteGitCommit( TString name="GitCommit" ){
TNamed n( name, gitcommitstr() );
n.Write();
}
#include "gitcommitstr.C"
// execute from within a git repo
//
void test(){
TFile *f = new TFile ( "out.root", "RECREATE" );
WriteGitCommit();
f->Write();
f->Close();
}
// produces a TNamed in the File with the git commit str saved in the title:
// root -l out.root
// root [0] GitCommit->GetTitle()
// 65a9b65bda32b50e43e3d1037dd7585f9a68648c
// root [1]
// also you can change the name by passing an optional param to WriteGitCommit( <name> )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment