1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 13:35:07 +00:00
serenity/DevTools/HackStudio/Git/GitRepo.cpp
Itamar 435c6c6f96 HackStudio: Add basic Git integration
This adds a "Git" tab to Hackstudio.
Currently has support for staging and unstaging files.
2020-09-15 21:43:29 +02:00

131 lines
4 KiB
C++

/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "GitRepo.h"
#include <LibCore/Command.h>
#include <stdio.h>
#include <stdlib.h>
namespace HackStudio {
GitRepo::CreateResult GitRepo::try_to_create(const LexicalPath& repository_root)
{
if (!git_is_installed()) {
return { CreateResult::Type::GitProgramNotFound, nullptr };
}
if (!git_repo_exists(repository_root)) {
return { CreateResult::Type::NoGitRepo, nullptr };
}
return { CreateResult::Type::Success, adopt(*new GitRepo(repository_root)) };
}
RefPtr<GitRepo> GitRepo::initialize_repository(const LexicalPath& repository_root)
{
auto res = command_wrapper("init", repository_root);
if (res.is_null())
return {};
ASSERT(git_repo_exists(repository_root));
return adopt(*new GitRepo(repository_root));
}
Vector<LexicalPath> GitRepo::unstaged_files() const
{
auto modified = modified_files();
auto untracked = untracked_files();
modified.append(move(untracked));
return modified;
}
Vector<LexicalPath> GitRepo::staged_files() const
{
auto raw_result = command("diff --cached --name-only");
if (raw_result.is_null())
return {};
return parse_files_list(raw_result);
}
Vector<LexicalPath> GitRepo::modified_files() const
{
auto raw_result = command("ls-files --modified --exclude-standard");
if (raw_result.is_null())
return {};
return parse_files_list(raw_result);
}
Vector<LexicalPath> GitRepo::untracked_files() const
{
auto raw_result = command("ls-files --others --exclude-standard");
if (raw_result.is_null())
return {};
return parse_files_list(raw_result);
}
Vector<LexicalPath> GitRepo::parse_files_list(const String& raw_result)
{
auto lines = raw_result.split('\n');
Vector<LexicalPath> files;
for (const auto& line : lines) {
files.empend(line);
}
return files;
}
String GitRepo::command(const String& git_command) const
{
return command_wrapper(git_command, m_repository_root);
}
String GitRepo::command_wrapper(const String& git_command, const LexicalPath& chdir)
{
return Core::command(String::format("git %s", git_command.characters()), chdir);
}
bool GitRepo::git_is_installed()
{
return !command_wrapper("--help", LexicalPath("/")).is_null();
}
bool GitRepo::git_repo_exists(const LexicalPath& repo_root)
{
return !command_wrapper("status", repo_root).is_null();
}
bool GitRepo::stage(const LexicalPath& file)
{
auto cmd = String::format("add %s", file.string().characters());
auto res = command(cmd);
return !res.is_null();
}
bool GitRepo::unstage(const LexicalPath& file)
{
auto cmd = String::format("reset HEAD -- %s", file.string().characters());
return !command(cmd).is_null();
}
}