Coverage Report

Created: 2026-10-17 07:15

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