Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/version.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "version.h"
5
#include "strbuf.h"
6
#include "gettext.h"
7
8
#ifndef GIT_VERSION_H
9
# include "version-def.h"
10
#else
11
# include GIT_VERSION_H
12
#endif
13
14
const char git_version_string[] = GIT_VERSION;
15
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
16
17
/*
18
 * Trim and replace each character with ascii code below 32 or above
19
 * 127 (included) using a dot '.' character.
20
 */
21
static void redact_non_printables(struct strbuf *buf)
22
0
{
23
0
  strbuf_trim(buf);
24
0
  for (size_t i = 0; i < buf->len; i++) {
25
0
    if (!isprint(buf->buf[i]) || buf->buf[i] == ' ')
26
0
      buf->buf[i] = '.';
27
0
  }
28
0
}
29
30
const char *git_user_agent(void)
31
0
{
32
0
  static const char *agent = NULL;
33
34
0
  if (!agent) {
35
0
    agent = getenv("GIT_USER_AGENT");
36
0
    if (!agent)
37
0
      agent = GIT_USER_AGENT;
38
0
  }
39
40
0
  return agent;
41
0
}
42
43
/*
44
  Retrieve, sanitize and cache operating system info for subsequent
45
  calls. Return a pointer to the sanitized operating system info
46
  string.
47
*/
48
static const char *os_info(void)
49
0
{
50
0
  static const char *os = NULL;
51
52
0
  if (!os) {
53
0
    struct strbuf buf = STRBUF_INIT;
54
55
0
    get_uname_info(&buf, 0);
56
    /* Sanitize the os information immediately */
57
0
    redact_non_printables(&buf);
58
0
    os = strbuf_detach(&buf, NULL);
59
0
  }
60
61
0
  return os;
62
0
}
63
64
const char *git_user_agent_sanitized(void)
65
0
{
66
0
  static const char *agent = NULL;
67
68
0
  if (!agent) {
69
0
    struct strbuf buf = STRBUF_INIT;
70
71
0
    strbuf_addstr(&buf, git_user_agent());
72
73
0
    if (!getenv("GIT_USER_AGENT")) {
74
0
      strbuf_addch(&buf, '-');
75
0
      strbuf_addstr(&buf, os_info());
76
0
    }
77
0
    redact_non_printables(&buf);
78
0
    agent = strbuf_detach(&buf, NULL);
79
0
  }
80
81
0
  return agent;
82
0
}
83
84
int get_uname_info(struct strbuf *buf, unsigned int full)
85
0
{
86
0
  struct utsname uname_info;
87
88
0
  if (uname(&uname_info)) {
89
0
    strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"),
90
0
          strerror(errno),
91
0
          errno);
92
0
    return -1;
93
0
  }
94
0
  if (full)
95
0
    strbuf_addf(buf, "%s %s %s %s\n",
96
0
          uname_info.sysname,
97
0
          uname_info.release,
98
0
          uname_info.version,
99
0
          uname_info.machine);
100
0
  else
101
0
       strbuf_addf(buf, "%s\n", uname_info.sysname);
102
0
  return 0;
103
0
}