-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_core.h
More file actions
323 lines (248 loc) · 9.34 KB
/
git_core.h
File metadata and controls
323 lines (248 loc) · 9.34 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* git_core.h - Git client core for NeXTSTEP 3.3
*
* Pure logic layer: no printf, no stdin, no GUI calls.
* Returns data via structs; reports progress via callback.
*
* (c) 2026 ARNLTony & Claude. MIT License.
*/
#ifndef GIT_CORE_H
#define GIT_CORE_H
/* --- Limits --- */
#define GIT_MAX_FILES 500
#define GIT_MAX_PATH 512
#define GIT_MAX_SHA 41 /* 40 hex + null */
#define GIT_MAX_MSG 1024
#define GIT_MAX_OWNER 128
#define GIT_MAX_REPO 128
#define GIT_MAX_BRANCH 64
#define GIT_MAX_TOKEN 256
#define GIT_MAX_AUTHOR 128
#define GIT_MAX_DATE 32
#define GIT_MAX_ERRMSG 512
#define GIT_MAX_COMMITS 100
#define GIT_MAX_BRANCHES 50
#define GIT_MAX_COMPARE_FILES 50
#define GIT_MAX_PATCH 4096
#define GIT_MAX_TAGS 50
#define GIT_MAX_RELEASES 20
#define GIT_MAX_IGNORE_PATTERNS 100
#define GIT_MAX_IGNORE_PATTERN 128
#define GIT_STATE_FILE ".nextstep_git"
#define GIT_PENDING_FILE ".nextstep_git_pending"
#define GIT_RESPONSE_BUF 131072 /* 128KB for API responses */
#define GIT_HTTP_BUF 4096
/* --- File status --- */
#define GIT_STATUS_TRACKED 0 /* clean, matches remote */
#define GIT_STATUS_MODIFIED 1 /* local differs from tracked */
#define GIT_STATUS_STAGED 2 /* staged for commit */
#define GIT_STATUS_DELETED 3 /* tracked but removed locally */
#define GIT_STATUS_NEW 4 /* on disk, not tracked */
#define GIT_STATUS_UNTRACKED 5 /* not tracked at all */
/* --- Error codes --- */
#define GIT_OK 0
#define GIT_ERR_NETWORK -1
#define GIT_ERR_AUTH -2
#define GIT_ERR_NOTFOUND -3
#define GIT_ERR_DISK -4
#define GIT_ERR_MEMORY -5
#define GIT_ERR_STATE -6
#define GIT_ERR_CONFLICT -7
#define GIT_ERR_TOOLARGE -8
#define GIT_ERR_NOCHANGES -9
#define GIT_ERR_PARAM -10
/* --- Progress events --- */
#define GIT_PROGRESS_START 0
#define GIT_PROGRESS_DOWNLOAD 1
#define GIT_PROGRESS_UPLOAD 2
#define GIT_PROGRESS_STATUS 3
#define GIT_PROGRESS_DONE 4
#define GIT_PROGRESS_ERROR 5
/* --- Data structures --- */
typedef struct GitProgress {
int event;
int current;
int total;
char message[GIT_MAX_ERRMSG];
} GitProgress;
/* Progress callback: return 0 to continue, -1 to cancel */
typedef int (*GitProgressFn)(GitProgress *progress, void *userdata);
typedef struct GitRepo {
char owner[GIT_MAX_OWNER];
char repo[GIT_MAX_REPO];
char branch[GIT_MAX_BRANCH];
char local_path[GIT_MAX_PATH];
char token[GIT_MAX_TOKEN];
char head_sha[GIT_MAX_SHA];
char tree_sha[GIT_MAX_SHA];
} GitRepo;
typedef struct GitFileEntry {
char path[GIT_MAX_PATH];
char sha[GIT_MAX_SHA];
int status;
long size;
} GitFileEntry;
typedef struct GitFileList {
GitFileEntry files[GIT_MAX_FILES];
int count;
} GitFileList;
typedef struct GitCommit {
char sha[GIT_MAX_SHA];
char message[GIT_MAX_MSG];
char author[GIT_MAX_AUTHOR];
char date[GIT_MAX_DATE];
} GitCommit;
typedef struct GitCommitLog {
GitCommit commits[GIT_MAX_COMMITS];
int count;
} GitCommitLog;
typedef struct GitDiffEntry {
char path[GIT_MAX_PATH];
int status;
char *old_content; /* from GitHub, NULL if new file */
char *new_content; /* from disk, NULL if deleted */
int old_size;
int new_size;
} GitDiffEntry;
typedef struct GitDiffResult {
GitDiffEntry *entries; /* heap-allocated array */
int count;
} GitDiffResult;
typedef struct GitBranchEntry {
char name[GIT_MAX_BRANCH];
char sha[GIT_MAX_SHA];
int is_current;
} GitBranchEntry;
typedef struct GitBranchList {
GitBranchEntry branches[GIT_MAX_BRANCHES];
int count;
} GitBranchList;
typedef struct GitCompareFile {
char filename[GIT_MAX_PATH];
char status[16]; /* "added", "removed", "modified", "renamed" */
int additions;
int deletions;
char *patch; /* heap-allocated patch text, may be NULL */
} GitCompareFile;
typedef struct GitCompareResult {
char status[16]; /* "ahead", "behind", "diverged", "identical" */
int ahead_by;
int behind_by;
int total_commits;
GitCompareFile *files; /* heap-allocated array */
int file_count;
} GitCompareResult;
typedef struct GitTagEntry {
char name[GIT_MAX_BRANCH];
char sha[GIT_MAX_SHA];
char message[GIT_MAX_MSG];
int is_annotated;
} GitTagEntry;
typedef struct GitTagList {
GitTagEntry tags[GIT_MAX_TAGS];
int count;
} GitTagList;
typedef struct GitRelease {
long id;
char tag_name[GIT_MAX_BRANCH];
char name[GIT_MAX_MSG];
char body[GIT_MAX_MSG];
int draft;
int prerelease;
} GitRelease;
typedef struct GitReleaseList {
GitRelease releases[GIT_MAX_RELEASES];
int count;
} GitReleaseList;
typedef struct GitMergeResult {
int code;
char message[GIT_MAX_ERRMSG];
char sha[GIT_MAX_SHA];
int conflicts;
} GitMergeResult;
typedef struct GitIgnoreList {
char patterns[GIT_MAX_IGNORE_PATTERNS][GIT_MAX_IGNORE_PATTERN];
int negated[GIT_MAX_IGNORE_PATTERNS];
int dir_only[GIT_MAX_IGNORE_PATTERNS];
int count;
} GitIgnoreList;
typedef struct GitResult {
int code;
char message[GIT_MAX_ERRMSG];
int files_affected;
} GitResult;
/* --- Core operations --- */
GitResult git_init_repo(GitRepo *r, char *owner, char *repo,
char *branch, char *local_path, char *token);
GitResult git_clone(GitRepo *r, GitProgressFn progress_fn, void *userdata);
GitResult git_pull(GitRepo *r, GitProgressFn progress_fn, void *userdata);
GitResult git_status(GitRepo *r, GitFileList *out);
GitResult git_add(GitRepo *r, char *path, GitFileList *state);
GitResult git_add_all(GitRepo *r, GitFileList *state);
GitResult git_commit(GitRepo *r, char *message,
char *author_name, char *author_email,
GitFileList *state);
GitResult git_push(GitRepo *r, GitFileList *state,
GitProgressFn progress_fn, void *userdata);
GitResult git_log(GitRepo *r, int max_count, GitCommitLog *out);
GitResult git_diff(GitRepo *r, char *path, GitFileList *state,
GitDiffResult *out,
GitProgressFn progress_fn, void *userdata);
/* --- Branch operations --- */
GitResult git_branch_list(GitRepo *r, GitBranchList *out);
GitResult git_branch_create(GitRepo *r, char *name, char *from_sha);
GitResult git_branch_switch(GitRepo *r, char *name, GitFileList *fl,
GitProgressFn progress_fn, void *userdata);
GitResult git_branch_delete(GitRepo *r, char *name);
/* --- Merge operations --- */
GitMergeResult git_merge(GitRepo *r, char *head_branch, char *message);
/* --- Tag operations --- */
GitResult git_tag_list(GitRepo *r, GitTagList *out);
GitResult git_tag_create(GitRepo *r, char *name, char *message);
/* --- Release operations --- */
GitResult git_release_list(GitRepo *r, GitReleaseList *out);
GitResult git_release_create(GitRepo *r, char *tag, char *title,
char *body);
/* --- Fork operations --- */
GitResult git_fork(GitRepo *r);
/* --- File delete operations --- */
GitResult git_rm(GitRepo *r, char *path, GitFileList *fl);
/* --- Gitignore operations --- */
GitResult git_ignore_load(GitRepo *r, GitIgnoreList *out);
int git_ignore_match(GitIgnoreList *ignore, char *path);
/* --- Compare operations --- */
GitResult git_compare(GitRepo *r, char *base, char *head,
GitCompareResult *out);
void git_compare_free(GitCompareResult *result);
/* --- State persistence --- */
GitResult git_load_state(GitRepo *r, GitFileList *out);
GitResult git_save_state(GitRepo *r, GitFileList *state);
GitResult git_load_pending(GitRepo *r, char *message_out, int message_size);
GitResult git_save_pending(GitRepo *r, char *message);
GitResult git_clear_pending(GitRepo *r);
/* --- Memory management --- */
void git_diff_free(GitDiffResult *diff);
/* --- Safe string formatting (snprintf shim for NeXTSTEP 3.3) --- */
int safe_snprintf(char *buf, int size, char *fmt, ...);
/* --- File utilities --- */
char *git_read_file(char *filepath, long *size_out);
int git_write_file(char *filepath, char *data, long size);
int git_is_directory(char *path);
int git_is_file(char *path);
int git_mkdir_p(char *path);
int git_scan_directory(char *dirpath, char paths[][GIT_MAX_PATH],
int max_entries);
/* --- GitHub API (shared with gh) --- */
int gh_api_request(char *token, char *method, char *path,
char *post_body, char *response, int response_size);
/* JSON helpers */
char *json_find_string(char *json, char *key, int *out_len);
long json_find_number(char *json, char *key);
int json_find_bool(char *json, char *key);
char *json_array_first(char *json, char **end);
char *json_array_next(char *pos, char **end);
void json_unescape(char *src, int src_len, char *dst, int dst_size);
void json_escape(char *src, char *dst, int dst_size);
int gh_base64_decode(char *src, int src_len, char *dst, int dst_size);
int gh_base64_encode(char *src, int src_len, char *dst, int dst_size);
#endif /* GIT_CORE_H */