Subject: Re: [PATCH v6 3/3] git-gui: handle GIT_DIR and GIT_WORK_TREE early

From: Mark Levedahl <[email-masked]>
Date: Thu, 7 May 2026 11:50:32 -0400

On 5/6/26 4:27 PM, Shroom Moo wrote:
> +proc resolve_initial_environment {} {
> +	global _gitdir env
> +
> +	# Only act if both GIT_DIR and GIT_WORK_TREE are set
> +	if {[info exists env(GIT_DIR)] && [info exists env(GIT_WORK_TREE)]} {
> +		# Validate GIT_DIR by resolving its absolute path
> +		if {[catch {set _abs [git rev-parse --absolute-git-dir]} err]} {
> +			catch {wm withdraw .}
> +			error_popup [strcat [mc "Invalid GIT_DIR:"] "\n\n$err"]
> +			exit 1
> +		}
> +
> +		# Change current directory to GIT_WORK_TREE
> +		if {[catch {cd $env(GIT_WORK_TREE)} err]} {
> +			catch {wm withdraw .}
> +			error_popup [strcat [mc "Cannot change to GIT_WORK_TREE:"] "\n\n$err"]
> +			exit 1
> +		}
GIT_DIR could be relative to the startup directory. The absolute variant should be
exported before the next step.

> +
> +		# Verify that GIT_WORK_TREE is a valid Git worktree
> +		if {[catch {git rev-parse --show-toplevel} err]} {
> +			catch {wm withdraw .}
> +			error_popup [strcat [mc "GIT_WORK_TREE is not a valid worktree:"] "\n\n$err"]
> +			exit 1
> +		}
> +
> +		# Use repository path specified by environment variables
> +		set _gitdir $_abs
> +		set ::_prefix {}
> +
> +		# Unset GIT_WORK_TREE to prevent it from being inherited by child processes
> +		unset env(GIT_WORK_TREE)
> +

There are many places in the code that have pairs of set/unset env(GIT_DIR) &
env(GIT_WORK_TREE).
Now that we have cases where $_gitworktree == {} and we don't want that exported, and
these are all unguarded.

Perhaps we need to create set/unset procs that do the right thing (setx assures
GIT_WORK_TREE is not in the env when $_gitworktree == {}), unsetx does not thow and error
when unset env(GIT_WORK_TREE) fails as the value doesn't exist, and invoke those common
procs to instead.

Mark