aboutsummaryrefslogtreecommitdiff
path: root/third_party/sanitize_patch_dates.go
blob: 712fb7f4395f3ee6d69c72fa0fc216d3c581eea5 (plain)
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
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"regexp"
	"strings"
)

func main() {
	log.SetFlags(0)
	log.SetPrefix("sanitize_patch_dates: ")
	if len(os.Args) == 1 {
		log.Fatalf("usage: sanitize_patch_dates *.patch")
	}
	for _, arg := range os.Args[1:] {
		if err := sanitize(arg); err != nil {
			log.Fatal(err)
		}
	}
}

var dateRegexp = regexp.MustCompile("20..-..-.. .*")

func sanitize(filename string) (err error) {
	r, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer r.Close()

	tempFilename := filename + "~"
	w, err := os.Create(tempFilename)
	if err != nil {
		return err
	}
	defer func() {
		if w == nil {
			return
		}
		if cerr := w.Close(); err == nil && cerr != nil {
			err = cerr
		}
	}()

	s := bufio.NewScanner(r)
	for s.Scan() {
		line := s.Text()
		if strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---") {
			line = dateRegexp.ReplaceAllLiteralString(line, "2000-01-01 00:00:00.000000000 -0000")
		}
		if _, err := fmt.Fprintln(w, line); err != nil {
			return err
		}
	}
	if err := s.Err(); err != nil {
		return err
	}

	if err := w.Close(); err != nil {
		return err
	}
	w = nil
	if err := os.Rename(tempFilename, filename); err != nil {
		return err
	}
	return nil
}