mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-10-10 08:55:22 +02:00
fix: clang format
This commit is contained in:
1 parent
518d4fd09c
commit
df0301fdcd
3 files changed
+71
-117
No files matched your search
+24
-29
@@ -97,20 +97,15 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
cJSON *json = cJSON_Parse(data);
|
||||
|
||||
if (!json) {
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
|
||||
if (error_ptr) {
|
||||
error(
|
||||
1,
|
||||
"program",
|
||||
"JSON parse error near: %.30s",
|
||||
error_ptr
|
||||
);
|
||||
}
|
||||
|
||||
free(data);
|
||||
}
|
||||
if (!json) {
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
|
||||
if (error_ptr) {
|
||||
error(1, "program", "JSON parse error near: %.30s", error_ptr);
|
||||
}
|
||||
|
||||
free(data);
|
||||
}
|
||||
error(!json, "program", "JSON parse error");
|
||||
|
||||
// Parse all of the directories which will( or do) contain the vaults
|
||||
@@ -303,24 +298,24 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Configure Git backup
|
||||
cJSON *gitJSON = cJSON_GetObjectItem(backupJSON, "git");
|
||||
|
||||
|
||||
if (gitJSON && cJSON_IsObject(gitJSON)) {
|
||||
cJSON *gitEnableJSON = cJSON_GetObjectItem(gitJSON, "enable");
|
||||
cJSON *gitNameJSON = cJSON_GetObjectItem(gitJSON, "name");
|
||||
cJSON *gitEmailJSON = cJSON_GetObjectItem(gitJSON, "email");
|
||||
|
||||
|
||||
error(!gitEnableJSON || !cJSON_IsBool(gitEnableJSON), "user", "Entry \"git.enable\" in %s must be a bool.", configPath);
|
||||
error(!gitNameJSON || !cJSON_IsString(gitNameJSON), "user", "Entry \"git.name\" in %s must be a string.", configPath);
|
||||
error(!gitEmailJSON || !cJSON_IsString(gitEmailJSON), "user", "Entry \"git.email\" in %s must be a string.", configPath);
|
||||
|
||||
|
||||
gitEnabled = cJSON_IsTrue(gitEnableJSON) ? 1 : 0;
|
||||
|
||||
|
||||
if (gitEnabled) {
|
||||
gitSignatureName = strdup(cJSON_GetStringValue(gitNameJSON));
|
||||
gitSignatureEmail = strdup(cJSON_GetStringValue(gitEmailJSON));
|
||||
|
||||
|
||||
error(gitSignatureName == NULL || gitSignatureEmail == NULL, "program", "malloc failed");
|
||||
|
||||
|
||||
debug("git in %s is enabled with name \"%s\" and email \"%s\"", configPath, gitSignatureName, gitSignatureEmail);
|
||||
} else {
|
||||
debug("git in %s is disabled", configPath);
|
||||
@@ -581,8 +576,8 @@ backup_config_end:
|
||||
|
||||
// initialized libgit2
|
||||
if (gitEnabled) {
|
||||
int libgit2InitializationTimes = git_libgit2_init();
|
||||
error(libgit2InitializationTimes != 1, "program", "libgit2 has been initialized %d times (instead of one)", libgit2InitializationTimes);
|
||||
int libgit2InitializationTimes = git_libgit2_init();
|
||||
error(libgit2InitializationTimes != 1, "program", "libgit2 has been initialized %d times (instead of one)", libgit2InitializationTimes);
|
||||
}
|
||||
|
||||
int shouldExit = 0;
|
||||
@@ -667,7 +662,7 @@ backup_config_end:
|
||||
char *notesDirectoryString = getDirectoryFromVault(vaultSelected, vaultsArray, vaultsCount, vaultsCountForEachDirectory, directoriesArray, numDirectories, shouldDebug);
|
||||
|
||||
if (gitEnabled) { // we check if we need to git init the vault each time we enter it
|
||||
ensureGitDirectory(notesDirectoryString, vaultSelected, gitSignatureName, gitSignatureEmail, shouldDebug);
|
||||
ensureGitDirectory(notesDirectoryString, vaultSelected, gitSignatureName, gitSignatureEmail, shouldDebug);
|
||||
}
|
||||
|
||||
while (!shouldExit && !shouldChangeVault) {
|
||||
@@ -773,12 +768,12 @@ backup_config_end:
|
||||
}
|
||||
openEditor(fullPath, editorToOpen, shouldRender, shouldJumpToEnd, shouldDebug);
|
||||
if (gitEnabled) { // After closing a file, we need to update the git repo (if git is enable
|
||||
char *date = getDateAndTime();
|
||||
char *commitMsg = malloc(29);
|
||||
snprintf(commitMsg, 29, "Update - %s", date);
|
||||
gitBackupUpdate(notesDirectoryString, vaultSelected, gitSignatureName, gitSignatureEmail, commitMsg, shouldDebug);
|
||||
free(date);
|
||||
free(commitMsg);
|
||||
char *date = getDateAndTime();
|
||||
char *commitMsg = malloc(29);
|
||||
snprintf(commitMsg, 29, "Update - %s", date);
|
||||
gitBackupUpdate(notesDirectoryString, vaultSelected, gitSignatureName, gitSignatureEmail, commitMsg, shouldDebug);
|
||||
free(date);
|
||||
free(commitMsg);
|
||||
}
|
||||
free(fullPath);
|
||||
} else if (strcmp(noteSelected, "Create new note") == 0) {
|
||||
|
||||
+46
-87
@@ -114,39 +114,40 @@ static void ensureDir(const char *path, const int shouldDebug) {
|
||||
}
|
||||
|
||||
void ensureGitDirectory(const char *path, const char *vault, const char *signatureName, const char *signatureEmail, const int shouldDebug) {
|
||||
git_repository *repo = NULL;
|
||||
char *fullPath = malloc(PATH_MAX);
|
||||
snprintf(fullPath, PATH_MAX, "%s/%s", path, vault);
|
||||
git_repository *repo = NULL;
|
||||
char *fullPath = malloc(PATH_MAX);
|
||||
snprintf(fullPath, PATH_MAX, "%s/%s", path, vault);
|
||||
|
||||
int git_repo_error_code = git_repository_open(&repo, fullPath);
|
||||
if (git_repo_error_code) { // error (most probably that the git repo wasn't initialized)
|
||||
int git_init_error_code = git_repository_init(&repo, fullPath, 0); // the 0 indicates that the directory isn't bare. See https://libgit2.org/docs/reference/main/repository/git_repository_init.html
|
||||
error(git_init_error_code, "program", "git initialization didn't worked. Error code : %d\nSee https://libgit2.org/docs/reference/main/repository/git_repository_init.html for documentation\n\n%s", git_init_error_code, git_error_last()->message);
|
||||
debug("git repo %s was initialized.", fullPath);
|
||||
|
||||
// we need to setup the .gitignore
|
||||
char *gitignorePath = malloc(PATH_MAX);
|
||||
snprintf(gitignorePath, PATH_MAX, "%s/.gitignore", fullPath);
|
||||
FILE *gitignore = fopen(gitignorePath, "a");
|
||||
error(gitignore == NULL, "program", "failed to open/create %s\n%s", gitignorePath, git_error_last()->message);
|
||||
fputs("*\n", gitignore);
|
||||
fputs("!*/\n", gitignore);
|
||||
fputs("!*.md\n", gitignore);
|
||||
fclose(gitignore);
|
||||
debug("Succesfully wrote .gitignore to %s", gitignorePath);
|
||||
git_repository_free(repo); // we free to be able to open it in gitBackupUpdate()
|
||||
char *date = getDateAndTime();
|
||||
char *commitMsg = malloc(37);
|
||||
snprintf(commitMsg, 37, "Initial commit - %s", date);
|
||||
gitBackupUpdate(path, vault, signatureName, signatureEmail, commitMsg, shouldDebug);
|
||||
free(date);
|
||||
free(commitMsg);
|
||||
} else { // the git repo already exists
|
||||
debug("%s is already a git repo. Skipping initialization...", fullPath);
|
||||
git_repository_free(repo);
|
||||
}
|
||||
int git_repo_error_code = git_repository_open(&repo, fullPath);
|
||||
if (git_repo_error_code) { // error (most probably that the git repo wasn't initialized)
|
||||
int git_init_error_code = git_repository_init(&repo, fullPath, 0); // the 0 indicates that the directory isn't bare. See https://libgit2.org/docs/reference/main/repository/git_repository_init.html
|
||||
error(git_init_error_code, "program", "git initialization didn't worked. Error code : %d\nSee https://libgit2.org/docs/reference/main/repository/git_repository_init.html for documentation\n\n%s",
|
||||
git_init_error_code, git_error_last()->message);
|
||||
debug("git repo %s was initialized.", fullPath);
|
||||
|
||||
free(fullPath);
|
||||
// we need to setup the .gitignore
|
||||
char *gitignorePath = malloc(PATH_MAX);
|
||||
snprintf(gitignorePath, PATH_MAX, "%s/.gitignore", fullPath);
|
||||
FILE *gitignore = fopen(gitignorePath, "a");
|
||||
error(gitignore == NULL, "program", "failed to open/create %s\n%s", gitignorePath, git_error_last()->message);
|
||||
fputs("*\n", gitignore);
|
||||
fputs("!*/\n", gitignore);
|
||||
fputs("!*.md\n", gitignore);
|
||||
fclose(gitignore);
|
||||
debug("Succesfully wrote .gitignore to %s", gitignorePath);
|
||||
git_repository_free(repo); // we free to be able to open it in gitBackupUpdate()
|
||||
char *date = getDateAndTime();
|
||||
char *commitMsg = malloc(37);
|
||||
snprintf(commitMsg, 37, "Initial commit - %s", date);
|
||||
gitBackupUpdate(path, vault, signatureName, signatureEmail, commitMsg, shouldDebug);
|
||||
free(date);
|
||||
free(commitMsg);
|
||||
} else { // the git repo already exists
|
||||
debug("%s is already a git repo. Skipping initialization...", fullPath);
|
||||
git_repository_free(repo);
|
||||
}
|
||||
|
||||
free(fullPath);
|
||||
}
|
||||
|
||||
void gitBackupUpdate(const char *path, const char *vault, const char *signatureName, const char *signatureEmail, const char *commitMsg, const int shouldDebug) {
|
||||
@@ -162,7 +163,7 @@ void gitBackupUpdate(const char *path, const char *vault, const char *signatureN
|
||||
git_oid commit_id;
|
||||
|
||||
char *fullPath = malloc(PATH_MAX);
|
||||
|
||||
|
||||
error(fullPath == NULL, "program", "Failed to allocate path");
|
||||
|
||||
snprintf(fullPath, PATH_MAX, "%s/%s", path, vault);
|
||||
@@ -196,33 +197,16 @@ void gitBackupUpdate(const char *path, const char *vault, const char *signatureN
|
||||
} else {
|
||||
git_oid head_oid;
|
||||
|
||||
return_code = git_reference_name_to_id(
|
||||
&head_oid,
|
||||
repo,
|
||||
"HEAD"
|
||||
);
|
||||
return_code = git_reference_name_to_id(&head_oid, repo, "HEAD");
|
||||
error(return_code, "program", "Failed to get HEAD\n%s", git_error_last()->message);
|
||||
|
||||
return_code = git_commit_lookup(
|
||||
&head_commit,
|
||||
repo,
|
||||
&head_oid
|
||||
);
|
||||
return_code = git_commit_lookup(&head_commit, repo, &head_oid);
|
||||
error(return_code, "program", "Failed to lookup HEAD commit\n%s", git_error_last()->message);
|
||||
|
||||
return_code = git_commit_tree(
|
||||
&head_tree,
|
||||
head_commit
|
||||
);
|
||||
return_code = git_commit_tree(&head_tree, head_commit);
|
||||
error(return_code, "program", "Failed to get HEAD tree\n%s", git_error_last()->message);
|
||||
|
||||
return_code = git_diff_tree_to_index(
|
||||
&diff,
|
||||
repo,
|
||||
head_tree,
|
||||
index,
|
||||
NULL
|
||||
);
|
||||
return_code = git_diff_tree_to_index(&diff, repo, head_tree, index, NULL);
|
||||
|
||||
error(return_code, "program", "Failed to create diff\n%s", git_error_last()->message);
|
||||
|
||||
@@ -243,39 +227,14 @@ void gitBackupUpdate(const char *path, const char *vault, const char *signatureN
|
||||
error(return_code, "program", "Failed to lookup tree\n%s", git_error_last()->message);
|
||||
|
||||
/* Author/committer */
|
||||
return_code = git_signature_now(
|
||||
&signature,
|
||||
signatureName,
|
||||
signatureEmail
|
||||
);
|
||||
return_code = git_signature_now(&signature, signatureName, signatureEmail);
|
||||
error(return_code, "program", "Failed to create signature\n%s", git_error_last()->message);
|
||||
|
||||
/* Create commit */
|
||||
if (git_repository_head_unborn(repo)) {
|
||||
return_code = git_commit_create_v(
|
||||
&commit_id,
|
||||
repo,
|
||||
"HEAD",
|
||||
signature,
|
||||
signature,
|
||||
NULL,
|
||||
commitMsg,
|
||||
tree,
|
||||
0
|
||||
);
|
||||
return_code = git_commit_create_v(&commit_id, repo, "HEAD", signature, signature, NULL, commitMsg, tree, 0);
|
||||
} else {
|
||||
return_code = git_commit_create_v(
|
||||
&commit_id,
|
||||
repo,
|
||||
"HEAD",
|
||||
signature,
|
||||
signature,
|
||||
NULL,
|
||||
commitMsg,
|
||||
tree,
|
||||
1,
|
||||
head_commit
|
||||
);
|
||||
return_code = git_commit_create_v(&commit_id, repo, "HEAD", signature, signature, NULL, commitMsg, tree, 1, head_commit);
|
||||
}
|
||||
error(return_code, "program", "Failed to create commit\n%s", git_error_last()->message);
|
||||
|
||||
@@ -351,13 +310,13 @@ void initAppFilesAndDirs(const char *home, const int shouldDebug) {
|
||||
}
|
||||
|
||||
char *getDateAndTime() {
|
||||
char *return_value = malloc(20);
|
||||
time_t now = time(NULL);
|
||||
struct tm tm_now;
|
||||
localtime_r(&now, &tm_now);
|
||||
char *return_value = malloc(20);
|
||||
time_t now = time(NULL);
|
||||
struct tm tm_now;
|
||||
localtime_r(&now, &tm_now);
|
||||
|
||||
strftime(return_value, sizeof(return_value), "%Y-%m-%%d %H:%M:%%S", &tm_now);
|
||||
return return_value;
|
||||
strftime(return_value, sizeof(return_value), "%Y-%m-%%d %H:%M:%%S", &tm_now);
|
||||
return return_value;
|
||||
}
|
||||
|
||||
void handleBackups(char **sourceDirectoryArray, const int sourceNumber, char **destinationDirectoryArray, const char *homeDir, const int interval, const char **rsyncArguments, const int rsyncArgumentsNumber,
|
||||
|
||||
+1
-1
@@ -6,6 +6,7 @@
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <ftw.h>
|
||||
#include <git2.h>
|
||||
#include <limits.h>
|
||||
#include <ncurses.h>
|
||||
#include <pwd.h>
|
||||
@@ -17,7 +18,6 @@
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <git2.h>
|
||||
|
||||
#ifndef VERSION
|
||||
#define VERSION "dev"
|
||||
|
||||
Reference in new issue
Block a user