gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 8d5a124 2/3: Store Git's describe in outputs u


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 8d5a124 2/3: Store Git's describe in outputs using libgit2
Date: Sun, 9 Oct 2016 14:55:57 +0000 (UTC)

branch: master
commit 8d5a124c607766dd1774d1a4c3616e416f6a3502
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Store Git's describe in outputs using libgit2
    
    One of the main reasons to build Gnuastro was to help/encourage
    reproducibility of scientific results. When the scientific research is
    defined in a version controlled system, keeping the Git commit information
    that produced the data-products would be very helpful. For one example of a
    pipeline for a research/paper see NoiseChisel paper's reproduction pipeline
    at https://gitlab.com/makhlaghi/NoiseChisel-paper . Such systems can be
    implemented in larger and smaller scales and can be very helpful/necessary
    for "scientific" (reproducible) results.
    
    Since we want to encourage such reproducible research results, this job is
    automatically done within Gnuastro now. When Gnuastro's programs, or the
    `gal_fits_write_keys_version' library function, are run within a Git
    version controlled directory (and libgit2 is installed), they store the
    commit's description in any Gnuastro output product (FITS file or ASCII
    table). See the manual for more details on the format of the returned
    value.
    
    Fortunately Git comes with the libgit2 library. So we don't need to make
    system calls, this library will get the commit information if Gnuastro's
    tools are run within a Git version controlled directory. Libgit2 is an
    optional dependency and its presence is checked at configure time. So there
    is no problem if its not available.
    
    The common FITS keywords were also organized to separte the program
    specific keywords from the versioning keywords. Also the "GNU Astronomy
    Utilities ..." FITS `COMMENT' was removed and the Gnuastro webpage was also
    removed from the FITS output to emphasize more on the more important
    keywords).
    
    User visible library changes:
    
     - `gal_fits_write_keys_version' will write the commit description when
       there is a Git repository and libgit2 is installed.
    
     - New `gnuastro/git.h' header with `gal_git_describe' function.
    
     - New `GAL_GNUASTRO_HAVE_LIBGIT2' macro in `gnuastro/gnuastro.h'.
    
    This finishes task #14172.
---
 bin/mkcatalog/mkcatalog.c             |    6 ++
 bin/mkprof/mkprof.c                   |   10 ++-
 configure.ac                          |   21 ++++-
 doc/gnuastro.texi                     |  145 ++++++++++++++++++++++++++++-----
 lib/Makefile.am                       |   20 +++--
 lib/fits.c                            |   39 ++++++---
 lib/git.c                             |   78 ++++++++++++++++++
 lib/gnuastro.h.in                     |    1 +
 lib/{gnuastro.h.in => gnuastro/git.h} |   27 +++---
 9 files changed, 288 insertions(+), 59 deletions(-)

diff --git a/bin/mkcatalog/mkcatalog.c b/bin/mkcatalog/mkcatalog.c
index 100170c..b2455e7 100644
--- a/bin/mkcatalog/mkcatalog.c
+++ b/bin/mkcatalog/mkcatalog.c
@@ -30,6 +30,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <string.h>
 #include <stdlib.h>
 
+#include <gnuastro/git.h>
 #include <gnuastro/wcs.h>
 #include <gnuastro/fits.h>
 #include <gnuastro/txtarray.h>
@@ -448,6 +449,11 @@ makeoutput(struct mkcatalogparams *p)
       sprintf(comment, "# %s %s catalog.\n", SPACK_STRING, p->name);
       sprintf(p->line, "# %s started on %s", SPACK_NAME, ctime(&p->rawtime));
       strcat(comment, p->line);
+      if(gal_git_describe())
+        {
+          sprintf(p->line, "# From commit %s\n", gal_git_describe());
+          strcat(comment, p->line);
+        }
 
 
       /* Write the input files: */
diff --git a/bin/mkprof/mkprof.c b/bin/mkprof/mkprof.c
index a1c9b17..38a09ec 100644
--- a/bin/mkprof/mkprof.c
+++ b/bin/mkprof/mkprof.c
@@ -30,6 +30,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <stdlib.h>
 
 #include <gnuastro/box.h>
+#include <gnuastro/git.h>
 #include <gnuastro/fits.h>
 #include <gnuastro/array.h>
 #include <gnuastro/threads.h>
@@ -377,12 +378,15 @@ build(void *inparam)
 void
 writelog(struct mkprofparams *p)
 {
-  char comments[1000];
   int space[]={6, 10, 15}, prec[]={3, 6};
   int int_cols[]={0, 2, 4, -1}, accu_cols[]={-1};
+  char comments[1000], gitdescribe[100], *gd=gal_git_describe();
+
+  if(gd) sprintf(gitdescribe, " from %s,", gd);
+  else   gitdescribe[0]='\0';
 
   sprintf(comments, "# Log file for "SPACK_STRING".\n"
-          "# Run on %s"
+          "# Created%s on %s"
           "# Column 0: Row number in catalog (starting from zero).\n"
           "# Column 1: Overlap magnitude with final image "
           "(zeropoint: %.3f).\n"
@@ -390,7 +394,7 @@ writelog(struct mkprofparams *p)
           "# Column 3: Fraction of brightness in Monte Carlo "
           "integrated pixels.\n"
           "# Column 4: An individual image was created.\n",
-          ctime(&p->rawtime), p->zeropoint);
+          ctime(&p->rawtime), gitdescribe, p->zeropoint);
 
 
   gal_txtarray_array_to_txt(p->log, p->cs0, LOGNUMCOLS, comments, int_cols,
diff --git a/configure.ac b/configure.ac
index d38fae2..5ce79b2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -201,6 +201,18 @@ AM_CONDITIONAL([COND_HASLIBJPEG], [test "x$has_libjpeg" = 
"xyes"])
 
 
 
+# Check libgit2:
+AC_SEARCH_LIBS([git_libgit2_init], [git2],
+               [has_libgit2=1], [has_libgit2=0])
+AC_DEFINE_UNQUOTED([GAL_GNUASTRO_HAVE_LIBGIT2], [$has_libgit2],
+                   [libgit2 is installed on the system])
+AS_IF([test "x$has_libgit2" = "x1"], [], [anywarnings=yes])
+AC_SUBST(HAVE_LIBGIT2, [$has_libgit2])
+
+
+
+
+
 # Check Ghostscript: "-dPDFFitPage" option to Ghostscript, used by
 # ConvertType to convert from EPS to PDF, has been introduced in
 # Ghostscript 9.10.  Make sure we have at least that version.
@@ -496,7 +508,14 @@ AS_IF([test x$enable_guide_message = xyes],
         AS_IF([test "x$has_libjpeg" = "xno"],
               [AS_ECHO(["  - libjpeg, could not be linked with in your library 
search path."])
                AS_ECHO(["    If JPEG outputs are desired from ConvertType, it 
will warn"])
-               AS_ECHO(["    you and abort with an error."])])
+               AS_ECHO(["    you and abort with an error."])
+               AS_ECHO([]) ])
+        AS_IF([test "x$has_libgit2" = "x0"],
+              [AS_ECHO(["  - libgit2, could not be linked with in your library 
search path."])
+               AS_ECHO(["    When present, Git's describe output will be 
stored in the"])
+               AS_ECHO(["    output files if Gnuastro's programs were called 
within a Git"])
+               AS_ECHO(["    version controlled directory to help in 
reproducibility."])
+               AS_ECHO([]) ])
         AS_IF([test "x$has_ghostscript" = "xno"],
               [AS_ECHO(["  - GPL GhostScript version 9.10 or later, with the 
executable"])
                AS_ECHO(["    name \`gs', was not found in your PATH 
environment variable."])
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 6ba6ea8..df77b52 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -512,6 +512,7 @@ Gnuastro library
 * Array manipulation::          Functions for manipulating arrays.
 * Bounding box::                Finding the bounding box.
 * FITS files::                  Working with FITS datat.
+* Git wrappers::                Wrappers for functions in libgit2.
 * Linked lists::                Various types of linked lists.
 * Mesh grid for an image::      Breaking an image into a grid.
 * Polygons::                    Working with the vertices of a polygon.
@@ -2468,6 +2469,16 @@ take effect.
 
 @table @asis
 
address@hidden libgit2
address@hidden Git
address@hidden libgit2
address@hidden Version control systems
+Git is one of the most common version control systems (see @ref{Version
+controlled source}). When @file{libgit2} is present, all Gnuastro's outputs
+will contain the version number of the working directory's repository for
+future reproducibility. See the @command{COMMIT} keyword header in
address@hidden headers} for a discussion.
+
 @item libjpeg
 @pindex libjpeg
 @cindex JPEG format
@@ -5225,24 +5236,74 @@ list. We have other mailing lists and tools for those 
purposes, see
 
 @cindex Output FITS headers
 @cindex CFITSIO version on outputs
-The output FITS files created by Gnuastro will have the following two
-keywords: @command{DATE}, @command{CFITSIO}, @command{WCSLIB} and
address@hidden The first specifies the time in UT of the file
-being created. The next three specify the versions of CFITSIO, WCSLIB
-and Gnuastro that was used to make the file. Note that WCSLIB has only
-recently added the version reporting capability. If you version of
-WCSLIB doesn't have this capability, it will not be reported. Some
-basic information about Gnuastro is also printed. The example below
-shows the last few keywords of one of the outputs of ImageCrop.
+The output FITS files created by Gnuastro's programs have some or all of
+the following standard keywords to keep the basic date and version
+information of Gnuastro, its dependencies and the pipeline that is using
+Gnuastro.
+
address@hidden @command
+
address@hidden DATE
+The creation time of the FITS file. This date is written directly by
+CFITSIO and is in UT format.
+
address@hidden COMMIT
+The commit description (output of address@hidden git describe}') from the
+directory where you called any of Gnuastro's programs. If the directory is
+not version controlled or you don't have @file{libgit2} installed (see
address@hidden dependencies}) then this key will not be present.
+
+At first sight, version control appears to be mainly a tool for software
+developers. However, a research's progress is almost identical to software
+development: first you have a rough idea that starts with handful of
+steps. But as the first results appear to be promising, you will have to
+extend it to make it more robust and work in all the situations your
+research covers, not just your first test samples. Slowly you will find
+wrong assumptions or bad implementations that need to be fixed (`bugs' in
+software development parlance). Finally, when you submit the research to
+your collaborators or a journal, many comments and suggestions will come in
+that you have to addressed.
+
+Software developers have created version control systems precisely for this
+kind of activity. Each significant moment in the project's history is given
+a ``version''. So you can be sure that your work is reproducible and track
+the progress and history, see @ref{Version controlled source}. With version
+control, you can easily experiment a new method and revert back if it fails
+and many other things. One important feature is that you stamp the research
+result (FITS image, table, report or paper) with the version number of your
+research processing address@hidden example see the NoiseChisel
+paper's @url{https://gitlab.com/makhlaghi/NoiseChisel-paper, reproduction
+pipeline}.}. This will enable you to exactly reproduce that same result
+later, even if you have made changes/progress.
+
+If the running directory contains non-committed work, then the commit
+description stored in this keyword will have a address@hidden'
+suffix. This can be very helpful to let you know that the data is not ready
+to be shared with collaborators or journal. You should only share results
+that are produced after all your work is committed (safely stored in the
+version controlled history).
+
address@hidden CFITSIO
+The version of CFITSIO used (see @ref{CFITSIO}).
+
address@hidden WCSLIB
+The version of WCSLIB used (see @ref{WCSLIB}). Note that older versions of
+WCSLIB do not report the version internally. So this is only available if
+you are using more recent WCSLIB versions.
+
address@hidden GNUASTRO
+The version of Gnuastro used (see @ref{Version numbering}).
address@hidden table
+
+Here is one example of the last few lines of an example output.
 
 @example
-              / ImageCrop (GNU Astronomy Utilities 0.1) 0.1:
-DATE    = ' ... '              / file creation date ( ... )
-CFITSIO = '3.37    '           / CFITSIO version.
-WCSLIB  = '5.5     '           / WCSLIB version.
-GNUASTRO= '0.1     '           / GNU Astronomy Utilities version.
-COMMENT GNU Astronomy Utilities 0.1
-COMMENT http://www.gnu.org/software/gnuastro/
+              / Versions and date
+DATE    = '...'                / file creation date
+COMMIT  = 'v0-8-g547f6eb'      / Commit description in running dir.
+CFITSIO = '3.39    '           / CFITSIO version.
+WCSLIB  = '5.15    '           / WCSLIB version.
+GNUASTRO= '0.3'                / GNU Astronomy Utilities version.
 END
 @end example
 
@@ -5257,6 +5318,8 @@ END
 
 
 
+
+
 @node Extensions and Tables, Image manipulation, Common program behavior, Top
 @chapter Extensions and Tables
 
@@ -14955,6 +15018,7 @@ problems. It will stabilize with the removal of this 
notice. Check the
 * Array manipulation::          Functions for manipulating arrays.
 * Bounding box::                Finding the bounding box.
 * FITS files::                  Working with FITS datat.
+* Git wrappers::                Wrappers for functions in libgit2.
 * Linked lists::                Various types of linked lists.
 * Mesh grid for an image::      Breaking an image into a grid.
 * Polygons::                    Working with the vertices of a polygon.
@@ -14996,7 +15060,16 @@ char *gnuastro_version=GAL_GNUASTRO_VERSION;
 @end deffn
 
 
address@hidden Macro GAL_GNUASTRO_PTHREAD_BARRIER
address@hidden Macro GAL_GNUASTRO_HAVE_LIBGIT2
+Libgit2 is an optional dependency of Gnuastro (see @ref{Optional
+dependencies}). When it is installed and detected at configure time, this
+macro will have a value of @code{1}. Otherwise, it will have a value of
address@hidden Gnuastro also comes with some wrappers to make it easier to use
+libgit2 (see @ref{Git wrappers}).
address@hidden deffn
+
+
address@hidden Macro GAL_GNUASTRO_HAVE_PTHREAD_BARRIER
 The POSIX threads standard define barriers as an optional
 requirement. Therefore, some operating systems choose to not include
 it. While installing Gnuastro, we check if your system has this construct
@@ -15216,7 +15289,7 @@ last pixels of the overlap will be put in 
@code{fpixel_o} and
 @code{lpixel_o}.
 @end deftypefun
 
address@hidden FITS files, Linked lists, Bounding box, Gnuastro library
address@hidden FITS files, Git wrappers, Bounding box, Gnuastro library
 @subsection FITS files (@file{fits.h})
 
 @cindex FITS
@@ -15701,7 +15774,41 @@ error notice.
 @address@hidden deftypefun
 
 
address@hidden Linked lists, Mesh grid for an image, FITS files, Gnuastro 
library
address@hidden Git wrappers, Linked lists, FITS files, Gnuastro library
address@hidden Git wrappers (@file{git.h})
+
address@hidden Git
address@hidden libgit2
+Git is one of the most commont tools for version control and it can often
+be useful during development, for example see @code{COMMIT} keyword in
address@hidden headers}. The functions introduced here are described in the
address@hidden/git.h} header. At installation time, Gnuastro will also
+check for the existance of libgit2 and store the value in the
address@hidden, see @ref{Installation
+information}. @file{gnuastro/git.h} includes @file{gnuastro/gnuastro.h}
+internally, so won't have to include both for this macro.
+
address@hidden {char *} gal_git_describe ( )
+When libgit2 is present and the program is called within a directory that
+is version controlled, then this function will return a string containing
+the commit description (similar to Gnuastro's unofficial version number,
+see @ref{Version numbering}). If there are uncommitted changes in the
+running directory, it will add a address@hidden' prefix to the
+description. When there is no tagged point in the previous commit, this
+function will return a uniquely abbreviated commit object as
+fallback. Similar to running the following command:
+
address@hidden
+$ git describe --dirty --always
address@hidden example
+
+Space for the output string is allocated within this function, so after
+using the value you have to @code{free} the output string. If libgit2 is
+not installed or the program calling this function is not within a version
+controlled directory, then the output will be the @code{NULL} pointer.
address@hidden deftypefun
+
address@hidden Linked lists, Mesh grid for an image, Git wrappers, Gnuastro 
library
 @subsection Linked lists (@file{linkedlist.h})
 
 @cindex Array
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 6a8ac99..cfe48c7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -31,19 +31,20 @@ libgnuastro_la_LDFLAGS = -version-info $(GAL_LT_VERSION)
 
 
 # Specify the library .c files
-libgnuastro_la_SOURCES = array.c box.c checkset.c configfiles.c fits.c \
-  linkedlist.c mesh.c mode.c polygon.c qsort.c spatialconvolve.c       \
+libgnuastro_la_SOURCES = array.c box.c checkset.c configfiles.c fits.c  \
+  git.c linkedlist.c mesh.c mode.c polygon.c qsort.c spatialconvolve.c  \
   statistics.c threads.c timing.c txtarray.c wcs.c
 
 
 # Installed headers, note that we are not blindly including all `.h' files
 # in the $(headersdir) directory. Some of the header files don't need to be
 # installed.
-pkginclude_HEADERS = gnuastro/gnuastro.h $(headersdir)/array.h          \
-  $(headersdir)/box.h $(headersdir)/fits.h $(headersdir)/linkedlist.h   \
-  $(headersdir)/mesh.h $(headersdir)/polygon.h $(headersdir)/qsort.h    \
-  $(headersdir)/spatialconvolve.h $(headersdir)/statistics.h            \
-  $(headersdir)/threads.h $(headersdir)/wcs.h $(headersdir)/txtarray.h
+pkginclude_HEADERS = gnuastro/gnuastro.h $(headersdir)/array.h            \
+  $(headersdir)/box.h $(headersdir)/fits.h $(headersdir)/git.h            \
+  $(headersdir)/linkedlist.h $(headersdir)/mesh.h $(headersdir)/polygon.h \
+  $(headersdir)/qsort.h $(headersdir)/spatialconvolve.h                   \
+  $(headersdir)/statistics.h $(headersdir)/threads.h $(headersdir)/wcs.h  \
+  $(headersdir)/txtarray.h
 
 
 # Files to distribute in the tarball. These are internal headers and don't
@@ -69,8 +70,9 @@ CLEANFILES = gnuastro.pc gnuastro/gnuastro.h
 gnuastro/gnuastro.h: Makefile gnuastro.h.in
        rm -f $@ address@hidden
        $(MKDIR_P) gnuastro
-       $(SED) -e 's|@address@hidden|$(VERSION)|g'                          \
-              -e 's|@address@hidden|$(HAS_PTHREAD_BARRIER)|g'  \
+       $(SED) -e 's|@address@hidden|$(VERSION)|g'                            \
+              -e 's|@address@hidden|$(HAVE_LIBGIT2)|g'                  \
+              -e 's|@address@hidden|$(HAVE_PTHREAD_BARRIER)|g'  \
                $(srcdir)/gnuastro.h.in >> address@hidden
        chmod a-w address@hidden
        mv address@hidden $@
diff --git a/lib/fits.c b/lib/fits.c
index c7feb13..730b7bb 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -30,6 +30,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <string.h>
 #include <unistd.h>
 
+#include <gnuastro/git.h>
 #include <gnuastro/fits.h>
 
 #include "checkset.h"
@@ -1437,6 +1438,7 @@ gal_fits_write_keys_version(fitsfile *fptr, struct 
gal_fits_key_ll *headers,
 {
   size_t i;
   int status=0;
+  char *gitdescribe;
   char cfitsioversion[20];
   char startblank[]="              / ";
   char *cp, *cpf, blankrec[80], titlerec[80];
@@ -1457,17 +1459,24 @@ gal_fits_write_keys_version(fitsfile *fptr, struct 
gal_fits_key_ll *headers,
   titlerec[79]='\0';
   cp=blankrec; do *cp=' '; while(++cp<cpf);
 
-  /*Print the starting information for the header.  */
+  /* If any header keywords are specified add them: */
+  if(headers)
+    {
+      fits_write_record(fptr, blankrec, &status);
+      sprintf(titlerec, "%s%s", startblank, spack_string);
+      for(i=strlen(titlerec);i<79;++i) titlerec[i]=' ';
+      fits_write_record(fptr, titlerec, &status);
+      gal_fits_update_keys(fptr, &headers);
+    }
+
+
+  /* Start printing the version information */
   fits_write_record(fptr, blankrec, &status);
-  sprintf(titlerec, "%s%s:", startblank, spack_string);
+  sprintf(titlerec, "%sVersions and date", startblank);
   for(i=strlen(titlerec);i<79;++i) titlerec[i]=' ';
   fits_write_record(fptr, titlerec, &status);
   gal_fits_io_error(status, NULL);
 
-  /* If any header keywords are specified add them: */
-  if(headers)
-    gal_fits_update_keys(fptr, &headers);
-
   /* Set the version of CFITSIO as a string. */
   sprintf(cfitsioversion, "%-.2f", CFITSIO_VERSION);
 
@@ -1489,12 +1498,18 @@ gal_fits_write_keys_version(fitsfile *fptr, struct 
gal_fits_key_ll *headers,
   /* Write the Gnuastro version. */
   fits_update_key(fptr, TSTRING, "GNUASTRO", PACKAGE_VERSION,
                   "GNU Astronomy Utilities version.", &status);
-  fits_write_comment(fptr, PACKAGE_STRING, &status);
-  fits_write_comment(fptr, PACKAGE_URL, &status);
-  /*
-  fits_write_comment(fptr, GAL_STRINGS_SHORT_COPYRIGHT, &status);
-  fits_write_comment(fptr, GAL_STRINGS_SHORT_LICENSE, &status);
-  */
+
+  /* If we are in a version controlled directory and have libgit2
+     installed, write the commit description into the FITS file. */
+  gitdescribe=gal_git_describe();
+  if(gitdescribe)
+    {
+      fits_update_key(fptr, TSTRING, "COMMIT", gitdescribe,
+                      "Git's commit description in running dir.", &status);
+      free(gitdescribe);
+    }
+
+  /* Report any error if a problem came up */
   gal_fits_io_error(status, NULL);
 }
 
diff --git a/lib/git.c b/lib/git.c
new file mode 100644
index 0000000..a9babdd
--- /dev/null
+++ b/lib/git.c
@@ -0,0 +1,78 @@
+/*********************************************************************
+Functions to deal with Git version controlled directories.
+This is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2016, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#include <config.h>
+
+#include <errno.h>
+#include <error.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <gnuastro/git.h>
+
+/* Return the result of Git describe and  */
+char *
+gal_git_describe(void)
+{
+  char *describe_return=NULL;
+
+/* Only actually work on Git if the library is present. */
+#if GAL_GNUASTRO_HAVE_LIBGIT2 == 1
+  git_buf buf={0};
+  git_repository *repo;
+  git_describe_result *describe_result;
+  git_describe_format_options format_options;
+  git_describe_options describe_options = GIT_DESCRIBE_OPTIONS_INIT;
+
+  /* Initialize Git's global setup. */
+  git_libgit2_init();
+
+  /* Initialize the descriptions. */
+  git_describe_init_format_options(&format_options,
+                                  GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
+
+  /* Set some values manually: */
+  describe_options.show_commit_oid_as_fallback=1;
+  format_options.dirty_suffix="-dirty";
+
+  /* Open the Git repository. */
+  if( !git_repository_open_ext(&repo, ".", 0, NULL)
+      && !git_describe_workdir(&describe_result, repo, &describe_options)
+      && !git_describe_format(&buf, describe_result, &format_options) )
+    {
+      /* Allocate space for and copy the description. */
+      errno=0;
+      describe_return=malloc(strlen(buf.ptr)+1);
+      if(describe_return==NULL)
+        error(EXIT_FAILURE, errno, "%lu bytes to copy Git's describe",
+              strlen(buf.ptr)+1);
+      strcpy(describe_return, buf.ptr);
+    }
+
+  /* Clean up. */
+  git_repository_free(repo);
+  git_libgit2_shutdown();
+#endif
+
+  /* Return */
+  return describe_return;
+}
diff --git a/lib/gnuastro.h.in b/lib/gnuastro.h.in
index 17bd25d..c7c6e31 100644
--- a/lib/gnuastro.h.in
+++ b/lib/gnuastro.h.in
@@ -30,6 +30,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 /* Macros. In gnuastro.h.in, the values in `@' --- `@'', will be replaced
    with installation specific values. */
 #define GAL_GNUASTRO_VERSION "@VERSION@"
+#define GAL_GNUASTRO_HAVE_LIBGIT2 @HAVE_LIBGIT2@
 #define GAL_GNUASTRO_HAVE_PTHREAD_BARRIER @HAVE_PTHREAD_BARRIER@
 
 
diff --git a/lib/gnuastro.h.in b/lib/gnuastro/git.h
similarity index 69%
copy from lib/gnuastro.h.in
copy to lib/gnuastro/git.h
index 17bd25d..9650701 100644
--- a/lib/gnuastro.h.in
+++ b/lib/gnuastro/git.h
@@ -1,10 +1,11 @@
 /*********************************************************************
-Functions dealing with general aspects of all Gnuastro.
+Functions to facilitate using the Git library.
+This is part of GNU Astronomy Utilities (Gnuastro) package.
 
 Original author:
      Mohammad Akhlaghi <address@hidden>
 Contributing author(s):
-Copyright (C) 2016, Free Software Foundation, Inc.
+Copyright (C) 2015, Free Software Foundation, Inc.
 
 Gnuastro is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
@@ -19,20 +20,13 @@ General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/
-#ifndef __GAL_GNUASTRO_H__
-#define __GAL_GNUASTRO_H__
-
-/* Include other headers if necessary here. Note that other header files
-   must be included before the C++ preparations below */
-
-
-
-/* Macros. In gnuastro.h.in, the values in `@' --- `@'', will be replaced
-   with installation specific values. */
-#define GAL_GNUASTRO_VERSION "@VERSION@"
-#define GAL_GNUASTRO_HAVE_PTHREAD_BARRIER @HAVE_PTHREAD_BARRIER@
+#ifndef __GAL_GIT_H__
+#define __GAL_GIT_H__
 
 
+#if GAL_GNUASTRO_HAVE_LIBGIT2 == 1
+#include <git2.h>
+#endif
 
 /* C++ Preparations */
 #undef __BEGIN_C_DECLS
@@ -53,9 +47,12 @@ __BEGIN_C_DECLS  /* From C++ preparations */
 
 
 
+/* Only make the Git definitions if the system has LIBGIT2,  */
+char *
+gal_git_describe();
 
 
 
 __END_C_DECLS    /* From C++ preparations */
 
-#endif           /* __GAL_GNUASTRO_H__ */
+#endif           /* __GAL_GIT_H__ */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]