Skip to content

Commit fa137f6

Browse files
pcloudsgitster
authored andcommitted
lockfile.c: store absolute path
Locked paths can be saved in a linked list so that if something wrong happens, *.lock are removed. For relative paths, this works fine if we keep cwd the same, which is true 99% of time except: - update-index and read-tree hold the lock on $GIT_DIR/index really early, then later on may call setup_work_tree() to move cwd. - Suppose a lock is being held (e.g. by "git add") then somewhere down the line, somebody calls real_path (e.g. "link_alt_odb_entry"), which temporarily moves cwd away and back. During that time when cwd is moved (either permanently or temporarily) and we decide to die(), attempts to remove relative *.lock will fail, and the next operation will complain that some files are still locked. Avoid this case by turning relative paths to absolute before storing the path in "filename" field. Reported-by: Yue Lin Ho <[email protected]> Helped-by: Ramsay Jones <[email protected]> Helped-by: Johannes Sixt <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Adapted-by: Michael Haggerty <[email protected]> Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4ace7ff commit fa137f6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lockfile.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,17 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
128128
path);
129129
}
130130

131-
strbuf_add(&lk->filename, path, pathlen);
132-
if (!(flags & LOCK_NO_DEREF))
133-
resolve_symlink(&lk->filename);
131+
if (flags & LOCK_NO_DEREF) {
132+
strbuf_add_absolute_path(&lk->filename, path);
133+
} else {
134+
struct strbuf resolved_path = STRBUF_INIT;
135+
136+
strbuf_add(&resolved_path, path, pathlen);
137+
resolve_symlink(&resolved_path);
138+
strbuf_add_absolute_path(&lk->filename, resolved_path.buf);
139+
strbuf_release(&resolved_path);
140+
}
141+
134142
strbuf_addstr(&lk->filename, LOCK_SUFFIX);
135143
lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
136144
if (lk->fd < 0) {

t/t2107-update-index-basic.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,19 @@ test_expect_success '--cacheinfo mode,sha1,path (new syntax)' '
6565
test_cmp expect actual
6666
'
6767

68+
test_expect_success '.lock files cleaned up' '
69+
mkdir cleanup &&
70+
(
71+
cd cleanup &&
72+
mkdir worktree &&
73+
git init repo &&
74+
cd repo &&
75+
git config core.worktree ../../worktree &&
76+
# --refresh triggers late setup_work_tree,
77+
# active_cache_changed is zero, rollback_lock_file fails
78+
git update-index --refresh &&
79+
! test -f .git/index.lock
80+
)
81+
'
82+
6883
test_done

0 commit comments

Comments
 (0)