Skip to content

Commit 54db285

Browse files
Unique-Usmangitster
authored andcommitted
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. We may need to refactor this function in the future if an `osVersion.format` config option is added, but for now we only need it to accept a "full" flag that makes it switch between providing full OS information and providing only the OS name. The mode providing only the OS name is needed in a following commit. Mentored-by: Christian Couder <[email protected]> Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f33230 commit 54db285

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

builtin/bugreport.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "diagnose.h"
1313
#include "object-file.h"
1414
#include "setup.h"
15+
#include "version.h"
1516

1617
static void get_system_info(struct strbuf *sys_info)
1718
{
18-
struct utsname uname_info;
1919
char *shell = NULL;
2020

2121
/* get git version from native cmd */
@@ -24,16 +24,7 @@ static void get_system_info(struct strbuf *sys_info)
2424

2525
/* system call for other version info */
2626
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, 1);
3728

3829
strbuf_addstr(sys_info, _("compiler info: "));
3930
get_compiler_info(sys_info);

version.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "version.h"
33
#include "version-def.h"
44
#include "strbuf.h"
5+
#include "gettext.h"
56

67
const char git_version_string[] = GIT_VERSION;
78
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
@@ -47,3 +48,25 @@ const char *git_user_agent_sanitized(void)
4748

4849
return agent;
4950
}
51+
52+
int get_uname_info(struct strbuf *buf, unsigned int full)
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+
if (full)
64+
strbuf_addf(buf, "%s %s %s %s\n",
65+
uname_info.sysname,
66+
uname_info.release,
67+
uname_info.version,
68+
uname_info.machine);
69+
else
70+
strbuf_addf(buf, "%s\n", uname_info.sysname);
71+
return 0;
72+
}

version.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ extern const char git_built_from_commit_string[];
77
const char *git_user_agent(void);
88
const char *git_user_agent_sanitized(void);
99

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, unsigned int full);
16+
1017
#endif /* VERSION_H */

0 commit comments

Comments
 (0)