File: git.c

package info (click to toggle)
gnuastro 0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,824 kB
  • sloc: ansic: 176,016; sh: 14,784; makefile: 1,298; cpp: 9
file content (78 lines) | stat: -rw-r--r-- 2,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*********************************************************************
Functions to deal with Git version controlled directories.
This is part of GNU Astronomy Utilities (Gnuastro) package.

Original author:
     Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2016-2024 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. */
char *
gal_git_describe(void)
{
  char *describe_return=NULL;

/* Only actually work on Git if the library is present. */
#if GAL_CONFIG_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, "%s: allocating %zu bytes to copy "
              "Git's describe", __func__, strlen(buf.ptr)+1);
      strcpy(describe_return, buf.ptr);
    }

  /* Clean up. */
  git_repository_free(repo);
  git_libgit2_shutdown();
#endif

  /* Return */
  return describe_return;
}