A little better build but I really need to clean this up and use a state machine to handle the process.

master
Zed A. Shaw 2 months ago
parent 93b181cdbc
commit 899e481c9d
  1. 2
      .vimrc_proj
  2. 3
      meson.build
  3. 26
      regtest.cpp
  4. 102
      watchgit.cpp

@ -1 +1 @@
set makeprg=meson\ compile\ -C \builddir set makeprg=meson\ compile\ -C\ .

@ -24,3 +24,6 @@ dependencies = [
executable('watchgit', 'watchgit.cpp', executable('watchgit', 'watchgit.cpp',
dependencies: dependencies) dependencies: dependencies)
executable('regtest', 'regtest.cpp',
dependencies: [fmt])

@ -0,0 +1,26 @@
#include <fmt/core.h>
#include <regex>
#include <string>
using namespace fmt;
using namespace std;
int main(int argc, char *argv[]) {
smatch matches;
regex to_test(argv[1]);
string line(argv[2]);
if(argc != 3) {
println("USAGE: regtest <regex> <line>");
} else {
if(regex_match(line, matches, to_test)) {
println("MATCHED: ");
for(auto &match : matches) {
println("\t{}", match.str());
}
} else {
println("NO MATCH");
}
}
}

@ -12,6 +12,7 @@
#include <git2.h> #include <git2.h>
#include <efsw/efsw.hpp> #include <efsw/efsw.hpp>
#include <regex> #include <regex>
#include <set>
using namespace std; using namespace std;
using namespace fmt; using namespace fmt;
@ -43,32 +44,51 @@ const char *unfuck_path(const git_status_entry *entry) {
return nullptr; return nullptr;
} }
void add_status(const git_status_entry *entry, unsigned int status_flags, vector<string> &updates) { void add_status(const git_status_entry *entry, unsigned int status_flags, set<string> &updates) {
const char *path = unfuck_path(entry); const char *path = unfuck_path(entry);
if(status_flags & GIT_STATUS_WT_NEW if(status_flags & GIT_STATUS_WT_NEW
|| status_flags & GIT_STATUS_INDEX_NEW) || status_flags & GIT_STATUS_INDEX_NEW)
{ {
updates.push_back(string{path}); updates.insert(string{path});
} }
if(status_flags & GIT_STATUS_WT_MODIFIED if(status_flags & GIT_STATUS_WT_MODIFIED
|| status_flags & GIT_STATUS_INDEX_MODIFIED) || status_flags & GIT_STATUS_INDEX_MODIFIED)
{ {
updates.push_back(string{path}); updates.insert(string{path});
} }
// need to confirm this gets the new name // need to confirm this gets the new name
if(status_flags & GIT_STATUS_WT_RENAMED if(status_flags & GIT_STATUS_WT_RENAMED
|| status_flags & GIT_STATUS_INDEX_RENAMED) || status_flags & GIT_STATUS_INDEX_RENAMED)
{ {
updates.push_back(string{path}); updates.insert(string{path});
} }
} }
void list_git_changes(set <string> &updates, git_repository* repo) {
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *statuses = nullptr;
//TODO: does this leak?
int err = git_status_list_new(&statuses, repo, &opts);
dbc::check(err == 0, git_error_last()->message);
size_t count = git_status_list_entrycount(statuses);
for(size_t i=0; i < count; i++) {
const git_status_entry *entry = git_status_byindex(statuses, i);
add_status(entry, entry->status, updates);
}
}
class UpdateListener : public efsw::FileWatchListener { class UpdateListener : public efsw::FileWatchListener {
public: public:
bool changes = false; bool changes = false;
git_repository* repo = nullptr;
UpdateListener(git_repository *r) : repo(r) {};
void handleFileAction(efsw::WatchID watchid, void handleFileAction(efsw::WatchID watchid,
const std::string& dir, const std::string& dir,
@ -76,25 +96,11 @@ class UpdateListener : public efsw::FileWatchListener {
efsw::Action action, efsw::Action action,
std::string oldFilename) override std::string oldFilename) override
{ {
changes = true; set<string> updates;
list_git_changes(updates, repo);
switch(action) { bool in_git = updates.contains(filename);
case efsw::Actions::Add: // this makes it so we only get it set one time when somethign is in git
print("ADD {} {} {}\n", dir, filename, oldFilename); changes = changes || in_git;
break;
case efsw::Actions::Delete:
print("DEL {} {} {}\n", dir, filename, oldFilename);
break;
case efsw::Actions::Modified:
print("MOD {} {} {}\n", dir, filename, oldFilename);
break;
case efsw::Actions::Moved:
print("MOV {} {} {}\n", dir, filename, oldFilename);
break;
default:
dbc::sentinel("Unknown efsw action.");
}
} }
void reset_state() { void reset_state() {
@ -102,31 +108,10 @@ class UpdateListener : public efsw::FileWatchListener {
} }
}; };
void list_git_changes(git_repository* repo) {
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *statuses = nullptr;
//TODO: does this leak?
int err = git_status_list_new(&statuses, repo, &opts);
dbc::check(err == 0, git_error_last()->message);
size_t count = git_status_list_entrycount(statuses);
vector<string> updates;
for(size_t i=0; i < count; i++) {
const git_status_entry *entry = git_status_byindex(statuses, i);
add_status(entry, entry->status, updates);
}
for(string path : updates) {
print("PATH {}\n", path);
}
}
void run_build(const char* command) { void run_build(const char* command) {
regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)"); regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
char buffer[BUF_MAX]; // BUF_MAX is a define already? char buffer[BUF_MAX]; // BUF_MAX is a define already?
smatch err;
ofstream stats_out; ofstream stats_out;
stats_out.open("stats.csv", ios::out | ios::app); stats_out.open("stats.csv", ios::out | ios::app);
auto t = time(nullptr); auto t = time(nullptr);
@ -141,16 +126,23 @@ void run_build(const char* command) {
while(fgets(buffer, BUF_MAX, build_out) != nullptr) { while(fgets(buffer, BUF_MAX, build_out) != nullptr) {
string line(buffer); // yeah, that's probably a problem string line(buffer); // yeah, that's probably a problem
print("{}\n", line); smatch err;
if(regex_match(line, err, err_re)) { bool match = regex_match(line, err, err_re);
if(match) {
string file_name = err[1].str(); string file_name = err[1].str();
string line = err[2].str(); string lnumber = err[2].str();
string col = err[3].str(); string col = err[3].str();
string type = err[4].str(); string type = err[4].str();
string message = err[5].str(); string message = err[5].str();
stats_out << put_time(&tm, "%FT%TZ"); stats_out << put_time(&tm, "%FT%TZ");
stats_out << format(",{},{},{},{},{}\n", file_name, line, col, type, message); string result = format(",{},{},{},{},{}\n", file_name, lnumber, col, type, message);
stats_out << result;
cout << "MATCHED: " << quoted(result);
} else {
//println("NOT MATCHED! {}", line);
} }
} }
@ -168,7 +160,7 @@ int main(int argc, char *argv[])
const char *git_path = argv[1]; const char *git_path = argv[1];
const char *build_cmd = argv[2]; const char *build_cmd = argv[2];
print("Using build command: {}", build_cmd); println("Using build command: {}", build_cmd);
efsw::FileWatcher* fileWatcher = new efsw::FileWatcher(); efsw::FileWatcher* fileWatcher = new efsw::FileWatcher();
dbc::check(fileWatcher != nullptr, "Failed to create filewatcher."); dbc::check(fileWatcher != nullptr, "Failed to create filewatcher.");
@ -177,22 +169,22 @@ int main(int argc, char *argv[])
int err = git_repository_open(&repo, git_path); int err = git_repository_open(&repo, git_path);
dbc::check(err == 0, git_error_last()->message); dbc::check(err == 0, git_error_last()->message);
UpdateListener* listener = new UpdateListener(); UpdateListener* listener = new UpdateListener(repo);
dbc::check(listener != nullptr, "Failed to create listener."); dbc::check(listener != nullptr, "Failed to create listener.");
print("Watching directory {} for changes...\n", git_path); print("Watching directory {} for changes...\n", git_path);
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true); efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
while(true) { while(true) {
print("WAITING...\r");
fileWatcher->watch(); fileWatcher->watch();
if(listener->changes) { if(listener->changes) {
sleep(1); sleep(1);
list_git_changes(repo); println("CHANGES! Running build {}", build_cmd);
listener->reset_state();
run_build(build_cmd); run_build(build_cmd);
listener->reset_state();
} }
sleep(1); sleep(1);
} }

Loading…
Cancel
Save