Skip to content

Commit 3a673d6

Browse files
committed
Merge pull request git-lfs#756 from github/fix-init-update-tests
Improve lfs.InRepo() detection in `init`/`update`
2 parents 4915979 + 7aa9c59 commit 3a673d6

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

commands/command_init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ func initCommand(cmd *cobra.Command, args []string) {
3737
Exit("Run `git lfs init --force` to reset git config.")
3838
}
3939

40-
initHooksCommand(cmd, args)
40+
if localInit || lfs.InRepo() {
41+
initHooksCommand(cmd, args)
42+
}
43+
4144
Print("Git LFS initialized.")
4245
}
4346

commands/command_update.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var (
2020
// updateCommand is used for updating parts of Git LFS that reside under
2121
// .git/lfs.
2222
func updateCommand(cmd *cobra.Command, args []string) {
23+
requireInRepo()
24+
2325
if err := lfs.InstallHooks(updateForce); err != nil {
2426
Error(err.Error())
2527
Print("Run `git lfs update --force` to overwrite this hook.")

lfs/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Environ() []string {
9393
}
9494

9595
func InRepo() bool {
96-
return LocalWorkingDir != ""
96+
return LocalGitDir != ""
9797
}
9898

9999
func ResolveDirs() {

test/test-init.sh

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ Git LFS initialized."
8585
Git LFS initialized." = "$(git lfs init --force)" ]
8686
[ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
8787

88-
# TODO: FIX FOR DOCKER TESTS
89-
exit 0
88+
[ -n "$LFS_DOCKER" ] && exit 0
9089

9190
echo "test with bare repository"
9291
cd ..
@@ -99,6 +98,30 @@ Git LFS initialized." = "$(git lfs init --force)" ]
9998
)
10099
end_test
101100

101+
begin_test "init outside repository directory"
102+
(
103+
set -e
104+
if [ -d "hooks" ]; then
105+
ls -al
106+
echo "hooks dir exists"
107+
exit 1
108+
fi
109+
110+
git lfs init 2>&1 > check.log
111+
112+
if [ -d "hooks" ]; then
113+
ls -al
114+
echo "hooks dir exists"
115+
exit 1
116+
fi
117+
118+
cat check.log
119+
120+
# doesn't print this because being in a git repo is not necessary for init
121+
[ "$(grep -c "Not in a git repository" check.log)" = "0" ]
122+
)
123+
end_test
124+
102125
begin_test "init --skip-smudge"
103126
(
104127
set -e
@@ -140,8 +163,7 @@ begin_test "init --local outside repository"
140163
(
141164
set +e
142165

143-
# TODO: FIX FOR DOCKER TESTS
144-
exit 0
166+
[ -n "$LFS_DOCKER" ] && exit 0
145167

146168
git lfs init --local 2> err.log
147169
res=$?

test/test-update.sh

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ Run \`git lfs update --force\` to overwrite this hook."
6868
[ "Updated pre-push hook." = "$(git lfs update --force)" ]
6969
[ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
7070

71-
# TODO: FIX FOR DOCKER TESTS
72-
exit 0
71+
[ -n "$LFS_DOCKER" ] && exit 0
7372

7473
echo "test with bare repository"
7574
cd ..
@@ -108,18 +107,33 @@ end_test
108107

109108
begin_test "update: outside git repository"
110109
(
110+
if [ -d "hooks" ]; then
111+
ls -al
112+
echo "hooks dir exists"
113+
exit 1
114+
fi
115+
111116
set +e
112117
git lfs update 2>&1 > check.log
113118
res=$?
114-
overwrite="$(grep "overwrite" check.log)"
115-
116119
set -e
120+
117121
if [ "$res" = "0" ]; then
118-
echo "Passes because $GIT_LFS_TEST_DIR is unset."
119-
exit 0
122+
if [ -z "$GIT_LFS_TEST_DIR" ]; then
123+
echo "Passes because $GIT_LFS_TEST_DIR is unset."
124+
exit 0
125+
fi
120126
fi
127+
121128
[ "$res" = "128" ]
122-
[ -z "$overwrite" ]
129+
130+
if [ -d "hooks" ]; then
131+
ls -al
132+
echo "hooks dir exists"
133+
exit 1
134+
fi
135+
136+
cat check.log
123137
grep "Not in a git repository" check.log
124138
)
125139
end_test

0 commit comments

Comments
 (0)