-
Notifications
You must be signed in to change notification settings - Fork 37
/
undo.go
63 lines (51 loc) · 1.23 KB
/
undo.go
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
package main
import (
"github.com/nu7hatch/gouuid"
"os"
)
func handleUndoCommand(opts *CommandLineOptions, args []string) error {
filePaths, err := filePathsFromArgs(args, true)
if err != nil {
return err
}
for i, p := range filePaths {
filePaths[i] = normalizePath(p)
}
items, err := latestHistoryItemsByDestinations(filePaths)
if err != nil {
return err
}
var conflictItems []HistoryItem
for _, item := range items {
if opts.DryRun {
logInfo("\"%s\" => \"%s\"", item.Dest, item.Source)
} else {
logDebug("\"%s\" => \"%s\"", item.Dest, item.Source)
if _, err := os.Stat(item.Source); os.IsNotExist(err) {
err = os.Rename(item.Dest, item.Source)
if err != nil {
return err
}
} else {
u, _ := uuid.NewV4()
item.IntermediatePath = item.Source + "-" + u.String()
conflictItems = append(conflictItems, item)
}
}
}
// See conflict resolution in main::processFileActions()
for _, item := range conflictItems {
err := os.Rename(item.Dest, item.IntermediatePath)
if err != nil {
return err
}
}
for _, item := range conflictItems {
err := os.Rename(item.IntermediatePath, item.Source)
if err != nil {
return err
}
}
deleteHistoryItems(items)
return nil
}