forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.cpp
More file actions
120 lines (98 loc) · 2.35 KB
/
Copy pathadd.cpp
File metadata and controls
120 lines (98 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <git2.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "git2cpp/repo.h"
#include "git2cpp/initializer.h"
enum print_options {
SKIP = 1,
VERBOSE = 2,
UPDATE = 4,
};
void init_array(git_strarray *array, int argc, char **argv)
{
unsigned int i;
array->count = argc;
array->strings = reinterpret_cast<char **>(malloc(sizeof(char*) * array->count));
assert(array->strings!=NULL);
for(i=0; i<array->count; i++) {
array->strings[i]=argv[i];
}
return;
}
int print_matched_cb( const char *path, const char * /*matched_pathspec*/,
git::Repository const & repo, print_options options )
{
git_status_t status = repo.file_status(path);
int ret;
if (status & GIT_STATUS_WT_MODIFIED ||
status & GIT_STATUS_WT_NEW) {
printf("add '%s'\n", path);
ret = 0;
} else {
ret = 1;
}
if (options & SKIP) {
ret = 1;
}
return ret;
}
void print_usage(void)
{
fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
fprintf(stderr, "\t-n, --dry-run dry run\n");
fprintf(stderr, "\t-v, --verbose be verbose\n");
fprintf(stderr, "\t-u, --update update tracked files\n");
}
int main (int argc, char** argv)
{
using namespace std::placeholders;
git_strarray array = {0};
int i = 1, options = 0;
for (; i < argc; ++i) {
if (argv[i][0] != '-') {
break;
}
else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
options |= VERBOSE;
}
else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
options |= SKIP;
}
else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
options |= UPDATE;
}
else if(!strcmp(argv[i], "-h")) {
print_usage();
break;
}
else if(!strcmp(argv[i], "--")) {
i++;
break;
}
else {
fprintf(stderr, "Unsupported option %s.\n", argv[i]);
print_usage();
return 1;
}
}
if (argc<=i) {
print_usage();
return 1;
}
init_array(&array, argc-i, argv+i);
git::Initializer threads_initializer;
git::Repository repo(".");
git::Index::matched_path_callback_t cb;
if (options&VERBOSE || options&SKIP) {
cb = std::bind(&print_matched_cb, _1, _2, std::cref(repo), static_cast<print_options>(options));
}
git::Index index = repo.index();
if (options&UPDATE) {
index.update_all(array, cb);
} else {
index.add_all(array, cb);
}
index.write();
return 0;
}