Skip to content

Commit 960d2a0

Browse files
committed
examples: consolidate includes into "common.h"
Consolidate all standard includes and defines into "common.h". This lets us avoid having to handle platform-specific things in multiple places.
1 parent 3be09b6 commit 960d2a0

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

examples/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,25 @@
1212
* <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
*/
1414

15+
#include <sys/types.h>
16+
#include <sys/stat.h>
1517
#include <stdio.h>
1618
#include <string.h>
1719
#include <stdlib.h>
1820
#include <git2.h>
21+
#include <fcntl.h>
22+
23+
#ifdef _WIN32
24+
# include <io.h>
25+
# include <Windows.h>
26+
# define open _open
27+
# define read _read
28+
# define close _close
29+
# define ssize_t int
30+
# define sleep(a) Sleep(a * 1000)
31+
#else
32+
# include <unistd.h>
33+
#endif
1934

2035
#ifndef PRIuZ
2136
/* Define the printf format specifer to use for size_t output */

examples/for-each-ref.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <git2.h>
2-
#include <stdio.h>
32
#include "common.h"
43

54
static int show_ref(git_reference *ref, void *data)

examples/general.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
* [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
3737
*/
3838

39+
#include "common.h"
40+
3941
/**
4042
* ### Includes
4143
*
4244
* Including the `git2.h` header will include all the other libgit2 headers
4345
* that you need. It should be the only thing you need to include in order
4446
* to compile properly and get all the libgit2 API.
4547
*/
46-
#include <git2.h>
47-
#include <stdio.h>
48-
#include <string.h>
48+
#include "git2.h"
4949

5050
static void oid_parsing(git_oid *out);
5151
static void object_database(git_repository *repo, git_oid *oid);

examples/index-pack.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
#include "common.h"
22

3-
#include <sys/types.h>
4-
#include <sys/stat.h>
5-
#include <fcntl.h>
6-
#ifdef _WIN32
7-
# include <io.h>
8-
# include <Windows.h>
9-
10-
# define open _open
11-
# define read _read
12-
# define close _close
13-
14-
#define ssize_t int
15-
#else
16-
# include <unistd.h>
17-
#endif
18-
193
/*
204
* This could be run in the main loop whilst the application waits for
215
* the indexing to finish in a worker thread

examples/status.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
*/
1414

1515
#include "common.h"
16-
#ifdef _WIN32
17-
# include <Windows.h>
18-
# define sleep(a) Sleep(a * 1000)
19-
#else
20-
# include <unistd.h>
21-
#endif
2216

2317
/**
2418
* This example demonstrates the use of the libgit2 status APIs,

0 commit comments

Comments
 (0)