version: refactor get_uname_info()

Some code from "builtin/bugreport.c" uses uname(2) to get system information. Let's refactor this code into a new get_uname_info() function, so that we can reuse it in a following commit. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Usman Akinyemi committed Feb 15, 2025 at 21:20 UTC 0a78d61247922f30ebf2ce09025dcaa7bd7e3583
3 files changed +29 -11
builtin/bugreport.c
+2 -11
@@ -12,10 +12,10 @@
12 #include "diagnose.h"
13 #include "object-file.h"
14 #include "setup.h"
15 +#include "version.h"
16
17 static void get_system_info(struct strbuf *sys_info)
18 {
18 - struct utsname uname_info;
19 char *shell = NULL;
20
21 /* get git version from native cmd */
@@ -24,16 +24,7 @@ static void get_system_info(struct strbuf *sys_info)
24
25 /* system call for other version info */
26 strbuf_addstr(sys_info, "uname: ");
27 - if (uname(&uname_info))
28 - strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"),
29 - strerror(errno),
30 - errno);
31 - else
32 - strbuf_addf(sys_info, "%s %s %s %s\n",
33 - uname_info.sysname,
34 - uname_info.release,
35 - uname_info.version,
36 - uname_info.machine);
27 + get_uname_info(sys_info);
28
29 strbuf_addstr(sys_info, _("compiler info: "));
30 get_compiler_info(sys_info);
version.c
+20
@@ -3,6 +3,7 @@
3 #include "version-def.h"
4 #include "strbuf.h"
5 #include "sane-ctype.h"
6 +#include "gettext.h"
7
8 const char git_version_string[] = GIT_VERSION;
9 const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
@@ -47,3 +48,22 @@ const char *git_user_agent_sanitized(void)
48
49 return agent;
50 }
51 +
52 +int get_uname_info(struct strbuf *buf)
53 +{
54 + struct utsname uname_info;
55 +
56 + if (uname(&uname_info)) {
57 + strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"),
58 + strerror(errno),
59 + errno);
60 + return -1;
61 + }
62 +
63 + strbuf_addf(buf, "%s %s %s %s\n",
64 + uname_info.sysname,
65 + uname_info.release,
66 + uname_info.version,
67 + uname_info.machine);
68 + return 0;
69 +}
version.h
+7
@@ -7,4 +7,11 @@ extern const char git_built_from_commit_string[];
7 const char *git_user_agent(void);
8 const char *git_user_agent_sanitized(void);
9
10 +/*
11 + Try to get information about the system using uname(2).
12 + Return -1 and put an error message into 'buf' in case of uname()
13 + error. Return 0 and put uname info into 'buf' otherwise.
14 +*/
15 +int get_uname_info(struct strbuf *buf);
16 +
17 #endif /* VERSION_H */