This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new 2e5a255b3a Fix Git pull for branches with different upstream names 
(#8974)
2e5a255b3a is described below

commit 2e5a255b3a282a3af65ae1282c0d54d0476619d8
Author: Gabriel Dutra <[email protected]>
AuthorDate: Wed Nov 4 01:23:41 2026 -0700

    Fix Git pull for branches with different upstream names (#8974)
    
    * Fix Git pull with differently named upstream
    
    * Handle configured remotes when pulling
---
 .../main/java/org/apache/hop/git/model/UIGit.java  |  66 +++++++-
 .../java/org/apache/hop/git/model/UIGitTest.java   | 167 +++++++++++++++++++++
 2 files changed, 228 insertions(+), 5 deletions(-)

diff --git a/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java 
b/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java
index abef52b96a..197916f67a 100644
--- a/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java
+++ b/plugins/misc/git/src/main/java/org/apache/hop/git/model/UIGit.java
@@ -79,6 +79,7 @@ import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.errors.NoWorkTreeException;
 import org.eclipse.jgit.errors.RevisionSyntaxException;
+import org.eclipse.jgit.lib.BranchConfig;
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.lib.Constants;
@@ -876,15 +877,29 @@ public class UIGit extends VCS {
       throw new HopException(
           "You have uncommitted changes. Please commit work before pulling 
changes.");
     }
-    if (!hasRemote()) {
-      throw new HopException("There is no remote set up to pull from. Please 
set this up first.");
-    }
 
     try {
+      Repository repository = git.getRepository();
+      String branch = getPullBranchName(repository);
+      BranchConfig branchConfig = new BranchConfig(repository.getConfig(), 
branch);
+      String remote = branchConfig.getRemote();
+      if (remote == null) {
+        remote = Constants.DEFAULT_REMOTE_NAME;
+      }
+
       // Pull = Fetch + Merge
-      git.fetch().setCredentialsProvider(credentialsProvider).call();
+      if (!BranchConfig.LOCAL_REPOSITORY.equals(remote)) {
+        if (!repository
+            .getConfig()
+            .getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION)
+            .contains(remote)) {
+          throw new HopException(
+              "There is no remote set up to pull from. Please set this up 
first.");
+        }
+        
git.fetch().setRemote(remote).setCredentialsProvider(credentialsProvider).call();
+      }
       return mergeBranch(
-          Constants.DEFAULT_REMOTE_NAME + "/" + getBranch(), 
MergeStrategy.RECURSIVE);
+          getPullBranch(repository, branch, branchConfig, remote), 
MergeStrategy.RECURSIVE);
     } catch (TransportException e) {
       if (e.getMessage()
               .contains(
@@ -904,12 +919,53 @@ public class UIGit extends VCS {
       } else {
         throw new HopException("There was an error doing a git pull", e);
       }
+    } catch (HopException e) {
+      throw e;
     } catch (Exception e) {
       throw new HopException("There was an error doing a git pull", e);
     }
     return false;
   }
 
+  private String getPullBranchName(Repository repository) throws HopException, 
IOException {
+    String fullBranch = repository.getFullBranch();
+    if (fullBranch == null || !fullBranch.startsWith(Constants.R_HEADS)) {
+      throw new HopException("Cannot pull because HEAD is not on a branch.");
+    }
+    return Repository.shortenRefName(fullBranch);
+  }
+
+  private String getPullBranch(
+      Repository repository, String branch, BranchConfig branchConfig, String 
remote)
+      throws HopException, IOException {
+    String mergeBranch = branchConfig.getMerge();
+    String pullBranch;
+    if (mergeBranch == null) {
+      pullBranch = Constants.DEFAULT_REMOTE_NAME + "/" + branch;
+    } else if (BranchConfig.LOCAL_REPOSITORY.equals(remote)) {
+      pullBranch = Repository.shortenRefName(mergeBranch);
+    } else {
+      String trackingBranch = branchConfig.getTrackingBranch();
+      pullBranch =
+          trackingBranch == null
+              ? remote + "/" + Repository.shortenRefName(mergeBranch)
+              : Repository.shortenRefName(trackingBranch);
+    }
+
+    if (repository.resolve(pullBranch) == null) {
+      if (mergeBranch == null) {
+        throw new HopException(
+            "There is no upstream branch configured for '"
+                + branch
+                + "' and '"
+                + pullBranch
+                + "' does not exist.");
+      }
+      throw new HopException("The configured upstream branch '" + pullBranch + 
"' does not exist.");
+    }
+    return pullBranch;
+  }
+
   public boolean push() throws HopException {
     return push("default");
   }
diff --git 
a/plugins/misc/git/src/test/java/org/apache/hop/git/model/UIGitTest.java 
b/plugins/misc/git/src/test/java/org/apache/hop/git/model/UIGitTest.java
index 9938ad1b2b..a6342ec73e 100644
--- a/plugins/misc/git/src/test/java/org/apache/hop/git/model/UIGitTest.java
+++ b/plugins/misc/git/src/test/java/org/apache/hop/git/model/UIGitTest.java
@@ -50,6 +50,7 @@ import org.apache.hop.core.logging.HopLogStore;
 import org.apache.hop.git.model.revision.ObjectRevision;
 import org.apache.hop.i18n.BaseMessages;
 import org.apache.hop.ui.core.dialog.EnterSelectionDialog;
+import org.eclipse.jgit.api.CreateBranchCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.RemoteAddCommand;
 import org.eclipse.jgit.api.Status;
@@ -62,6 +63,7 @@ import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.RepositoryState;
 import org.eclipse.jgit.merge.MergeStrategy;
@@ -280,6 +282,171 @@ public class UIGitTest extends RepositoryTestCase {
     assertTrue(uiGit.pull());
   }
 
+  @Test
+  public void testPullFromConfiguredUpstreamWithDifferentName() throws 
Exception {
+    setupRemote();
+    Git git2 = new Git(db2);
+
+    File sourceFile = new File(db2.getWorkTree(), "SomeFile.txt");
+    FileUtils.writeStringToFile(sourceFile, "Initial content", 
StandardCharsets.UTF_8);
+    git2.add().addFilepattern("SomeFile.txt").call();
+    git2.commit().setMessage("Initial commit").call();
+
+    git.fetch().call();
+    git.checkout()
+        .setCreateBranch(true)
+        .setName("local-feature")
+        .setStartPoint("origin/master")
+        .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
+        .call();
+
+    FileUtils.writeStringToFile(sourceFile, "Updated content", 
StandardCharsets.UTF_8);
+    git2.add().addFilepattern("SomeFile.txt").call();
+    git2.commit().setMessage("Update remote branch").call();
+
+    assertTrue(uiGit.pull());
+    assertEquals(
+        "Updated content",
+        FileUtils.readFileToString(
+            new File(db.getWorkTree(), "SomeFile.txt"), 
StandardCharsets.UTF_8));
+
+    git2.close();
+  }
+
+  @Test
+  public void testPullFetchesConfiguredRemote() throws Exception {
+    setupRemote();
+    URIish uri = new URIish(db2.getDirectory().toURI().toURL());
+    git.remoteAdd().setName("upstream").setUri(uri).call();
+    Git git2 = new Git(db2);
+
+    File sourceFile = new File(db2.getWorkTree(), "SomeFile.txt");
+    FileUtils.writeStringToFile(sourceFile, "Initial content", 
StandardCharsets.UTF_8);
+    git2.add().addFilepattern("SomeFile.txt").call();
+    git2.commit().setMessage("Initial commit").call();
+
+    git.fetch().setRemote("upstream").call();
+    git.checkout()
+        .setCreateBranch(true)
+        .setName("local-feature")
+        .setStartPoint("upstream/master")
+        .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
+        .call();
+
+    FileUtils.writeStringToFile(sourceFile, "Updated content", 
StandardCharsets.UTF_8);
+    git2.add().addFilepattern("SomeFile.txt").call();
+    git2.commit().setMessage("Update upstream branch").call();
+
+    assertTrue(uiGit.pull());
+    assertEquals(
+        "Updated content",
+        FileUtils.readFileToString(
+            new File(db.getWorkTree(), "SomeFile.txt"), 
StandardCharsets.UTF_8));
+
+    git2.close();
+  }
+
+  @Test
+  public void 
testPullReportsConfiguredUpstreamMissingWhenFetchRefspecDoesNotMatch()
+      throws Exception {
+    setupRemote();
+    Git git2 = new Git(db2);
+
+    FileUtils.writeStringToFile(
+        new File(db2.getWorkTree(), "SomeFile.txt"), "Initial content", 
StandardCharsets.UTF_8);
+    git2.add().addFilepattern("SomeFile.txt").call();
+    git2.commit().setMessage("Initial commit").call();
+
+    git.fetch().call();
+    git.checkout()
+        .setCreateBranch(true)
+        .setName("local-feature")
+        .setStartPoint("origin/master")
+        .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
+        .call();
+    RefUpdate localTrackingBranch = 
db.updateRef("refs/remotes/origin/local-feature");
+    localTrackingBranch.setNewObjectId(db.resolve(Constants.HEAD));
+    localTrackingBranch.update();
+
+    db.getConfig()
+        .setString(
+            ConfigConstants.CONFIG_REMOTE_SECTION,
+            Constants.DEFAULT_REMOTE_NAME,
+            "fetch",
+            "+refs/heads/master:refs/remotes/origin/master");
+    db.getConfig()
+        .setString(
+            ConfigConstants.CONFIG_BRANCH_SECTION,
+            "local-feature",
+            ConfigConstants.CONFIG_KEY_MERGE,
+            "refs/heads/remote-feature");
+    db.getConfig().save();
+
+    HopException exception = assertThrows(HopException.class, uiGit::pull);
+
+    assertTrue(
+        exception
+            .getMessage()
+            .contains("configured upstream branch 'origin/remote-feature' does 
not exist"));
+    assertNull(exception.getCause());
+
+    git2.close();
+  }
+
+  @Test
+  public void testPullFromLocalUpstreamDoesNotRequireRemote() throws Exception 
{
+    initialCommit();
+    git.checkout().setCreateBranch(true).setName("source").call();
+    writeTrashFile("SomeFile.txt", "Local upstream content");
+    git.add().addFilepattern("SomeFile.txt").call();
+    git.commit().setMessage("Update local upstream").call();
+    git.checkout().setName("master").call();
+
+    db.getConfig()
+        .setString(
+            ConfigConstants.CONFIG_BRANCH_SECTION,
+            "master",
+            ConfigConstants.CONFIG_KEY_REMOTE,
+            ".");
+    db.getConfig()
+        .setString(
+            ConfigConstants.CONFIG_BRANCH_SECTION,
+            "master",
+            ConfigConstants.CONFIG_KEY_MERGE,
+            "refs/heads/source");
+    db.getConfig().save();
+
+    assertTrue(uiGit.pull());
+    assertEquals(
+        "Local upstream content",
+        FileUtils.readFileToString(
+            new File(db.getWorkTree(), "SomeFile.txt"), 
StandardCharsets.UTF_8));
+  }
+
+  @Test
+  public void testPullFromDetachedHeadReportsClearError() throws Exception {
+    setupRemote();
+    initialCommit();
+    git.checkout().setName(db.resolve(Constants.HEAD).name()).call();
+
+    HopException exception = assertThrows(HopException.class, uiGit::pull);
+
+    assertTrue(exception.getMessage().contains("not on a branch"));
+    assertNull(exception.getCause());
+  }
+
+  @Test
+  public void testPullWithoutUpstreamReportsClearError() throws Exception {
+    setupRemote();
+    initialCommit();
+
+    HopException exception = assertThrows(HopException.class, uiGit::pull);
+
+    assertTrue(exception.getMessage().contains("no upstream branch configured 
for 'master'"));
+    assertTrue(exception.getMessage().contains("'origin/master' does not 
exist"));
+    assertNull(exception.getCause());
+  }
+
   @Test
   public void testPullMerge() throws Exception {
     // source: db2, target: db

Reply via email to