From 2099e48cdd64ad28d7910455117c909067579e8e Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Wed, 9 Apr 2008 12:12:51 +0200 Subject: [PATCH] Add 'branches' command to test the fetching of branches in Configuration --- InterviewCursor.cpp => Branches.cpp | 38 +++++++++++++++++++++--- InterviewCursor.cpp => Branches.h | 22 +++++++++----- Changes.cpp | 4 ++- CommandLineParser.cpp | 2 ++ Configuration.cpp | 58 ++++++++++++++++++++++++++++++++++++- Configuration.h | 14 ++++++++- InterviewCursor.cpp | 1 + InterviewCursor.h | 4 +-- Push.cpp | 1 + Record.cpp | 1 + Revert.cpp | 1 + VngCommandLine.cpp | 6 ++++ WhatsNew.cpp | 1 + commits/Commit.h | 2 +- commits/Commit_p.h | 1 + hunks/HunksCursor.cpp | 1 + hunks/HunksCursor.h | 3 +- todo | 8 +---- vng.pro | 2 ++ 19 files changed, 144 insertions(+), 26 deletions(-) copy InterviewCursor.cpp => Branches.cpp (50%) copy InterviewCursor.cpp => Branches.h (72%) diff --git a/InterviewCursor.cpp b/Branches.cpp similarity index 50% copy from InterviewCursor.cpp copy to Branches.cpp index 99c0c40..5354742 100644 --- a/InterviewCursor.cpp +++ b/Branches.cpp @@ -16,13 +16,43 @@ * along with this program. If not, see . */ -#include "InterviewCursor.h" +#include "Branches.h" +#include "CommandLineParser.h" +#include "Logger.h" +// #include "GitRunner.h" +// #include "Vng.h" -InterviewCursor::InterviewCursor() - : m_config(0) +// #include +// #include + +static const CommandLineOption options[] = { + // TODO :) + CommandLineLastOption +}; + +Branches::Branches() + : AbstractCommand("branches") +{ + CommandLineParser::addOptionDefinitions(options); +} + +AbstractCommand::ReturnCodes Branches::run() +{ + if (! checkInRepository()) + return NotInRepo; + + foreach(Branch branch, m_config.allBranches()) { + Logger::info() << branch.branchName() << endl; + } + return Ok; +} + +QString Branches::argumentDescription() const { + return QString(); } -InterviewCursor::~InterviewCursor() +QString Branches::commandDescription() const { + return ""; // TODO } diff --git a/InterviewCursor.cpp b/Branches.h similarity index 72% copy from InterviewCursor.cpp copy to Branches.h index 99c0c40..ee0b494 100644 --- a/InterviewCursor.cpp +++ b/Branches.h @@ -16,13 +16,19 @@ * along with this program. If not, see . */ -#include "InterviewCursor.h" +#ifndef BRANCHES_H +#define BRANCHES_H -InterviewCursor::InterviewCursor() - : m_config(0) -{ -} +#include "AbstractCommand.h" -InterviewCursor::~InterviewCursor() -{ -} +class Branches : public AbstractCommand { +public: + Branches(); + +protected: + virtual QString argumentDescription() const; + virtual ReturnCodes run(); + virtual QString commandDescription() const; +}; + +#endif diff --git a/Changes.cpp b/Changes.cpp index 38641bf..7abbdfe 100644 --- a/Changes.cpp +++ b/Changes.cpp @@ -19,9 +19,10 @@ #include "Changes.h" #include "CommandLineParser.h" #include "GitRunner.h" -#include "commits/Commit.h" #include "Logger.h" #include "Vng.h" +#include "hunks/ChangeSet.h" +#include "commits/Commit.h" #include @@ -50,6 +51,7 @@ Changes::Changes() : AbstractCommand("changes") { CommandLineParser::addOptionDefinitions(options); + CommandLineParser::setArgumentDefinition("changes [FILE or DIRECTORY]" ); } QString Changes::argumentDescription() const diff --git a/CommandLineParser.cpp b/CommandLineParser.cpp index e31eb1e..8617563 100644 --- a/CommandLineParser.cpp +++ b/CommandLineParser.cpp @@ -144,6 +144,8 @@ void CommandLineParser::Private::setArgumentDefinition(const char *defs) inArg = true; requiredArguments++; } + else if (s[0] == '[') + inArg = true; if (s.endsWith('>')) inArg = false; else if (!inArg) diff --git a/Configuration.cpp b/Configuration.cpp index 36020ab..5493730 100644 --- a/Configuration.cpp +++ b/Configuration.cpp @@ -17,8 +17,11 @@ */ #include "Configuration.h" #include "Logger.h" +#include "GitRunner.h" +#include "AbstractCommand.h" #include +#include #ifndef Q_OS_WIN #include @@ -28,7 +31,8 @@ Configuration::Configuration(const QString §ion) : m_repoDir("."), m_section(section), m_dirty(true), - m_emptyRepo(false) + m_emptyRepo(false), + m_fetchedBranches(false) { } @@ -120,3 +124,55 @@ bool Configuration::isEmptyRepo() const const_cast (this)->readConfig(); return m_emptyRepo; } + +QList Configuration::allBranches() +{ + fetchBranches(); + QList answer; + answer += m_localBranches; + answer += m_remoteBranches; + return answer; +} + +QList Configuration::branches() +{ + fetchBranches(); + return m_localBranches; +} + +QList Configuration::remoteBranches() +{ + fetchBranches(); + return m_remoteBranches; +} + +void Configuration::fetchBranches() +{ + if (m_fetchedBranches) + return; + m_fetchedBranches = true; + + QProcess git; + QStringList arguments; + arguments << "ls-remote" << "."; + GitRunner runner(git, arguments); + AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput); + if (rc != AbstractCommand::Ok) { + return; + } + char buf[1024]; + while(true) { + qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf)); + if (lineLength == -1) + break; + if (lineLength > 46) { // only take stuff that is in the 'refs' dir. + QString name(buf + 46); + name = name.trimmed(); // remove linefeed + Branch branch(name); + if (name.startsWith("remotes")) + m_remoteBranches.append(branch); + else + m_localBranches.append(branch); + } + } +} diff --git a/Configuration.h b/Configuration.h index bb71890..01ad4e5 100644 --- a/Configuration.h +++ b/Configuration.h @@ -23,6 +23,8 @@ #include #include +#include "commits/Branch.h" + class Configuration { public: Configuration(const QString §ion); @@ -47,13 +49,23 @@ public: /// returns true when there is a repository, but it has not had its initial commit yet. bool isEmptyRepo() const; + /// return the branches in the current repo. + QList allBranches(); + /// return the branches in the current repo. + QList branches(); + /// return the branches in the current repo. + QList remoteBranches(); + private: void readConfig(); + void fetchBranches(); QDir m_repoDir; QString m_section; - bool m_dirty, m_emptyRepo; + bool m_dirty, m_emptyRepo, m_fetchedBranches; QHash m_options; + QList m_localBranches; + QList m_remoteBranches; }; #endif diff --git a/InterviewCursor.cpp b/InterviewCursor.cpp index 99c0c40..b618aeb 100644 --- a/InterviewCursor.cpp +++ b/InterviewCursor.cpp @@ -17,6 +17,7 @@ */ #include "InterviewCursor.h" +#include "Configuration.h" InterviewCursor::InterviewCursor() : m_config(0) diff --git a/InterviewCursor.h b/InterviewCursor.h index 03f14ce..833a30b 100644 --- a/InterviewCursor.h +++ b/InterviewCursor.h @@ -19,10 +19,10 @@ #ifndef INTERVIEWCURSOR_H #define INTERVIEWCURSOR_H -#include "Configuration.h" - #include +class Configuration; + class InterviewCursor { public: enum Scope { diff --git a/Push.cpp b/Push.cpp index 538fc63..c075d66 100644 --- a/Push.cpp +++ b/Push.cpp @@ -44,6 +44,7 @@ Push::Push() : AbstractCommand("Push") { CommandLineParser::addOptionDefinitions(options); + CommandLineParser::setArgumentDefinition("push [ReturnCodes]" ); } AbstractCommand::ReturnCodes Push::run() diff --git a/Record.cpp b/Record.cpp index fcb349a..3f18533 100644 --- a/Record.cpp +++ b/Record.cpp @@ -85,6 +85,7 @@ Record::Record() : AbstractCommand("record") { CommandLineParser::addOptionDefinitions(options); + CommandLineParser::setArgumentDefinition("record [FILE or DIRECTORY]" ); } QString Record::argumentDescription() const diff --git a/Revert.cpp b/Revert.cpp index 240a77d..77567fc 100644 --- a/Revert.cpp +++ b/Revert.cpp @@ -37,6 +37,7 @@ Revert::Revert() : AbstractCommand("revert") { CommandLineParser::addOptionDefinitions(options); + CommandLineParser::setArgumentDefinition("revert [FILE or DIRECTORY]" ); } AbstractCommand::ReturnCodes Revert::run() diff --git a/VngCommandLine.cpp b/VngCommandLine.cpp index 09ebd2e..b8ef6f2 100644 --- a/VngCommandLine.cpp +++ b/VngCommandLine.cpp @@ -22,6 +22,7 @@ // commands #include "Add.h" +#include "Branches.h" #include "Changes.h" #include "Initialize.h" #include "Push.h" @@ -72,6 +73,7 @@ int VngCommandLine::run() QStringList commands; // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort commands << "add"; + commands << "branches"; commands << "changes"; commands << "help"; commands << "initialize"; @@ -91,6 +93,7 @@ int VngCommandLine::run() out << "--extended--help" << endl; if (QDir(config.repository().absolutePath() + "/.git").exists()) { out << "add" << endl; + out << "branches" << endl; out << "changes" << endl; out << "push" << endl; out << "record" << endl; @@ -138,6 +141,8 @@ int VngCommandLine::run() ac = new WhatsNew(); else if (m_command == "add") ac = new Add(); + else if (m_command == "branches") + ac = new Branches(); else if (m_command == "changes") ac = new Changes(); else if (m_command == "initialize") @@ -179,6 +184,7 @@ void VngCommandLine::printHelp() out << " push Copy and apply patches from this repository to another one." << endl; out << "Querying the repository:" << endl; // out << " diff Create a diff between two versions of the repository." << endl; + out << " breanches " << endl; out << " changes Gives a changelog-style summary of the repository history." << endl; out << "Administrating repositories:" << endl; out << " initialize Initialize a new source tree as a vng repository." << endl; diff --git a/WhatsNew.cpp b/WhatsNew.cpp index 28c6003..a1cbd2e 100644 --- a/WhatsNew.cpp +++ b/WhatsNew.cpp @@ -39,6 +39,7 @@ WhatsNew::WhatsNew() : AbstractCommand("whatsnew") { CommandLineParser::addOptionDefinitions(options); + CommandLineParser::setArgumentDefinition("whatsnew [FILE or DIRECTORY]" ); } AbstractCommand::ReturnCodes WhatsNew::run() diff --git a/commits/Commit.h b/commits/Commit.h index 110fc9a..672f9ff 100644 --- a/commits/Commit.h +++ b/commits/Commit.h @@ -19,12 +19,12 @@ #define COMMIT_H #include "../Vng.h" -#include "../hunks/ChangeSet.h" #include #include class CommitPrivate; +class ChangeSet; class Commit { public: diff --git a/commits/Commit_p.h b/commits/Commit_p.h index 827997e..ca4b32a 100644 --- a/commits/Commit_p.h +++ b/commits/Commit_p.h @@ -17,6 +17,7 @@ */ #include "Commit.h" +#include "../hunks/ChangeSet.h" class CommitPrivate { diff --git a/hunks/HunksCursor.cpp b/hunks/HunksCursor.cpp index 86201c5..28046c4 100644 --- a/hunks/HunksCursor.cpp +++ b/hunks/HunksCursor.cpp @@ -17,6 +17,7 @@ */ #include "HunksCursor.h" +#include "ChangeSet.h" #include diff --git a/hunks/HunksCursor.h b/hunks/HunksCursor.h index cd92f66..b9cbf0f 100644 --- a/hunks/HunksCursor.h +++ b/hunks/HunksCursor.h @@ -20,7 +20,8 @@ #define HUNKSCURSOR_H #include "../InterviewCursor.h" -#include "ChangeSet.h" + +class ChangeSet; class HunksCursor : public InterviewCursor { public: diff --git a/todo b/todo index 10c42d1..ebfe43a 100644 --- a/todo +++ b/todo @@ -43,6 +43,7 @@ Bugs * Also show / store file permissions in the diff and thus in revert * a Whatsnew -u still shows a/foo and b/foo remove the 'a' and 'b' Maybe add a bool arg to ChangeSet::writeDiff * Instead of hardcoding '.git' as a dirname; use git-rev-parse in Configuration and in commits/Commit +* reading a patch that has '\ No newline at end of file' fails use git-read-tree --reset HEAD and git-update-index --refresh to implement 'check' use 'prune' and 'pack' to implement optimize. @@ -57,13 +58,6 @@ changes: allow -v to show the diff. We should read the diff in memory and if the user passed a filename we should only show the diffs for that file. -Config: - I think we need a way to get all the braches from the config. Use - git ls-remote . - to get those. - Maybe we want to have a Branch object that inherits Commit and adds a 'name' string? - - [09:31] git people: is it possible to push just a single commit to devil, or does it ineed to be a branch? [09:31] brad: you can for example do: git push :name-of-new-branch [09:31] brad: and later remove the branch again with git push :name-of-new-branch diff --git a/vng.pro b/vng.pro index fafe7e2..1bfa7ae 100644 --- a/vng.pro +++ b/vng.pro @@ -14,6 +14,7 @@ SOURCES += \ Configuration.cpp \ VngCommandLine.cpp \ Add.cpp \ + Branches.cpp \ Changes.cpp \ Initialize.cpp \ Push.cpp \ @@ -42,6 +43,7 @@ HEADERS += \ Configuration.h \ VngCommandLine.h \ Add.h \ + Branches.h \ Changes.h \ Initialize.h \ Push.h \ -- 2.11.4.GIT