2017-06-06 17:45:44 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <Eina.h>
|
|
|
|
#include <Ecore.h>
|
|
|
|
#include <Ecore_File.h>
|
|
|
|
|
|
|
|
#include "Edi.h"
|
|
|
|
#include "edi_private.h"
|
|
|
|
#include <edi_exe.h>
|
|
|
|
#include "edi_path.h"
|
|
|
|
#include "edi_scm.h"
|
|
|
|
|
|
|
|
Edi_Scm_Engine *_edi_scm_global_object = NULL;
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_exec(const char *command)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
char *oldpwd;
|
|
|
|
Edi_Scm_Engine *self = _edi_scm_global_object;
|
|
|
|
|
|
|
|
if (!self) return -1;
|
|
|
|
|
|
|
|
oldpwd = getcwd(NULL, PATH_MAX);
|
|
|
|
|
|
|
|
chdir(edi_project_get());
|
|
|
|
code = edi_exe_wait(command);
|
|
|
|
chdir(oldpwd);
|
|
|
|
|
|
|
|
free(oldpwd);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
_edi_scm_exec_response(const char *command)
|
|
|
|
{
|
|
|
|
char *oldpwd, *response;
|
|
|
|
Edi_Scm_Engine *self = _edi_scm_global_object;
|
|
|
|
|
|
|
|
if (!self) return NULL;
|
|
|
|
|
|
|
|
oldpwd = getcwd(NULL, PATH_MAX);
|
|
|
|
|
|
|
|
chdir(edi_project_get());
|
|
|
|
response = edi_exe_response(command);
|
|
|
|
chdir(oldpwd);
|
|
|
|
|
|
|
|
free(oldpwd);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
edi_scm_git_new(void)
|
|
|
|
{
|
|
|
|
return _edi_scm_exec("git init .");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_file_add(const char *path)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git add '%s'", path);
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_file_mod(const char *path)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git mod '%s'", path);
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_file_move(const char *source, const char *dest)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git mv %s %s", source, dest);
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_file_del(const char *path)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git rm '%s'", path);
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_status(void)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append(command, "git status");
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_commit(const char *message)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git commit -m '%s'", message);
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_push(void)
|
|
|
|
{
|
2017-06-06 19:23:40 -07:00
|
|
|
return _edi_scm_exec("git push");
|
2017-06-06 17:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_pull(void)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append(command, "git pull");
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_stash(void)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append(command, "git stash");
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_edi_scm_git_remote_add(const char *remote_url)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git remote add origin %s", remote_url);
|
|
|
|
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
2017-06-06 19:23:40 -07:00
|
|
|
if (code == 0)
|
|
|
|
code = _edi_scm_exec("git push --set-upstream origin master");
|
|
|
|
|
2017-06-06 17:45:44 -07:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
_edi_scm_git_remote_name_get(void)
|
|
|
|
{
|
2017-06-07 14:32:10 -07:00
|
|
|
static char *_remote_name;
|
2017-06-06 17:45:44 -07:00
|
|
|
Edi_Scm_Engine *engine = _edi_scm_global_object;
|
|
|
|
|
|
|
|
if (!engine)
|
|
|
|
return NULL;
|
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
if (!_remote_name)
|
|
|
|
_remote_name = _edi_scm_exec_response("git config --get user.name");
|
2017-06-06 17:45:44 -07:00
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
return _remote_name;
|
2017-06-06 17:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
_edi_scm_git_remote_email_get(void)
|
|
|
|
{
|
2017-06-07 14:32:10 -07:00
|
|
|
static char *_remote_email;
|
2017-06-06 17:45:44 -07:00
|
|
|
Edi_Scm_Engine *engine = _edi_scm_global_object;
|
|
|
|
|
|
|
|
if (!engine)
|
|
|
|
return NULL;
|
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
if (!_remote_email)
|
|
|
|
_remote_email = _edi_scm_exec_response("git config --get user.email");
|
2017-06-06 17:45:44 -07:00
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
return _remote_email;
|
2017-06-06 17:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
_edi_scm_git_remote_url_get(void)
|
|
|
|
{
|
2017-06-07 14:32:10 -07:00
|
|
|
static char *_remote_url;
|
2017-06-06 17:45:44 -07:00
|
|
|
Edi_Scm_Engine *engine = _edi_scm_global_object;
|
|
|
|
|
|
|
|
if (!engine)
|
|
|
|
return NULL;
|
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
if (!_remote_url)
|
|
|
|
_remote_url = _edi_scm_exec_response("git remote get-url origin");
|
2017-06-06 17:45:44 -07:00
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
return _remote_url;
|
2017-06-06 17:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-06-07 14:32:10 -07:00
|
|
|
_edi_scm_git_credentials_set(const char *name, const char *email)
|
2017-06-06 17:45:44 -07:00
|
|
|
{
|
|
|
|
int code;
|
|
|
|
Eina_Strbuf *command = eina_strbuf_new();
|
|
|
|
|
|
|
|
eina_strbuf_append_printf(command, "git config user.name '%s'", name);
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
eina_strbuf_reset(command);
|
|
|
|
eina_strbuf_append_printf(command, "git config user.email '%s'", email);
|
|
|
|
code = _edi_scm_exec(eina_strbuf_string_get(command));
|
|
|
|
|
|
|
|
eina_strbuf_free(command);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_edi_scm_enabled(Edi_Scm_Engine *engine)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
if (!engine) return EINA_FALSE;
|
|
|
|
|
|
|
|
if (!engine->path)
|
|
|
|
{
|
|
|
|
path = edi_path_append(edi_project_get(), engine->directory);
|
|
|
|
engine->path = eina_stringshare_add(path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ecore_file_exists(engine->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Bool
|
|
|
|
edi_scm_remote_enabled(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = _edi_scm_global_object;
|
|
|
|
if (!e)
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
return !!e->remote_url_get();
|
2017-06-06 17:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Bool
|
|
|
|
edi_scm_enabled(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *engine = _edi_scm_global_object;
|
|
|
|
if (!engine)
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
|
|
|
return _edi_scm_enabled(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Edi_Scm_Engine *
|
|
|
|
edi_scm_engine_get(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *engine = _edi_scm_global_object;
|
|
|
|
if (!engine)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
edi_scm_shutdown()
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *engine = _edi_scm_global_object;
|
|
|
|
|
|
|
|
if (!engine)
|
|
|
|
return;
|
|
|
|
|
|
|
|
eina_stringshare_del(engine->name);
|
|
|
|
eina_stringshare_del(engine->directory);
|
|
|
|
eina_stringshare_del(engine->path);
|
|
|
|
free(engine);
|
|
|
|
|
|
|
|
_edi_scm_global_object = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
edi_scm_add(const char *path)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
return e->file_add(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
edi_scm_del(const char *path)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
return e->file_del(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
edi_scm_move(const char *src, const char *dest)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
return e->move(src, dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_edi_scm_commit_thread_cb(void *data, Ecore_Thread *thread)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e;
|
|
|
|
const char *message = data;
|
|
|
|
|
|
|
|
e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
e->commit(message);
|
|
|
|
|
|
|
|
ecore_thread_cancel(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
edi_scm_commit(const char *message)
|
|
|
|
{
|
|
|
|
ecore_thread_run(_edi_scm_commit_thread_cb, NULL, NULL, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_edi_scm_status_thread_cb(void *data, Ecore_Thread *thread)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = data;
|
|
|
|
|
|
|
|
e->status();
|
|
|
|
|
|
|
|
ecore_thread_cancel(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
edi_scm_status(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
ecore_thread_run(_edi_scm_status_thread_cb, NULL, NULL, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
edi_scm_remote_add(const char *remote_url)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
return e->remote_add(remote_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_edi_scm_stash_thread_cb(void *data, Ecore_Thread *thread)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = data;
|
|
|
|
|
|
|
|
e->stash();
|
|
|
|
|
|
|
|
ecore_thread_cancel(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
edi_scm_stash(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
ecore_thread_run(_edi_scm_stash_thread_cb, NULL, NULL, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
2017-06-07 14:32:10 -07:00
|
|
|
edi_scm_credentials_set(const char *user, const char *email)
|
2017-06-06 17:45:44 -07:00
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
return e->credentials_set(user, email);
|
2017-06-06 17:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_edi_scm_pull_thread_cb(void *data, Ecore_Thread *thread)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = data;
|
|
|
|
|
|
|
|
e->pull();
|
|
|
|
|
|
|
|
ecore_thread_cancel(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
edi_scm_pull(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
ecore_thread_run(_edi_scm_pull_thread_cb, NULL, NULL, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_edi_scm_push_thread_cb(void *data, Ecore_Thread *thread)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = data;
|
|
|
|
|
|
|
|
e->push();
|
|
|
|
|
|
|
|
ecore_thread_cancel(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
edi_scm_push(void)
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *e = edi_scm_engine_get();
|
|
|
|
|
|
|
|
ecore_thread_run(_edi_scm_push_thread_cb, NULL, NULL, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Edi_Scm_Engine *
|
|
|
|
_edi_scm_git_init()
|
|
|
|
{
|
|
|
|
Edi_Scm_Engine *engine;
|
|
|
|
|
|
|
|
if (!ecore_file_app_installed("git"))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
_edi_scm_global_object = engine = calloc(1, sizeof(Edi_Scm_Engine));
|
|
|
|
engine->name = eina_stringshare_add("git");
|
|
|
|
engine->directory = eina_stringshare_add(".git");
|
|
|
|
engine->file_add = _edi_scm_git_file_add;
|
|
|
|
engine->file_mod = _edi_scm_git_file_mod;
|
|
|
|
engine->file_del = _edi_scm_git_file_del;
|
|
|
|
engine->move = _edi_scm_git_file_move;
|
|
|
|
engine->status = _edi_scm_git_status;
|
|
|
|
engine->commit = _edi_scm_git_commit;
|
|
|
|
engine->pull = _edi_scm_git_pull;
|
|
|
|
engine->push = _edi_scm_git_push;
|
|
|
|
engine->stash = _edi_scm_git_stash;
|
|
|
|
|
2017-06-07 14:32:10 -07:00
|
|
|
engine->remote_add = _edi_scm_git_remote_add;
|
|
|
|
engine->remote_name_get = _edi_scm_git_remote_name_get;
|
|
|
|
engine->remote_email_get = _edi_scm_git_remote_email_get;
|
|
|
|
engine->remote_url_get = _edi_scm_git_remote_url_get;
|
|
|
|
engine->credentials_set = _edi_scm_git_credentials_set;
|
2017-06-06 17:45:44 -07:00
|
|
|
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Edi_Scm_Engine *
|
|
|
|
edi_scm_init(void)
|
|
|
|
{
|
|
|
|
if (edi_project_file_exists(".git"))
|
|
|
|
return _edi_scm_git_init();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|