package org.ajoberstar.gradle.git.publish.tasks;

import java.io.File;
import java.util.Arrays;

import javax.inject.Inject;

import org.ajoberstar.grgit.gradle.GrgitService;
import org.gradle.api.DefaultTask;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;

@UntrackedTask(because = "Git tracks the state")
public class GitPublishPush extends DefaultTask {
  private final Property<GrgitService> grgitService;
  private final Property<String> branch;

  @Inject
  public GitPublishPush(ObjectFactory objectFactory) {
    this.grgitService = objectFactory.property(GrgitService.class);
    this.branch = objectFactory.property(String.class);

    this.onlyIf(t -> {
      try {
        var git = getGrgitService().get().getGrgit();
        var status = git.getBranch().status(op -> {
          op.setName(getBranch().get());
        });
        return status.getAheadCount() > 0;
      } catch (IllegalStateException e) {
        // if we're not tracking anything yet (i.e. orphan) we need to push
        return true;
      }
    });
  }

  @Internal
  public Property<GrgitService> getGrgitService() {
    return grgitService;
  }

  @Input
  public Property<String> getBranch() {
    return branch;
  }

  @TaskAction
  public void push() {
    var git = getGrgitService().get().getGrgit();
    var pubBranch = getBranch().get();
    git.push(op -> {
      op.setRefsOrSpecs(Arrays.asList(String.format("refs/heads/%s:refs/heads/%s", pubBranch, pubBranch)));
    });
  }
}

@@ -168,16 +168,70 @@
 ervice;%0A
+import org.ajoberstar.grgit.operation.BranchChangeOp;%0A
 import o
@@ -1579,19 +1579,214 @@
 )));%0A    %7D);
+%0A%0A    // ensure tracking is set%0A    git.getBranch().change(op -%3E %7B%0A      op.setName(pubBranch);%0A      op.setStartPoint(%22origin/%22 + pubBranch);%0A      op.setMode(BranchChangeOp.Mode.TRACK);%0A    %7D);
 %0A  %7D%0A%7D%0A

Fix tracking of orphan branches