@saeed42/worktree-worker 1.6.2 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/main.js +133 -0
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -1828,6 +1828,139 @@ git.get("/commits", async (c) => {
1828
1828
  );
1829
1829
  }
1830
1830
  });
1831
+ var pushSchema2 = z3.object({
1832
+ cwd: z3.string().min(1, "Working directory is required"),
1833
+ branch: z3.string().min(1, "Branch is required"),
1834
+ githubToken: z3.string().min(1, "GitHub token is required"),
1835
+ upToCommit: z3.string().optional()
1836
+ });
1837
+ git.post("/push", async (c) => {
1838
+ const log = logger.child({ route: "git/push" });
1839
+ try {
1840
+ const body = await c.req.json().catch(() => ({}));
1841
+ const parsed = pushSchema2.safeParse(body);
1842
+ if (!parsed.success) {
1843
+ return c.json(
1844
+ { success: false, error: { code: "VALIDATION_ERROR", message: parsed.error.message } },
1845
+ 400
1846
+ );
1847
+ }
1848
+ const { cwd, branch, githubToken, upToCommit } = parsed.data;
1849
+ log.info("Pushing to remote", { branch, hasUpToCommit: !!upToCommit });
1850
+ await gitService.exec([
1851
+ "config",
1852
+ "--global",
1853
+ "credential.helper",
1854
+ "store --file=/tmp/.git-credentials"
1855
+ ]);
1856
+ await gitService.exec(["config", "--global", "core.askPass", ""]);
1857
+ const credLine = `https://x-access-token:${githubToken}@github.com
1858
+ `;
1859
+ await fs.writeFile("/tmp/.git-credentials", credLine, { mode: 384 });
1860
+ try {
1861
+ let pushArgs;
1862
+ if (upToCommit) {
1863
+ pushArgs = ["push", "origin", `${upToCommit}:${branch}`];
1864
+ } else {
1865
+ pushArgs = ["push", "origin", branch];
1866
+ }
1867
+ log.debug("Executing git push", { args: pushArgs });
1868
+ const result = await gitService.exec(pushArgs, cwd);
1869
+ if (result.code !== 0) {
1870
+ const output = result.stderr || result.stdout || "";
1871
+ log.error("Git push failed", { stderr: output, code: result.code });
1872
+ if (output.includes("rejected") || output.includes("non-fast-forward")) {
1873
+ return c.json(
1874
+ { success: false, error: { code: "REJECTED", message: "Push rejected: Remote has changes. Please pull first." } },
1875
+ 409
1876
+ );
1877
+ }
1878
+ if (output.includes("Permission denied") || output.includes("authentication") || output.includes("fatal: Authentication")) {
1879
+ return c.json(
1880
+ { success: false, error: { code: "AUTH_FAILED", message: "GitHub authentication failed." } },
1881
+ 403
1882
+ );
1883
+ }
1884
+ return c.json(
1885
+ { success: false, error: { code: "PUSH_FAILED", message: output || "Git push failed" } },
1886
+ 500
1887
+ );
1888
+ }
1889
+ log.info("Git push successful", { branch });
1890
+ return c.json({
1891
+ success: true,
1892
+ data: {
1893
+ branch,
1894
+ message: "Push successful"
1895
+ }
1896
+ });
1897
+ } finally {
1898
+ try {
1899
+ await fs.unlink("/tmp/.git-credentials");
1900
+ } catch {
1901
+ }
1902
+ }
1903
+ } catch (err) {
1904
+ const error = err;
1905
+ log.error("Failed to push", { error: error.message });
1906
+ return c.json(
1907
+ { success: false, error: { code: "INTERNAL_ERROR", message: error.message } },
1908
+ 500
1909
+ );
1910
+ }
1911
+ });
1912
+ var discardSchema = z3.object({
1913
+ cwd: z3.string().min(1, "Working directory is required"),
1914
+ baseBranch: z3.string().min(1, "Base branch is required"),
1915
+ commitSha: z3.string().optional()
1916
+ // If provided, reset to before this commit
1917
+ });
1918
+ git.post("/discard", async (c) => {
1919
+ const log = logger.child({ route: "git/discard" });
1920
+ try {
1921
+ const body = await c.req.json().catch(() => ({}));
1922
+ const parsed = discardSchema.safeParse(body);
1923
+ if (!parsed.success) {
1924
+ return c.json(
1925
+ { success: false, error: { code: "VALIDATION_ERROR", message: parsed.error.message } },
1926
+ 400
1927
+ );
1928
+ }
1929
+ const { cwd, baseBranch, commitSha } = parsed.data;
1930
+ let resetTarget;
1931
+ if (commitSha) {
1932
+ resetTarget = `${commitSha}^`;
1933
+ log.info("Reverting commit", { commitSha });
1934
+ } else {
1935
+ resetTarget = `origin/${baseBranch}`;
1936
+ log.info("Discarding all local commits", { baseBranch });
1937
+ await gitService.exec(["fetch", "origin", baseBranch], cwd);
1938
+ }
1939
+ const result = await gitService.exec(["reset", "--hard", resetTarget], cwd);
1940
+ if (result.code !== 0) {
1941
+ log.error("Git reset failed", { stderr: result.stderr });
1942
+ return c.json(
1943
+ { success: false, error: { code: "RESET_FAILED", message: result.stderr || "Failed to reset" } },
1944
+ 500
1945
+ );
1946
+ }
1947
+ log.info("Discard successful", { resetTarget });
1948
+ return c.json({
1949
+ success: true,
1950
+ data: {
1951
+ resetTarget,
1952
+ message: commitSha ? "Commit reverted" : "All local commits discarded"
1953
+ }
1954
+ });
1955
+ } catch (err) {
1956
+ const error = err;
1957
+ log.error("Failed to discard", { error: error.message });
1958
+ return c.json(
1959
+ { success: false, error: { code: "INTERNAL_ERROR", message: error.message } },
1960
+ 500
1961
+ );
1962
+ }
1963
+ });
1831
1964
 
1832
1965
  // src/main.ts
1833
1966
  var app = new Hono5();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saeed42/worktree-worker",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "description": "Git worktree management service for AI agent trials",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",