File: Unannex.hs

package info (click to toggle)
git-annex 10.20230126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 69,344 kB
  • sloc: haskell: 74,654; javascript: 9,103; sh: 1,304; makefile: 203; perl: 136; ansic: 44
file content (101 lines) | stat: -rw-r--r-- 2,838 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{- git-annex command
 -
 - Copyright 2010-2021 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Command.Unannex where

import Command
import qualified Annex
import Annex.Perms
import Annex.Link
import qualified Annex.Queue
import Utility.CopyFile
import qualified Database.Keys
import Utility.InodeCache
import Annex.InodeSentinal
import Git.FilePath
import qualified Utility.RawFilePath as R

cmd :: Command
cmd = withAnnexOptions [annexedMatchingOptions] $
	command "unannex" SectionUtility
		"undo accidental add command"
		paramPaths (withParams seek)

seek :: CmdParams -> CommandSeek
seek ps = withFilesInGitAnnex ww (seeker False) =<< workTreeItems ww ps
  where
	ww = WarnUnmatchLsFiles

seeker :: Bool -> AnnexedFileSeeker
seeker fast = AnnexedFileSeeker
	{ startAction = start fast
	, checkContentPresent = Just True
	, usesLocationLog = False
	}

start :: Bool -> SeekInput -> RawFilePath -> Key -> CommandStart
start fast si file key = 
	starting "unannex" (mkActionItem (key, file)) si $
		perform fast file key

perform :: Bool -> RawFilePath -> Key -> CommandPerform
perform fast file key = do
	Annex.Queue.addCommand [] "rm"
		[ Param "--cached"
		, Param "--force"
		, Param "--quiet"
		, Param "--"
		]
		[fromRawFilePath file]
	isAnnexLink file >>= \case
		-- If the file is locked, it needs to be replaced with
		-- the content from the annex. Note that it's possible
		-- for key' (read from the symlink) to differ from key
		-- (cached in git).
		Just key' -> do
			cleanupdb
			next $ cleanup fast file key'
		-- If the file is unlocked, it can be unmodified or not and
		-- does not need to be replaced either way.
		Nothing -> do
			cleanupdb
			next $ return True
  where
	cleanupdb = do
		Database.Keys.removeAssociatedFile key
			=<< inRepo (toTopFilePath file)
		maybe noop Database.Keys.removeInodeCache
			=<< withTSDelta (liftIO . genInodeCache file)

cleanup :: Bool -> RawFilePath -> Key -> CommandCleanup
cleanup fast file key = do
	liftIO $ removeFile (fromRawFilePath file)
	src <- calcRepo (gitAnnexLocation key)
	ifM (pure fast <||> Annex.getRead Annex.fast)
		( do
			-- Only make a hard link if the annexed file does not
			-- already have other hard links pointing at it. This
			-- avoids unannexing (and uninit) ending up hard
			-- linking files together, which would be surprising.
			s <- liftIO $ R.getFileStatus src
			if linkCount s > 1
				then copyfrom src
				else hardlinkfrom src
		, copyfrom src
		)
  where
	copyfrom src = 
		thawContent file `after` liftIO 
			(copyFileExternal CopyAllMetaData
				(fromRawFilePath src)
				(fromRawFilePath file))
	hardlinkfrom src =
		-- creating a hard link could fall; fall back to copying
		ifM (liftIO $ catchBoolIO $ R.createLink src file >> return True)
			( return True
			, copyfrom src
			)