From 7fd6386ca0c18e25073dd21d8f33787d37284caa Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Sat, 18 Oct 2008 18:42:28 +0200 Subject: [PATCH] Further work on amend record, the general usecase of amending the most recent commit works mostly. --- src/commands/AmendRecord.cpp | 77 ++++++++++++++++++++++++++++----- src/commands/UnRecord.cpp | 100 ++++++++++++++++++++++--------------------- src/commands/UnRecord.h | 7 ++- src/patches/Commit.h | 2 +- src/src.pro | 1 + 5 files changed, 126 insertions(+), 61 deletions(-) diff --git a/src/commands/AmendRecord.cpp b/src/commands/AmendRecord.cpp index 453ba5f..c7b5dd2 100644 --- a/src/commands/AmendRecord.cpp +++ b/src/commands/AmendRecord.cpp @@ -18,13 +18,27 @@ */ #include "AmendRecord.h" -#include "../CommandLineParser.h" +#include "UnRecord.h" +#include "Record.h" +#include +#include +#include +#include + +#include static const CommandLineOption options[] = { {"-a, --all", "answer yes to all patches"}, {"-i, --interactive", "prompt user interactively"}, {"-m, --patch-name PATCHNAME", "name of patch"}, - {"-A, --author EMAIL", "specify author id"}, + {"--from-match PATTERN", "select changes starting with a patch matching PATTERN"}, + {"--from-patch REGEXP", "select changes starting with a patch matching REGEXP"}, + //{"--from-tag REGEXP", "select changes starting with a tag matching REGEXP"}, + {"--match PATTERN", "select patches matching PATTERN"}, + {"-p, --patches REGEXP", "select patches matching REGEXP"}, + {"-e, --edit", "(re)edit the patch name"}, + //{"-t REGEXP,--tags REGEXP", "select tags matching REGEXP"}, + //{"-A, --author EMAIL", "specify author id"}, //{"--logfile FILE", "give patch name and comment in file"}, //{"--delete-logfile", "delete the logfile when done"}, //{"--no-test", "don't run the test script"}, @@ -50,14 +64,57 @@ AbstractCommand::ReturnCodes AmendRecord::run() return NotInRepo; moveToRoot(); -/* - get which commit to amend "c1" using the cursor - - if c1 is the last made commit - unrecord commit - do a normal record, passing in the old content - done + CommitsCursor cursor(CommitsCursor::SelectOnePatch); + cursor.setUseMatcher(true); + Interview interview(cursor, name()); + interview.setUsePager(shouldUsePager()); + if (! interview.start()) { + Logger::warn() << "amend-record cancelled." << endl; + return Ok; + } + + Commit acceptedCommit = cursor.head(); + while (acceptedCommit.acceptance() != Vng::Accepted) { + Commit commit = acceptedCommit.previous()[0]; + if (! commit.isValid()) + break; + acceptedCommit = commit; + } + + // qDebug() << "going to amend" << acceptedCommit.commitTreeIsmSha1(); + if (dryRun()) + return Ok; + + if (acceptedCommit.previousCommitsCount() != 1) { + Logger::error() << "Vng failed: could not find proper parent of commit\n"; + return InvalidOptions; + } + CommandLineParser *args = CommandLineParser::instance(); + const bool editMessage = (m_config.contains("edit") && !args->contains("skip")) || args->contains("edit"); + + if (acceptedCommit.commitTreeIsm() == "HEAD") { // easy ;) + UnRecord unRecord; + Commit parent = acceptedCommit.previous().first(); + unRecord.setUnrecordCommit(parent); + ReturnCodes rc = unRecord.run(); + if (rc) { + qDebug() << "oops" << rc; // TODO + return rc; + } + Record record; + record.setPatchName(acceptedCommit.logMessage().toLatin1()); + record.setEditComment(editMessage); + rc = record.run(); + if (rc) { + qDebug() << "oops" << rc; // TODO + // TODO specifically handle UserCancelled which requires us to undo the unrecord + return rc; + } + return Ok; + } + qDebug() << "sorry this usecase is not handled yet"; +/* do a normal record, passing in a dummy comment. get the sha1 of that commit, call it "tmp1" @@ -75,9 +132,7 @@ AbstractCommand::ReturnCodes AmendRecord::run() # git rebase --onto tmp_branch c1 master do cherry picks of all the commits following c1 till our previous HEAD - */ - return Ok; } diff --git a/src/commands/UnRecord.cpp b/src/commands/UnRecord.cpp index 7e90246..df9723b 100644 --- a/src/commands/UnRecord.cpp +++ b/src/commands/UnRecord.cpp @@ -69,63 +69,67 @@ AbstractCommand::ReturnCodes UnRecord::run() CommandLineParser *args = CommandLineParser::instance(); - Commit acceptedCommit; + Commit acceptedCommit = m_unrecordCommit; int unrecordCount = 0; bool oneAutoAcceptedPatch = false; - if (args->contains("all")) { - Commit head("HEAD"); - Commit commit = head.firstCommitInBranch(); - if (! commit.isValid()) { - Logger::error() << "Vng failed: Could not find the branch point, are you sure you are on a branch?\n"; - return OtherVngError; - } - acceptedCommit = commit; - while(true) { - unrecordCount++; - if (commit == head) - break; - commit = commit.next(); - oneAutoAcceptedPatch = true; - } - } - else { // use a cursor - CommitsCursor cursor(CommitsCursor::SelectRange); - cursor.setUseMatcher(true); - if (args->contains("last")) { - int count = args->optionArgument("last").toInt(); - for (int i = 0; i < count && cursor.isValid(); i++) { - cursor.setResponse(true); - cursor.forward(); + if (acceptedCommit.isValid()) { + unrecordCount = 1; + } else { + if (args->contains("all")) { + Commit head("HEAD"); + Commit commit = head.firstCommitInBranch(); + if (! commit.isValid()) { + Logger::error() << "Vng failed: Could not find the branch point, are you sure you are on a branch?\n"; + return OtherVngError; } - } - else { - Interview interview(cursor, name()); - interview.setUsePager(shouldUsePager()); - if (! interview.start()) { - Logger::warn() << "unrecord cancelled." << endl; - return Ok; + acceptedCommit = commit; + while(true) { + unrecordCount++; + if (commit == head) + break; + commit = commit.next(); + oneAutoAcceptedPatch = true; } } + else { // use a cursor + CommitsCursor cursor(CommitsCursor::SelectRange); + cursor.setUseMatcher(true); + if (args->contains("last")) { + int count = args->optionArgument("last").toInt(); + for (int i = 0; i < count && cursor.isValid(); i++) { + cursor.setResponse(true); + cursor.forward(); + } + } + else { + Interview interview(cursor, name()); + interview.setUsePager(shouldUsePager()); + if (! interview.start()) { + Logger::warn() << "unrecord cancelled." << endl; + return Ok; + } + } - Commit commit = cursor.head(); - while (true) { - if (commit.acceptance() == Vng::Rejected) // can't use this one. - break; - else if (commit.acceptance() == Vng::Accepted) - acceptedCommit = commit; - if (commit.previousCommitsCount() == 0) // at first commit. - break; - if (commit.acceptance() == Vng::Undecided) { - if (acceptedCommit.isValid()) // already found a 'yes'. + Commit commit = cursor.head(); + while (true) { + if (commit.acceptance() == Vng::Rejected) // can't use this one. break; - oneAutoAcceptedPatch = true; + else if (commit.acceptance() == Vng::Accepted) + acceptedCommit = commit; + if (commit.previousCommitsCount() == 0) // at first commit. + break; + if (commit.acceptance() == Vng::Undecided) { + if (acceptedCommit.isValid()) // already found a 'yes'. + break; + oneAutoAcceptedPatch = true; + } + commit = commit.previous()[0]; + if (unrecordCount >= cursor.oldestCommitAltered()) + break; + unrecordCount++; } - commit = commit.previous()[0]; - if (unrecordCount >= cursor.oldestCommitAltered()) - break; - unrecordCount++; + acceptedCommit = commit; } - acceptedCommit = commit; } if (unrecordCount == 0 || !acceptedCommit.isValid()) { diff --git a/src/commands/UnRecord.h b/src/commands/UnRecord.h index 970f88e..912394c 100644 --- a/src/commands/UnRecord.h +++ b/src/commands/UnRecord.h @@ -25,10 +25,15 @@ class UnRecord : public AbstractCommand public: UnRecord(); + void setUnrecordCommit(const Commit &commit) { m_unrecordCommit = commit; } + virtual ReturnCodes run(); + protected: virtual QString argumentDescription() const; - virtual ReturnCodes run(); virtual QString commandDescription() const; + +private: + Commit m_unrecordCommit; }; #endif diff --git a/src/patches/Commit.h b/src/patches/Commit.h index 2307c1d..6aa16bb 100644 --- a/src/patches/Commit.h +++ b/src/patches/Commit.h @@ -45,7 +45,7 @@ public: QString author() const; QString committer() const; - QString logMessage() const; + QString logMessage() const; // TODO change to be a byteArray /// The tree structure sha1, which is part of the commit. QString tree() const; /// the param passed to the constructor. This can be something like "HEAD" diff --git a/src/src.pro b/src/src.pro index 60c70dc..112bb00 100644 --- a/src/src.pro +++ b/src/src.pro @@ -3,6 +3,7 @@ CONFIG += console CONFIG -= app_bundle DESTDIR = .. TARGET = vng +INCLUDEPATH += hunks patches commands # this is a line we can enable for static linking #LIBS += -Wl,-Bstatic -lQtCore -Wl,-Bdynamic -- 2.11.4.GIT