Compare commits
22 Commits
3c7b5d0958
...
master
Author | SHA1 | Date | |
---|---|---|---|
feeaab1ebe | |||
7aed1eed11 | |||
d8de4e070e | |||
fe46da5417 | |||
a3cbb20a53 | |||
1d119cb63e | |||
a336b507fb | |||
84c422ee38 | |||
4ab36ec64f | |||
7ba9cb8147 | |||
5338c30e79 | |||
2d8a947cb4 | |||
f3680754ec | |||
73340cbb7a | |||
65f72eca79 | |||
90853294da | |||
96994dc37e | |||
c2328b69f0 | |||
4101823f6e | |||
a1d59c78b6 | |||
34fe915e38 | |||
4e13dda29d |
@@ -10,6 +10,11 @@ unix {
|
||||
LIBS += -L/opt/ptu5/opt/build-ATBTariffCalculator-Desktop_Qt_5_12_12_GCC_64bit-Debug/CalculatorCInterface/ -lCalculatorCInterface
|
||||
}
|
||||
|
||||
win32 {
|
||||
INCLUDEPATH += C:\Users\G.Hoffmann\Downloads\libgit2-1.7.2\libgit2-1.7.2\include
|
||||
LIBS += -LC:\build-ATBTariffCalculator-Desktop_Qt_6_5_0_MinGW_64_bit-Release\CalculatorCInterface\release -lCalculatorCInterface
|
||||
}
|
||||
|
||||
# You can make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
@@ -26,15 +26,18 @@ int main(int argc, char *argv[])
|
||||
//setDebugLevel(LOG_NOTICE);
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
#if 1
|
||||
|
||||
//TariffCalculatorHandle handle = NewTariffCalculator();
|
||||
//DeleteTariffCalculator(handle);
|
||||
|
||||
if (InitGitLibrary() > 0) {
|
||||
qCritical() << CloneRepository("https://git.mimbach49.de/GerhardHoffmann/customer_999.git", "/tmp/customer_999");
|
||||
ShutdownGitLibrary();
|
||||
}
|
||||
//if (InitGitLibrary() > 0) {
|
||||
// qCritical() << CloneRepository("https://git.mimbach49.de/GerhardHoffmann/customer_999.git", "C:\\tmp\\customer_999");
|
||||
// qCritical() << CheckoutLocalBranch("C:\\tmp\\customer_999", "master");
|
||||
// ShutdownGitLibrary();
|
||||
// }
|
||||
|
||||
qCritical() << GetFileMenuSize("customer_999");
|
||||
#else
|
||||
QLibrary library("C:\\build-ATBTariffCalculator-Desktop_Qt_6_5_0_MinGW_64_bit-Release\\CalculatorCInterface\\release\\CalculatorCInterface.dll");
|
||||
if (library.load()) {
|
||||
|
@@ -2,12 +2,20 @@ QT+=core
|
||||
|
||||
TEMPLATE = lib
|
||||
DEFINES += CALCULATOR_C_INTERFACE_LIBRARY
|
||||
QMAKE_CXXFLAGS += -fPIC
|
||||
|
||||
|
||||
QMAKE_CXXFLAGS += -fPIC -std=c++20
|
||||
|
||||
unix {
|
||||
LIBS += -L/usr/lib64 -lgit2
|
||||
}
|
||||
|
||||
win32 {
|
||||
INCLUDEPATH += C:\Users\G.Hoffmann\Downloads\libgit2-1.7.2\libgit2-1.7.2\include
|
||||
|
||||
LIBS += -LC:\Users\G.Hoffmann\Downloads\libgit2-1.7.2\libgit2-1.7.2\build\Debug -lgit2
|
||||
}
|
||||
|
||||
# INCLUDEPATH+=$$_PRO_FILE_PWD_/../Utilities/
|
||||
|
||||
|
||||
@@ -20,12 +28,17 @@ CONFIG += c++20 console
|
||||
|
||||
SOURCES += \
|
||||
calculator_c_interface_lib.cpp \
|
||||
tariff_calculator.cpp
|
||||
git_library.cpp \
|
||||
tariff_calculator.cpp \
|
||||
local_git_repository.cpp
|
||||
|
||||
HEADERS += \
|
||||
calculator_c_interface_lib.h \
|
||||
calculator_c_interface_lib_global.h \
|
||||
tariff_calculator.h
|
||||
git_library.h \
|
||||
tariff_calculator.h \
|
||||
local_git_repository.h \
|
||||
global_defines.h
|
||||
|
||||
# Default rules for deployment.
|
||||
unix {
|
||||
|
@@ -1,13 +1,37 @@
|
||||
#include "calculator_c_interface_lib.h"
|
||||
#include "local_git_repository.h"
|
||||
#include "git_library.h"
|
||||
|
||||
//#include <git2/common.h>
|
||||
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QByteArray>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
#include <git2.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <algorithm>
|
||||
|
||||
//static QMap<QString, git_repository *> customerRepoMap;
|
||||
//static std::mutex m;
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
TariffCalculatorHandle NewTariffCalculator(void) {
|
||||
return new TariffCalculator();
|
||||
}
|
||||
@@ -16,33 +40,107 @@ void DeleteTariffCalculator(TariffCalculatorHandle handle) {
|
||||
delete handle;
|
||||
}
|
||||
|
||||
int InitGitLibrary(void) {
|
||||
return git_libgit2_init();
|
||||
int InitGitLibraryInternal(void) {
|
||||
return GitLibrary::Init();
|
||||
}
|
||||
|
||||
int ShutdownGitLibrary(void) {
|
||||
return git_libgit2_shutdown();
|
||||
int ShutdownGitLibraryInternal(void) {
|
||||
LocalGitRepository::DestroyAllRepositories();
|
||||
return GitLibrary::Shutdown();
|
||||
}
|
||||
|
||||
int CloneRepository(char const *url, char const *local_path) {
|
||||
git_repository *out;
|
||||
git_clone_options opts;
|
||||
int CloneRepositoryInternal(char const *url, char const *local_path) {
|
||||
return GitLibrary::CloneRepository(url, local_path);
|
||||
}
|
||||
|
||||
int res = 0;
|
||||
if ((res = git_clone_options_init(&opts, GIT_CLONE_OPTIONS_VERSION)) == 0) {
|
||||
opts.checkout_branch = "master";
|
||||
int CheckoutRepositoryInternal(char const *url, char const *localRepoName, char const *branchName) {
|
||||
return GitLibrary::CheckoutRepository(url, localRepoName, branchName);
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s:%d %s %s\n", __func__, __LINE__, url, local_path);
|
||||
int CommitFileInternal(char const *localRepoName, char const *branchName,
|
||||
char const *fName, char const *commitMessage) {
|
||||
return GitLibrary::CommitFile(localRepoName, branchName, fName, commitMessage);
|
||||
}
|
||||
|
||||
if ((res = git_clone(&out, url, local_path, &opts)) < 0) {
|
||||
git_error const *error = git_error_last();
|
||||
fprintf(stderr, "%s:%d error: %s\n", __func__, __LINE__, error->message);
|
||||
int PushRepositoryInternal(char const *localRepoName, char const *branchName,
|
||||
char const *userName, char const *userPassword) {
|
||||
return GitLibrary::PushRepository(localRepoName, branchName, userName, userPassword);
|
||||
}
|
||||
|
||||
int PullRepositoryInternal(char const *localRepoName, char const *remoteRepoName) {
|
||||
return GitLibrary::PullRepository(localRepoName, remoteRepoName);
|
||||
}
|
||||
|
||||
#include <local_git_repository.h>
|
||||
|
||||
|
||||
void SetReposRootDirectoryInternal(char const *p) {
|
||||
LocalGitRepository::SetReposRootDirectory(QString::fromUtf8(p));
|
||||
}
|
||||
|
||||
char const *GetReposRootDirectoryInternal() {
|
||||
return (char const *)LocalGitRepository::GetReposRootDirectory().constData();
|
||||
}
|
||||
|
||||
char const *GetLocalRepositoryPathInternal(char const *localGitRepo) {
|
||||
return (char const *)LocalGitRepository::GetInstance(localGitRepo)->localRepositoryPath().constData();
|
||||
}
|
||||
|
||||
int32_t GetFileMenuSizeInternal(char const *localGitRepo) {
|
||||
return LocalGitRepository::GetInstance(localGitRepo)->GetFileMenuSizeInternal();
|
||||
}
|
||||
|
||||
char const *GetFileMenuInternal(const char *localGitRepo) {
|
||||
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileMenuInternal().constData();
|
||||
if (a.isValidUtf8()) {
|
||||
int const len = GetFileMenuSizeInternal(localGitRepo);
|
||||
if (len > 0) {
|
||||
char *json = new char [len+1];
|
||||
// fprintf(stderr, "allocate pointer %p\n", json);
|
||||
memset(json, 0x00, len+1);
|
||||
memcpy(json, a.constData(), std::min(len, (int)a.size()));
|
||||
return json;
|
||||
}
|
||||
} else {
|
||||
git_error const *error = git_error_last();
|
||||
fprintf(stderr, "%s:%d error: %s\n", __func__, __LINE__, error->message);
|
||||
}
|
||||
return res;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
char const *GetFileNameInternal(char const *localGitRepo, char const *fileId) {
|
||||
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileNameInternal(fileId);
|
||||
if (a.isValidUtf8()) {
|
||||
char *c = new char[a.size() + 1];
|
||||
memset(c, 0x00, a.size() + 1);
|
||||
memcpy(c, a.constData(), a.size());
|
||||
return c;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int32_t GetFileSize(char const *localGitRepo, char const *fileId) {
|
||||
return LocalGitRepository::GetInstance(localGitRepo)->GetFileSize(fileId);
|
||||
}
|
||||
|
||||
char const *GetFileInternal(char const *localGitRepo, char const *fileId) {
|
||||
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileInternal(fileId);
|
||||
if (a.isValidUtf8()) {
|
||||
char *c = new char[a.size() + 1];
|
||||
memset(c, 0x00, a.size() + 1);
|
||||
memcpy(c, a.constData(), a.size());
|
||||
return c;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool SetFileInternal(char const *localGitRepo, char const *fileId, char const *json, int size) {
|
||||
return LocalGitRepository::GetInstance(localGitRepo)->SetFileInternal(QString(fileId), QByteArray(json, size));
|
||||
}
|
||||
|
||||
void DeleteMem(char *p) {
|
||||
if (p) {
|
||||
// fprintf(stderr, "delete pointer %p\n", p);
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#ifndef CALCULATOR_C_INTERFACE_LIB_H_INCLUDED
|
||||
#define CALCULATOR_C_INTERFACE_LIB_H_INCLUDED
|
||||
|
||||
#include "calculator_c_interface_lib_global.h"
|
||||
#include "tariff_calculator.h"
|
||||
|
||||
@@ -10,16 +9,31 @@ typedef TariffCalculator *TariffCalculatorHandle;
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void DeleteMem(char *p) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
void SetReposRootDirectoryInternal(char const *p) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
char const *GetReposRootDirectoryInternal() CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
char const *GetLocalRepositoryPathInternal(char const *localRepo) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
// interface for menu of webpage
|
||||
char const *GetFileMenuInternal(char const *localRepo) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int32_t GetFileMenuSizeInternal(char const *localRepo) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
char const *GetFileNameInternal(char const *localRepo, char const *fileId) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int32_t GetFileSize(char const *localRepo, char const *fileId) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
char const *GetFileInternal(char const *localRepo, char const *fileId) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
bool SetFileInternal(char const *localRepo, char const *fileId, char const *json, int size) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
TariffCalculatorHandle NewTariffCalculator(void) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
void DeleteTariffCalculator(TariffCalculatorHandle handle) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
int InitGitLibrary(void) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int ShutdownGitLibrary(void) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
int CloneRepository(char const *url, char const *local_path) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
|
||||
void DeleteTariffCalculator(TariffCalculatorHandle handle) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
// libgit2
|
||||
int InitGitLibraryInternal(void) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int ShutdownGitLibraryInternal(void) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int CloneRepositoryInternal(char const *url, char const *local_path) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int CheckoutRepositoryInternal(char const *url, char const *localRepoName, char const *branchName) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int CommitFileInternal(char const *local_path, char const *branch_name, char const *file_name, char const *commit_message) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int PushRepositoryInternal(char const *local_path, char const *branch_name, char const *user, char const *password) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int PullRepositoryInternal(char const *localRepoName, char const *remoteRepoName) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
1025
CalculatorCInterface/git_library.cpp
Normal file
1025
CalculatorCInterface/git_library.cpp
Normal file
File diff suppressed because it is too large
Load Diff
47
CalculatorCInterface/git_library.h
Normal file
47
CalculatorCInterface/git_library.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef GIT_LIBRARY_H_INCLUDED
|
||||
#define GIT_LIBRARY_H_INCLUDED
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QDir>
|
||||
#include <git2.h>
|
||||
|
||||
class CWD {
|
||||
QDir m_prev;
|
||||
QDir m_localRepoDir;
|
||||
QDir m_localRepoGitDir;
|
||||
bool m_valid;
|
||||
public:
|
||||
CWD(QString const &localRepoName);
|
||||
~CWD();
|
||||
|
||||
QDir const &localRepoDir() const { return m_localRepoDir; }
|
||||
QDir &localRepoDir() { return m_localRepoDir; }
|
||||
QDir const &localRepoGitDir() const { return m_localRepoGitDir; }
|
||||
QDir &localRepoGitDir() { return m_localRepoGitDir; }
|
||||
};
|
||||
|
||||
class GitLibrary {
|
||||
static QString m_userName;
|
||||
static QString m_userPassword;
|
||||
public:
|
||||
static QString &userName() { return m_userName; }
|
||||
static QString &userPassword() { return m_userPassword; }
|
||||
|
||||
static int Init();
|
||||
static int Shutdown();
|
||||
static int CloneRepository(char const *url, char const *localRepoName);
|
||||
static int CheckoutRepository(char const *url, char const *localRepoName, char const *branchName);
|
||||
static int CommitRepository(char const *localRepoName, char const *branchName, char const *commitMessage);
|
||||
static int CommitFile(char const *localRepoName, char const *branchName, char const *fName, char const *commitMessage);
|
||||
static int PushRepository(char const *localRepoName, char const *branchName, char const *userName, char const *userPassword);
|
||||
static int PullRepository(char const *localRepoName, char const *remoteRepoName);
|
||||
|
||||
// explicit GitLibrary();
|
||||
// ~GitLibrary();
|
||||
};
|
||||
|
||||
#endif // #define GIT_LIBRARY_H_INCLUDED
|
7
CalculatorCInterface/global_defines.h
Normal file
7
CalculatorCInterface/global_defines.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef GLOBAL_DEFINES_H_INCLUDED
|
||||
#define GLOBAL_DEFINES_H_INCLUDED
|
||||
|
||||
#define HEADER __func__ << "(" << __LINE__ << ")"
|
||||
|
||||
#endif // GLOBAL_DEFINES_H_INCLUDED
|
||||
|
212
CalculatorCInterface/local_git_repository.cpp
Normal file
212
CalculatorCInterface/local_git_repository.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#include "local_git_repository.h"
|
||||
#include "git_library.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QJsonDocument>
|
||||
#include <QByteArray>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
#include <QFile>
|
||||
|
||||
|
||||
QString LocalGitRepository::repoRootDirectory = "";
|
||||
QMap<QString, LocalGitRepository *> LocalGitRepository::localGitRepos;
|
||||
|
||||
QMap<QString, LocalGitRepository *> &LocalGitRepository::GetLocalGitRepos() {
|
||||
return localGitRepos;
|
||||
}
|
||||
|
||||
void LocalGitRepository::SetReposRootDirectory(QString s) {
|
||||
if (repoRootDirectory.isEmpty()) {
|
||||
repoRootDirectory = s;
|
||||
return;
|
||||
}
|
||||
qCritical() << __func__ << ":" << __LINE__
|
||||
<< "ERROR git-repository-root-directory already set"
|
||||
<< repoRootDirectory;
|
||||
}
|
||||
|
||||
QString LocalGitRepository::GetReposRootDirectory() {
|
||||
return repoRootDirectory;
|
||||
}
|
||||
|
||||
LocalGitRepository::LocalGitRepository(QString const &localRepository)
|
||||
: m_localRepository(localRepository)
|
||||
, m_fileMenu("{}")
|
||||
, m_fileMenuSize(-1)
|
||||
, m_git_repository(nullptr) {
|
||||
}
|
||||
|
||||
LocalGitRepository::~LocalGitRepository() {
|
||||
}
|
||||
|
||||
void LocalGitRepository::SetGitRepository(git_repository *git_repo) {
|
||||
m_git_repository = git_repo;
|
||||
}
|
||||
|
||||
git_repository const *LocalGitRepository::GetGitRepository() const {
|
||||
return m_git_repository;
|
||||
}
|
||||
|
||||
git_repository *LocalGitRepository::GetGitRepository() {
|
||||
return m_git_repository;
|
||||
}
|
||||
|
||||
QString LocalGitRepository::localRepositoryName() const { // e.g. customer_999
|
||||
return m_localRepository;
|
||||
}
|
||||
|
||||
QString LocalGitRepository::localRepositoryPath() const {
|
||||
if (!repoRootDirectory.isEmpty()) {
|
||||
return QDir::cleanPath(repoRootDirectory + QDir::separator() + m_localRepository);
|
||||
}
|
||||
qCritical() << __func__ << ":" << __LINE__
|
||||
<< "ERROR git-repository-root-directory not set";
|
||||
return "";
|
||||
}
|
||||
|
||||
QString LocalGitRepository::localRepositoryPath(QString const &localRepository) {
|
||||
if (!repoRootDirectory.isEmpty()) {
|
||||
return QDir::cleanPath(repoRootDirectory + QDir::separator() + localRepository);
|
||||
}
|
||||
qCritical() << __func__ << ":" << __LINE__
|
||||
<< "ERROR git-repository-root-directory not set";
|
||||
return "";
|
||||
}
|
||||
|
||||
LocalGitRepository *LocalGitRepository::GetInstance(QString const& localRepository) {
|
||||
LocalGitRepository *repo = nullptr;
|
||||
if (GetLocalGitRepos().count(localRepository) > 0) {
|
||||
repo = GetLocalGitRepos()[localRepository];
|
||||
} else {
|
||||
repo = new LocalGitRepository(localRepository);
|
||||
qCritical() << "created local git-repository" << localRepository;
|
||||
GetLocalGitRepos().insert(localRepository, repo);
|
||||
}
|
||||
if (repo == nullptr) {
|
||||
qCritical() << __func__ << ":" << __LINE__
|
||||
<< "ERROR: could not find local git-repository" << localRepository;
|
||||
}
|
||||
return repo;
|
||||
}
|
||||
|
||||
bool LocalGitRepository::DestroyInstance(QString const &localRepository) {
|
||||
if (GetLocalGitRepos().count(localRepository) > 0) {
|
||||
LocalGitRepository *repo = GetLocalGitRepos().take(localRepository);
|
||||
delete repo;
|
||||
qCritical() << "deleted local git-repository" << localRepository;
|
||||
return true;
|
||||
}
|
||||
qCritical() << __func__ << ":" << __LINE__
|
||||
<< "ERROR: could not find local git-repository" << localRepository;
|
||||
return false;
|
||||
}
|
||||
|
||||
void LocalGitRepository::DestroyAllRepositories() {
|
||||
for (auto it = GetLocalGitRepos().keyValueBegin(); it != GetLocalGitRepos().keyValueEnd(); ++it) {
|
||||
QString const &key = it->first;
|
||||
LocalGitRepository *repo = GetLocalGitRepos()[key];
|
||||
delete repo;
|
||||
qCritical() << "deleted local git-repository" << key;
|
||||
}
|
||||
GetLocalGitRepos().clear();
|
||||
}
|
||||
|
||||
int32_t LocalGitRepository::GetFileMenuSizeInternal() const {
|
||||
return m_fileMenuSize;
|
||||
}
|
||||
|
||||
QByteArray LocalGitRepository::GetFileMenuInternal() {
|
||||
if (m_fileMenuSize == -1) {
|
||||
QFile f(QDir::cleanPath(repoRootDirectory + QDir::separator()
|
||||
+ m_localRepository + QDir::separator()
|
||||
+ "etc/psa_webinterface/menu_config.json"));
|
||||
if (f.exists()) {
|
||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
m_fileMenu = QTextStream(&f).readAll().toUtf8();
|
||||
|
||||
if (m_fileMenu.isValidUtf8()) {
|
||||
QString const &s = QString::fromUtf8(m_fileMenu);
|
||||
m_fileMenuSize = s.toLocal8Bit().size();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m_fileMenu;
|
||||
}
|
||||
|
||||
QByteArray LocalGitRepository::GetFileNameInternal(QString const &fId) {
|
||||
QRegularExpressionMatch match;
|
||||
static const QRegularExpression re("(master|[0-9]+)");
|
||||
if (fId.lastIndexOf(re, -1, &match) != -1) {
|
||||
int idx = fId.indexOf("/");
|
||||
if (idx != -1) {
|
||||
QString path = fId.mid(idx);
|
||||
idx = path.indexOf(":");
|
||||
if (idx != -1) {
|
||||
path = path.mid(0, idx);
|
||||
QString s = match.captured(match.lastCapturedIndex());
|
||||
if (s != "master") {
|
||||
if (fId.contains("psa_tariff")) {
|
||||
QString fn(QDir::cleanPath(
|
||||
repoRootDirectory + QDir::separator() +
|
||||
QString(m_localRepository) + QDir::separator()
|
||||
+ path + QDir::separator()
|
||||
+ QString("tariff%1.json").arg(s.toUInt(), 2, 10, QChar('0'))));
|
||||
return fn.toUtf8();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
QByteArray LocalGitRepository::GetFileInternal(QString const &fId) {
|
||||
QByteArray a = GetFileNameInternal(fId);
|
||||
if (a.isValidUtf8()) {
|
||||
QFile fn(a);
|
||||
if (fn.exists()) {
|
||||
if (fn.open(QIODevice::ReadOnly)) {
|
||||
return fn.readAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QByteArray("{}");
|
||||
}
|
||||
|
||||
int32_t LocalGitRepository::GetFileSize(QString const &fId) {
|
||||
QByteArray a = GetFileNameInternal(fId);
|
||||
if (a.isValidUtf8()) {
|
||||
QFile fn(a);
|
||||
if (fn.exists()) {
|
||||
return fn.size();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool LocalGitRepository::SetFileInternal(QString const &fId, QByteArray const &json) {
|
||||
QByteArray a = GetFileNameInternal(fId);
|
||||
if (a.isValidUtf8()) {
|
||||
QFile fn(a);
|
||||
if (fn.exists()) {
|
||||
if (fn.open(QIODevice::WriteOnly)) {
|
||||
qint64 bytesWritten = 0;
|
||||
qint64 bytesToWrite = json.size();
|
||||
while (bytesToWrite > 0 &&
|
||||
(bytesWritten = fn.write(json.constData(), bytesToWrite)) != -1) {
|
||||
bytesToWrite -= bytesWritten;
|
||||
}
|
||||
fn.flush();
|
||||
return (bytesToWrite == 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
58
CalculatorCInterface/local_git_repository.h
Normal file
58
CalculatorCInterface/local_git_repository.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef LOCAL_GIT_REPOSITORY_H_INCLUDED
|
||||
#define LOCAL_GIT_REPOSITORY_H_INCLUDED
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QDir>
|
||||
#include <QMap>
|
||||
|
||||
#include <git2.h>
|
||||
|
||||
class GitLibrary;
|
||||
class LocalGitRepository {
|
||||
friend class GitLibrary;
|
||||
QString m_localRepository;
|
||||
mutable QByteArray m_fileMenu;
|
||||
mutable int32_t m_fileMenuSize;
|
||||
git_repository *m_git_repository;
|
||||
|
||||
static QString repoRootDirectory;
|
||||
static QMap<QString, LocalGitRepository *> localGitRepos;
|
||||
|
||||
protected: // force heap-based objects
|
||||
LocalGitRepository(QString const &localRepository);
|
||||
~LocalGitRepository();
|
||||
|
||||
public:
|
||||
static void SetReposRootDirectory(QString s);
|
||||
static QString GetReposRootDirectory();
|
||||
|
||||
static LocalGitRepository *GetInstance(QString const &localRepository);
|
||||
static bool DestroyInstance(QString const &localRepository);
|
||||
static void DestroyAllRepositories();
|
||||
|
||||
static QMap<QString, LocalGitRepository *> &GetLocalGitRepos();
|
||||
|
||||
QString localRepositoryName() const;
|
||||
QString localRepositoryPath() const;
|
||||
|
||||
void SetGitRepository(git_repository *git_repo);
|
||||
git_repository const *GetGitRepository() const;
|
||||
git_repository *GetGitRepository();
|
||||
|
||||
static QString localRepositoryPath(QString const &localRepository);
|
||||
|
||||
QByteArray GetFileMenuInternal();
|
||||
int32_t GetFileMenuSizeInternal() const;
|
||||
|
||||
QByteArray GetFileNameInternal(QString const &fileId);
|
||||
int32_t GetFileSize(QString const &fileId);
|
||||
QByteArray GetFileInternal(QString const &fileId);
|
||||
|
||||
bool SetFileInternal(QString const &fileId, QByteArray const &json);
|
||||
};
|
||||
|
||||
#endif // LOCAL_GIT_REPOSITORY_H_INCLUDED
|
BIN
windows/git2.dll
Normal file
BIN
windows/git2.dll
Normal file
Binary file not shown.
BIN
windows/git2.exp
Normal file
BIN
windows/git2.exp
Normal file
Binary file not shown.
BIN
windows/git2.lib
Normal file
BIN
windows/git2.lib
Normal file
Binary file not shown.
BIN
windows/git2.pdb
Normal file
BIN
windows/git2.pdb
Normal file
Binary file not shown.
35
windows/helloworld.lpr
Normal file
35
windows/helloworld.lpr
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
|
||||
{%RunFlags MESSAGES+}
|
||||
{$mode ObjFPC}{$H-}
|
||||
program helloworld(output);
|
||||
|
||||
|
||||
uses
|
||||
TariffCalculator, CTypes;
|
||||
|
||||
var
|
||||
TariffCalc: TariffCalculatorHandle;
|
||||
CustomerRepo: array[0..100] of char;
|
||||
LocalCustomerRepo: array[0..100] of char;
|
||||
|
||||
begin
|
||||
writeLn('Hello, World!') ;
|
||||
// TariffCalc := NewTariffCalculator;
|
||||
// DeleteTariffCalculator(TariffCalc);
|
||||
if InitGitLibrary() > 0 then
|
||||
begin
|
||||
writeLn('init OK!') ;
|
||||
CustomerRepo := 'https://git.mimbach49.de/GerhardHoffmann/customer_999.git';
|
||||
LocalCustomerRepo := 'C:\\tmp\\customer_999';
|
||||
CloneRepository(CustomerRepo, LocalCustomerRepo);
|
||||
|
||||
ShutdownGitLibrary();
|
||||
end
|
||||
else
|
||||
begin
|
||||
writeLn('init NOT OK!') ;
|
||||
Readln;
|
||||
end;
|
||||
end.
|
||||
|
35
windows/tariffcalculator.pas
Normal file
35
windows/tariffcalculator.pas
Normal file
@@ -0,0 +1,35 @@
|
||||
unit TariffCalculator;
|
||||
|
||||
{$mode ObjFPC}{$H-}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, CTypes;
|
||||
|
||||
type
|
||||
// Can't use the class directly, so it is treated as an opaque handle.
|
||||
// THandle is guaranteed to have the right size, even on other platforms.
|
||||
TariffCalculatorHandle = THandle;
|
||||
|
||||
function NewTariffCalculator: TariffCalculatorHandle; stdcall;
|
||||
procedure DeleteTariffCalculator(handle: TariffCalculatorHandle); stdcall;
|
||||
|
||||
function InitGitLibrary: cint32; stdcall;
|
||||
function ShutdownGitLibrary: cint32; stdcall;
|
||||
function CloneRepository(var url; var local_path) : cint32; stdcall;
|
||||
|
||||
implementation
|
||||
|
||||
const
|
||||
DLLName = 'CalculatorCInterface.dll';
|
||||
|
||||
function NewTariffCalculator: TariffCalculatorHandle; stdcall; external DLLName;
|
||||
procedure DeleteTariffCalculator(handle: TariffCalculatorHandle); stdcall; external DLLName;
|
||||
|
||||
function InitGitLibrary: cint32; stdcall; external DLLName;
|
||||
function ShutdownGitLibrary: cint32; stdcall; external DLLName;
|
||||
function CloneRepository(var url; var local_path) : cint32; stdcall; external DLLName;
|
||||
|
||||
end.
|
||||
|
Reference in New Issue
Block a user