-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.c
More file actions
133 lines (109 loc) · 3.43 KB
/
Copy pathfunc.c
File metadata and controls
133 lines (109 loc) · 3.43 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"
#ifdef _WIN32
#define DEVNULL "nul"
#else
#define DEVNULL "/dev/null"
#endif
#define MAX_SIZE 2048
#define SIZE 100
#define URL_SIZE 512
#define COMMAND_SIZE 1024
// Function to create a GitHub repository using the GitHub API
void create_github_repo(char *repo_name, char *username, char *token)
{
char command[1024];
// Use double-quote JSON, escape inner quotes
sprintf(command,
"curl -u \"%s:%s\" -X POST "
"-H \"Accept: application/vnd.github.v3+json\" "
"-d \"{\\\"name\\\":\\\"%s\\\",\\\"private\\\":false}\" "
"https://api.github.com/user/repos > %s 2>&1",
username, token, repo_name, DEVNULL);
printf("Creating repo with command:\n%s\n", command); // temporary for debugging
system(command);
}
// Function to retrieve the Git remote URL from .git/config
char *get_git_remote_url()
{
FILE *fp;
char buffer[MAX_SIZE];
char *url = NULL;
fp = fopen(".git/config", "r");
if (fp == NULL)
{
return strdup("Nincs_git_repo");
}
while (fgets(buffer, MAX_SIZE, fp) != NULL)
{
if (strstr(buffer, "url =") != NULL)
{
// Find the URL part and copy it into a new string
char *url_start = strstr(buffer, "url =") + strlen("url = https://");
url = strdup(url_start);
// Remove trailing newline characters
url[strcspn(url, "\r\n")] = '\0';
break;
}
}
fclose(fp);
return url;
}
// Function to push to an existing Git repository
void normal_git_push(char *git_url, char *token, char *username)
{
system("git add -A");
printf("Your git repo: %s\n", git_url);
char szoveg[SIZE];
do
{
printf("Add the commit message: ");
fgets(szoveg, SIZE, stdin);
} while (has_spec_character(szoveg) != 0);
char gitcommit[COMMAND_SIZE];
snprintf(gitcommit, sizeof(gitcommit), "git commit -m \"%s\"", szoveg);
system(gitcommit);
// Create the gitUrl with safety checks
char gitUrl[URL_SIZE];
snprintf(gitUrl, sizeof(gitUrl), "https://%s:%s@%s", username, token, git_url);
// Create the git command with safety checks
char gitCommand[COMMAND_SIZE];
snprintf(gitCommand, sizeof(gitCommand), "git push -u %s HEAD", gitUrl);
system(gitCommand);
}
// Function to create a new local Git repository and link it to GitHub
void first_git_create(char *username, char *token)
{
system("git branch -m main");
printf("You don't have a git repo\n");
char repo_name[SIZE];
// Searches for special characters
do
{
printf("Write the name of the repository: ");
fgets(repo_name, SIZE, stdin);
} while (has_spec_character(repo_name) != 0);
repo_name[strlen(repo_name) - 1] = '\0'; // Remove trailing newline
// Create the repository on GitHub via the API
create_github_repo(repo_name, username, token);
// Initialize a local Git repository
system("git init");
char remoteadd[200];
sprintf(remoteadd, "git remote add origin https://github.com/%s/%s", username, repo_name);
system(remoteadd);
}
// Searches for special characters
int has_spec_character(char *str)
{
for (int i = 0; str[i] != '\0'; i++)
{
if ((unsigned char)str[i] > 127)
{
printf("Error! Has a Special character!\n");
return 1;
}
}
return 0;
}