Skip to content

Commit

Permalink
Fix #763: Handle GIT_PREFIX by setting GIT_WORK_TREE and changing dir…
Browse files Browse the repository at this point in the history
…ectory
  • Loading branch information
jonas committed May 20, 2018
1 parent 13afba8 commit c9e4ccd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Improvements:
- Build against netbsd-curses. (GH #789)
- Change the blame view to render more like `git blame`. (GH #812)
- Improve worktree and submodule support. (GH #459, #781, #783)
- Support running Tig via a Git alias. (GH #763)

Bug fixes:

Expand Down
25 changes: 25 additions & 0 deletions src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,30 @@ die_if_failed(enum status_code code, const char *msg)
die("%s: %s", msg, get_status_message(code));
}

static inline enum status_code
handle_git_prefix(void)
{
const char *prefix = getenv("GIT_PREFIX");
char cwd[4096];

if (!prefix || !*prefix)
return SUCCESS;

/*
* GIT_PREFIX is set when tig is invoked as a git alias.
* Tig expects to run from the subdirectory so clear the prefix
* and set GIT_WORK_TREE accordinglyt.
*/
if (!getcwd(cwd, sizeof(cwd)))
return error("Failed to read CWD");
if (setenv("GIT_WORK_TREE", cwd, 1))
return error("Failed to set GIT_WORK_TREE");
if (setenv("GIT_PREFIX", "", 1))
return error("Failed to clear GIT_PREFIX");

return chdir(prefix) ? error("Failed to change directory to %s", prefix) : SUCCESS;
}

int
main(int argc, const char *argv[])
{
Expand All @@ -761,6 +785,7 @@ main(int argc, const char *argv[])
codeset = nl_langinfo(CODESET);
}

die_if_failed(handle_git_prefix(), "Failed to handle GIT_PREFIX");
die_if_failed(load_repo_info(), "Failed to load repo info.");
die_if_failed(load_options(), "Failed to load user config.");
die_if_failed(load_git_config(), "Failed to load repo config.");
Expand Down
56 changes: 56 additions & 0 deletions test/main/git-alias-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/sh

. libtest.sh
. libgit.sh

export LINES=5

benchmarks_git="$output_dir/benchmarks.git"
benchmarks_dir="$output_dir/$work_dir"

tigrc <<EOF
set line-graphics = ascii
EOF

steps '
:save-display main.screen
'

executable alias-vars <<EOF
{
echo "PWD=\$(pwd)"
env | grep GIT_
} | sed 's,$output_dir,ROOT,' >> "$output_dir/alias-vars"
EOF

test_setup_work_dir()
{
mkdir -p "$output_dir/base"
cd "$output_dir/base"
create_repo_from_tgz "$base_dir/files/scala-js-benchmarks.tgz"
git branch work-branch ee912870202200a0b9cf4fd86ba57243212d341e
git worktree add "$output_dir/$work_dir" work-branch
git config --global alias.tig !tig
git config --global alias.alias-vars !alias-vars
}

test_exec_work_dir test_setup_work_dir

runner=git

work_dir="$work_dir/deltablue"
test_tig -- src/main/scala
in_work_dir git alias-vars

assert_equals 'alias-vars' <<EOF
PWD=ROOT/work dir
GIT_DIR=ROOT/base/.git/worktrees/work dir
GIT_PREFIX=deltablue/
EOF

assert_equals 'main.screen' <<EOF
2014-03-01 17:26 Jonas Fonseca * [work-branch] [master] WIP: Upgrade to 0.4-SNAP
2014-01-16 22:51 Jonas Fonseca I Move classes under org.scalajs.benchmark packag
[main] ee912870202200a0b9cf4fd86ba57243212d341e - commit 1 of 2 100%
EOF
2 changes: 1 addition & 1 deletion test/tools/libtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ diff_color_arg=
indent=' '
verbose=
debugger=
runner=exec
trace=
todos=
valgrind=
Expand Down Expand Up @@ -599,7 +600,6 @@ test_tig()
"$debugger" tig "$@"
else
set +e
runner=exec
if [ "$expected_status_code" = 0 ] && [ -n "$valgrind" ]; then
runner=valgrind_exec
if [ "$timeout" -gt 0 ]; then
Expand Down

0 comments on commit c9e4ccd

Please sign in to comment.