plandex-ai/plandex · error

error adding files to git repository for dir: %s, err: %v

Error message

error adding files to git repository for dir: %s, err: %v

What it means

After writing original files, GetPlanDiffs stages them with gitAdd(tempDirPath, "."). If the `git add` exec fails (non-zero exit), the error is wrapped with the temp dir path for context. Without this commit, the subsequent diff between 'original files' and current files cannot be produced.

Source

Thrown at app/server/db/diff_helpers.go:91

					return
				}
			}
			errCh <- nil
		}(path, context)
	}

	for range planState.ContextsByPath {
		err = <-errCh
		if err != nil {
			return "", fmt.Errorf("error writing original files to temp dir: %v", err)
		}
	}

	if hasAnyOriginal {
		// add and commit the files in the temp dir
		err := gitAdd(tempDirPath, ".")
		if err != nil {
			return "", fmt.Errorf("error adding files to git repository for dir: %s, err: %v", tempDirPath, err)
		}

		err = gitCommit(tempDirPath, "original files")
		if err != nil {
			return "", fmt.Errorf("error committing files to git repository for dir: %s, err: %v", tempDirPath, err)
		}
	}

	// write the current files to the temp dir
	errCh = make(chan error, len(files))

	for path, file := range files {
		go func(path, file string) {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
					errCh <- fmt.Errorf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
					runtime.Goexit() // don't allow outer function to continue and double-send to channel

View on GitHub (pinned to e2d772072e)

Solutions

  1. Run `git -C <tempDir> add .` manually to reproduce and read git's stderr.
  2. If 'dubious ownership' appears, run as the repo owner or set safe.directory appropriately for the service user.
  3. Remove any stale .git/index.lock in the affected directory after confirming no git process is running.
  4. Verify git is installed and on PATH for the server process user.

Example fix

// before
err := gitAdd(tempDirPath, ".")
// after (retry-safe: clear stale lock first)
os.Remove(filepath.Join(tempDirPath, ".git", "index.lock"))
err := gitAdd(tempDirPath, ".")
Defensive patterns

Strategy: retry

Validate before calling

// pre-check repo is functional before add
if out, err := exec.Command("git", "-C", tempDirPath, "status", "--porcelain").CombinedOutput(); err != nil {
    return fmt.Errorf("temp repo unusable: %v, output: %s", err, out)
}

Try / catch

out, err := GetPlanDiffs(orgId, planId, plain)
if err != nil && strings.Contains(err.Error(), "error adding files to git repository") {
    lock := filepath.Join(tempDirPath, ".git", "index.lock")
    if _, statErr := os.Stat(lock); statErr == nil {
        os.Remove(lock) // stale lock from a killed process
        // retry the operation
    }
}

Prevention

When it happens

Trigger: gitAdd runs `git add .` in the temp repo and it fails: not a git repo (init failed silently), git missing/broken, index.lock present, or a file violates git rules (e.g. path considered unsafe, too-large file, CRLF/ownership 'dubious ownership' safety error).

Common situations: 'dubious ownership' errors when the repo dir is owned by another user (common in containers); stale index.lock from a killed process; git not installed; plan file paths flagged by git's safe.directory/protect checks.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/d986d3b90e5f14d1. Report an issue: GitHub.