Browse Source

Moved pull functionality to branch

- Branch is now responsible to pull it selves
Alexey Edelev 5 years ago
parent
commit
45f4de42f0
4 changed files with 82 additions and 14 deletions
  1. 54 4
      gitbranch.cpp
  2. 10 1
      gitbranch.h
  3. 14 2
      githandler.cpp
  4. 4 7
      githandler.h

+ 54 - 4
gitbranch.cpp

@@ -11,7 +11,7 @@ const char remoteAddtion = '/';
 
 GitBranch::GitBranch() : GitReference(nullptr, nullptr)
   ,m_commit(nullptr)
-  ,m_type(Invalid)
+  ,m_type(GitBranch::Invalid)
 {
 }
 
@@ -44,6 +44,7 @@ GitBranch::GitBranch(GitBranch&& other) : GitReference(std::move(other))
     }
     m_commit = other.m_commit;
     other.m_commit = nullptr;
+    other.m_type = GitBranch::Invalid;
 }
 
 GitBranch &GitBranch::operator=(GitBranch&& other)
@@ -55,6 +56,7 @@ GitBranch &GitBranch::operator=(GitBranch&& other)
         }
         m_commit = other.m_commit;
         other.m_commit = nullptr;
+        other.m_type = GitBranch::Invalid;
     }
     return static_cast<GitBranch&>(GitReference::operator=(std::move(other)));
 }
@@ -74,8 +76,8 @@ GitBranch::BranchType GitBranch::type() const
 
 void GitBranch::setUpstream(const GitBranch& branch)
 {
-    if(type() == GIT_BRANCH_REMOTE
-            || branch.type() == GIT_BRANCH_LOCAL) {
+    if(type() == GitBranch::Remote
+            || branch.type() == GitBranch::Local) {
         qWarning() << "Try to setup invalid pair of upstream/local branches";
         return;
     }
@@ -85,7 +87,7 @@ void GitBranch::setUpstream(const GitBranch& branch)
 GitBranch GitBranch::upstream() const
 {
     git_reference* ref = nullptr;
-    if(type() == GIT_BRANCH_REMOTE) {
+    if(type() == GitBranch::Remote) {
         qDebug() << "Skipping upstream read for remote branch";
         return GitBranch();
     }
@@ -95,3 +97,51 @@ GitBranch GitBranch::upstream() const
     }
     return GitBranch(ref, GIT_BRANCH_REMOTE, repository());
 }
+
+void GitBranch::pull(PullStrategy strategy)
+{
+    if(type() == GitBranch::Remote) {
+        qWarning() << "It's not possible to update remote branch, seems issue in branch handling logic";
+        return;
+    }
+
+    GitBranch upstreamBranch = upstream();
+
+    qDebug() << "Upstream branch for " << fullName() << " is " << upstreamBranch.fullName();
+
+    git_annotated_commit* commit = upstreamBranch.annotatedCommit();
+    git_merge_analysis_t analResult;
+    git_merge_preference_t preferences;
+    git_merge_analysis(&analResult, &preferences, m_repository->raw(), (const git_annotated_commit**)&commit, 1);
+
+    if (analResult & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
+        qDebug() << "Up-to-date";
+        return;
+    }
+
+    if (analResult & GIT_MERGE_ANALYSIS_FASTFORWARD) {
+        qDebug() << "Fast-forward possible";
+        fastForward();
+        return;
+    }
+
+    switch (strategy) {
+    case Merge:
+        //TDB: Apply merge strategy
+        break;
+    default:
+        //TDB: Apply merge strategy
+        break;
+    }
+    qWarning() << "Other merge methods are not supported yet";
+}
+
+void GitBranch::fastForward()
+{
+    git_reference *newRaw = nullptr;
+    if(0 == git_reference_set_target(&newRaw, m_raw, upstream().oid().raw(), "Update branch HEAD using fast-forward")) {
+        m_raw = newRaw;
+    } else {
+        qWarning() << "Could not apply fast-forward " << lastError();
+    }
+}

+ 10 - 1
gitbranch.h

@@ -14,7 +14,7 @@ class GitBranch : public GitReference
     Q_PROPERTY(QString remote READ remote NOTIFY remoteChanged)
     Q_PROPERTY(BranchType type READ type CONSTANT)
     Q_PROPERTY(GitOid oid READ oid CONSTANT)
-    Q_ENUMS(BranchType)
+    Q_ENUMS(BranchType PullStrategy)
 
 public:
     enum BranchType {
@@ -23,6 +23,11 @@ public:
         Remote = GIT_BRANCH_REMOTE
     };
 
+    enum PullStrategy {
+        Rebase,
+        Merge
+    };
+
     GitBranch(git_reference *ref, git_branch_t type, GitRepository *parent);
     GitBranch(GitBranch &&other);
     GitBranch &operator=(GitBranch &&other);
@@ -53,6 +58,8 @@ public:
 
     void setUpstream(const GitBranch &branch);
 
+    void pull(PullStrategy strategy);
+
 public slots:
     void setName(QString name) {
         if (m_name == name)
@@ -71,6 +78,8 @@ signals:
 
 private:
     void free();
+    void fastForward();
+
     GitBranch();
     Q_DISABLE_COPY(GitBranch)
 

+ 14 - 2
githandler.cpp

@@ -199,7 +199,7 @@ GitDiff* GitHandler::activeDiff() const
     return m_activeDiff.data();
 }
 
-void GitHandler::pull(PullStrategy strategy) const
+void GitHandler::pull(GitBranch::PullStrategy strategy) const
 {
     if(m_activeRepo->remotes().count() <= 0) {
         qWarning() << "No remotes available for repository. Please add manually in console";
@@ -215,7 +215,19 @@ void GitHandler::pull(PullStrategy strategy) const
     }
 
     remote->fetch();
-//    git_merge_analysis();
+    GitOid oid = m_activeRepo->head();
+    auto branches = m_activeRepo->branches();
+    auto result = std::find_if(branches.begin(), branches.end(), [&](QPointer<GitBranch>& branch) {
+        return branch->oid() == oid;
+    });
+
+    if(result == branches.end() || (*result).isNull()) {
+        qDebug() << "HEAD is not on branch";
+        return;
+    }
+
+    GitBranch *headBranch = (*result);
+    headBranch->pull(strategy);
 }
 
 void GitHandler::updateModels()

+ 4 - 7
githandler.h

@@ -4,6 +4,7 @@
 #include <QObject>
 
 #include <repositorymodel.h>
+#include <gitbranch.h>
 #include <QFutureWatcher>
 
 class CommitModel;
@@ -29,13 +30,7 @@ class GitHandler : public QObject
     Q_PROPERTY(bool isBusy READ isBusy NOTIFY isBusyChanged)
     Q_PROPERTY(QUrl homePath READ homePath CONSTANT)
 
-    Q_ENUMS(PullStrategy)
 public:
-    enum PullStrategy {
-        Rebase,
-        Merge
-    };
-
     GitHandler();
     virtual ~GitHandler();
     Q_INVOKABLE void open(const QString &path, bool activate = false);
@@ -50,7 +45,7 @@ public:
 
     Q_INVOKABLE void copy(const QString& sha1);
 
-    Q_INVOKABLE void pull(PullStrategy strategy) const;
+    Q_INVOKABLE void pull(GitBranch::PullStrategy strategy) const;
 
     RepositoryModel* repositories() const
     {
@@ -148,4 +143,6 @@ private:
     //    GitOid m_constantHead; //TODO:
 };
 
+Q_DECLARE_METATYPE(GitBranch::PullStrategy)
+
 #endif // GITHANDLER_H