Compare commits

...

20 Commits

Author SHA1 Message Date
feeaab1ebe pull = fetch/merge 2024-04-15 16:58:00 +02:00
7aed1eed11 changed parameters 2024-04-15 16:44:42 +02:00
d8de4e070e PullRepository(): call fetch and merge 2024-04-15 16:41:11 +02:00
fe46da5417 format code 2024-04-05 12:44:34 +02:00
a3cbb20a53 at least it compiles 2024-04-05 12:13:27 +02:00
1d119cb63e continued merge 2024-04-05 11:41:26 +02:00
a336b507fb forglobal defines 2024-04-05 11:10:12 +02:00
84c422ee38 just safe again 2024-04-03 16:40:46 +02:00
4ab36ec64f just safe 2024-04-03 16:29:58 +02:00
7ba9cb8147 ostern ... 2024-03-28 16:45:46 +01:00
5338c30e79 saving ... 2024-03-27 08:40:16 +01:00
2d8a947cb4 Adapt to new atb-units 2024-03-25 16:13:26 +01:00
f3680754ec save for the weekend 2024-03-22 13:46:54 +01:00
73340cbb7a just save 2024-03-21 16:57:53 +01:00
65f72eca79 own class for git-repo 2024-03-21 16:57:02 +01:00
90853294da add PushLocalRepository() 2024-03-14 15:04:06 +01:00
96994dc37e CommitFile() 2024-03-13 16:09:12 +01:00
c2328b69f0 added CheckoutBranch() 2024-03-13 13:17:02 +01:00
4101823f6e win stuff 2024-03-11 16:41:33 +01:00
a1d59c78b6 Use H- 2024-03-11 16:39:14 +01:00
11 changed files with 1541 additions and 34 deletions

View File

@@ -31,10 +31,13 @@ int main(int argc, char *argv[])
//TariffCalculatorHandle handle = NewTariffCalculator();
//DeleteTariffCalculator(handle);
if (InitGitLibrary() > 0) {
qCritical() << CloneRepository("https://git.mimbach49.de/GerhardHoffmann/customer_999.git", "C:\\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()) {

View File

@@ -2,7 +2,9 @@ QT+=core
TEMPLATE = lib
DEFINES += CALCULATOR_C_INTERFACE_LIBRARY
QMAKE_CXXFLAGS += -fPIC
QMAKE_CXXFLAGS += -fPIC -std=c++20
unix {
LIBS += -L/usr/lib64 -lgit2
@@ -10,6 +12,7 @@ unix {
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
}
@@ -25,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 {

View File

@@ -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 res = 0;
if ((res = git_clone_options_init(&opts, GIT_CLONE_OPTIONS_VERSION)) == 0) {
opts.checkout_branch = "master";
fprintf(stderr, "%s:%d %s %s\n", __func__, __LINE__, url, local_path);
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 CloneRepositoryInternal(char const *url, char const *local_path) {
return GitLibrary::CloneRepository(url, local_path);
}
} else {
git_error const *error = git_error_last();
fprintf(stderr, "%s:%d error: %s\n", __func__, __LINE__, error->message);
int CheckoutRepositoryInternal(char const *url, char const *localRepoName, char const *branchName) {
return GitLibrary::CheckoutRepository(url, localRepoName, branchName);
}
int CommitFileInternal(char const *localRepoName, char const *branchName,
char const *fName, char const *commitMessage) {
return GitLibrary::CommitFile(localRepoName, branchName, fName, commitMessage);
}
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;
}
}
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;
}
return res;
}
#ifdef __cplusplus

View File

@@ -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
}

File diff suppressed because it is too large Load Diff

View 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

View File

@@ -0,0 +1,7 @@
#ifndef GLOBAL_DEFINES_H_INCLUDED
#define GLOBAL_DEFINES_H_INCLUDED
#define HEADER __func__ << "(" << __LINE__ << ")"
#endif // GLOBAL_DEFINES_H_INCLUDED

View 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;
}

View 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

35
windows/helloworld.lpr Normal file
View 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.

View File

@@ -1,6 +1,6 @@
unit TariffCalculator;
{$mode ObjFPC}{$H+}
{$mode ObjFPC}{$H-}
interface