-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.h
285 lines (260 loc) · 8.43 KB
/
data.h
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
#ifndef DATA_H // Include guard
#define DATA_H
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "utility.h"
#include "file-util.h"
#include "struct-state.h"
// Function prototypes because of bad source management
int create_root_commit();
int change_head(int);
int create_stage();
int create_new_branch(char* branch);
bool detached_head(int state);
int switch_branch(char* branch);
// search in current dir and parents
char* find_root_path()
{
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
for (;;) {
if ( file_exists(LITDIR_NAME, true) ) {
char* result = (char*) malloc(PATH_MAX * sizeof(char));
getcwd(result, PATH_MAX);
chdir(original_path);
return result;
}
if ( is_root() ) break;
chdir("..");
}
chdir(original_path);
return NULL;
}
char* find_repo_data()
{
char* result = find_root_path();
if (result == NULL) return NULL;
strcat(result, "\\"); strcat(result, LITDIR_NAME);
return result;
}
bool is_in_repo()
{
return (find_repo_data() != NULL);
}
// Note that it does not use is_in_repo
char* get_config_path(bool is_global)
{
char* confpath;
confpath = (char*) malloc(PATH_MAX * sizeof(char));
if (is_global) {
strcpy(confpath, CONFIG_PATH);
} else {
confpath = find_repo_data();
}
strcat(confpath, "\\config");
return confpath;
}
int create_config_global()
{
char* folder_path = get_config_path(1);
if ( !file_exists(folder_path, true) ) {
mkdir(folder_path);
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path)); // Keep current path
chdir(folder_path);
fclose(fopen("alias.txt", "w")); // Create empty alias file
FILE* f_user = fopen("user.txt", "w");
fprintf(f_user, "Default\[email protected]\n");
fclose(f_user);
chdir(original_path);
return 0;
}
return 1;
}
// Read mode
FILE* open_wt_file(char* relpath)
{
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
chdir(find_root_path());
//
FILE* file = fopen(relpath, "r");
//
chdir(original_path);
return file;
}
// Configures user.name / note that it assumes that all files already exist with data in them
int config_user(char* setting, char* data, int is_global)
{
if ( ! (strcmp(setting, "name") == 0 || strcmp(setting, "email") == 0) ) return 1; // Error code
char* filename = get_config_path(is_global);
strcat(filename, "\\user.txt");
FILE* f_user = fopen(filename, "r");
// Keeping the other one
char *email = (char*) malloc(EMAIL_MAX * sizeof(char));
char *name = (char*) malloc(USERNAME_MAX * sizeof(char));
email[0] = '\0';
name[0] = '\0';
fgets(name, USERNAME_MAX, f_user);
fgets(email, EMAIL_MAX, f_user);
name[strcspn(name, "\n\r")] = '\0'; // fgets will include line break at the end of the string
email[strcspn(email, "\n\r")] = '\0';
fclose(f_user);
// Settling what to keep
f_user = fopen(filename, "w");
if (strcmp(setting, "name") == 0) {
name = data;
} else if (strcmp(setting, "email") == 0) {
email = data;
}
// Writing
fprintf(f_user, "%s\n%s\n", name, email);
fclose(f_user);
free(email); free(name);
return 0;
}
int get_author_name(char* name)
{
char* filename = get_config_path(false);
strcat(filename, "\\user.txt");
FILE* f_user = fopen(filename, "r");
// Keeping the other one
bool undefined_local = false;
if ( fgets(name, USERNAME_MAX, f_user) == NULL ) undefined_local = true;
if ( strlen(name) < 2 ) undefined_local = true;
if ( undefined_local ) {
char* filename_glob = get_config_path(true);
strcat(filename_glob, "\\user.txt");
FILE* f_user_glob = fopen(filename_glob, "r");
fgets(name, USERNAME_MAX, f_user_glob);
fclose(f_user_glob);
}
name[strcspn(name, "\n\r")] = '\0';
fclose(f_user);
return undefined_local;
}
int get_author_email(char* email)
{
char* filename = get_config_path(false);
strcat(filename, "\\user.txt");
FILE* f_user = fopen(filename, "r");
// Keeping the other one
bool undefined_local = false;
if ( fgets(email, EMAIL_MAX, f_user) == NULL ) {};
if ( fgets(email, EMAIL_MAX, f_user) == NULL ) undefined_local = true; // (email is on the second line)
if ( strlen(email) < 2 ) undefined_local = true;
if ( undefined_local ) {
char* filename_glob = get_config_path(true);
strcat(filename_glob, "\\user.txt");
FILE* f_user_glob = fopen(filename_glob, "r");
fgets(email, EMAIL_MAX, f_user_glob);
fgets(email, EMAIL_MAX, f_user_glob);
fclose(f_user_glob);
}
email[strcspn(email, "\n\r")] = '\0';
fclose(f_user);
return undefined_local;
}
int config_alias(char* alias, char* command, int is_global)
{
if (strlen(alias) < 1) return 1; // Validation of alias
char command_copy[COMMAND_MAX];
strcpy(command_copy, command);
char* token = strtok(command_copy, " ");
if (strcmp(token, "lit") != 0) return 1;
token = strtok(NULL, " ");
if (token == NULL || search_str(COMMANDS, token, N_COMMANDS) == -1) return 1; // Checking the validity of command
// Opening config
char* filename = get_config_path(is_global);
strcat(filename, "\\alias.txt");
FILE* f_alias = fopen(filename, "a");
// Writing
fprintf(f_alias, "%s %s\n", alias, command);
fclose(f_alias);
return 0;
}
char* get_alias(char* alias)
{
if (is_in_repo()) {
char* filename = get_config_path(0);
strcat(filename, "\\alias.txt");
FILE* f_alias_repo = fopen(filename, "r");
// Getting lines in reverse order
char lines[MAX_ALIASES][2][COMMAND_MAX]; // lines[n][0]: alias name // lines[n][1]: command
int n_lines = 0;
while ( fscanf(f_alias_repo, "%s ", lines[n_lines][0]) != EOF ) { // Getting the alias name
fgets(lines[n_lines][1], COMMAND_MAX, f_alias_repo); // Getting the command
lines[n_lines][1][strcspn(lines[n_lines][1], "\n")] = '\0'; // Removing the trailing line break
n_lines++;
}
fclose(f_alias_repo);
for (int i = n_lines - 1; i >= 0; i--) {
if ( strcmp(lines[i][0], alias) == 0 ) {
char* output = (char*) malloc(COMMAND_MAX * sizeof(char));
strcpy(output, lines[i][1]);
return output;
}
}
}
char* filename = get_config_path(1);
strcat(filename, "\\alias.txt");
FILE* f_alias_global = fopen(filename, "r");
// Getting lines in reverse order
char lines[MAX_ALIASES][2][COMMAND_MAX]; // lines[n][0]: alias name // lines[n][1]: command
int n_lines = 0;
while ( fscanf(f_alias_global, "%s ", lines[n_lines][0]) != EOF ) { // Getting the alias name
fgets(lines[n_lines][1], COMMAND_MAX, f_alias_global); // Getting the command
lines[n_lines][1][strcspn(lines[n_lines][1], "\n")] = '\0'; // Removing the trailing line break
n_lines++;
}
fclose(f_alias_global);
for (int i = n_lines - 1; i >= 0; i--) {
if ( strcmp(lines[i][0], alias) == 0 ) {
char* output = (char*) malloc(COMMAND_MAX * sizeof(char));
strcpy(output, lines[i][1]);
return output;
}
}
return NULL;
}
// * This function should be further developed in respect to other features that are yet to be added
int initialize_repo()
{
mkdir(LITDIR_NAME);
system("attrib +h .lit");
// char original_path[PATH_MAX];
// getcwd(original_path, sizeof(original_path));
chdir(LITDIR_NAME);
mkdir("config");
chdir("config");
fclose(fopen("alias.txt", "w")); // Create empty alias file
// User info file
FILE* f_user = fopen("user.txt", "w");
// fprintf(f_user, "Default\[email protected]\n");
fclose(f_user);
chdir("..");
// Tags
mkdir("tags");
// States
mkdir("states"); chdir("states");
mkdir("stage");
chdir("stage");
chdir("..");
mkdir("commits");
chdir("..");
mkdir("branch");
// commits
create_root_commit();
change_head(ROOT_ID);
create_stage();
create_new_branch("master");
switch_branch("master");
detached_head(-1);
chdir(".."); // Go back to the original path
return 0;
}
#endif // DATA_H