Merge branch 'es/worktree-add-post-checkout-hook' into next
[git] / contrib / persistent-https / main.go
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // The git-remote-persistent-https binary speeds up SSL operations by running
16 // a daemon job that keeps a connection open to a Git server. This ensures the
17 // git-remote-persistent-https--proxy is running and delegating execution
18 // to the git-remote-http binary with the http_proxy set to the daemon job.
19 // A unix socket is used to authenticate the proxy and discover the
20 // HTTP address. Note, both the client and proxy are included in the same
21 // binary.
22 package main
23
24 import (
25         "flag"
26         "fmt"
27         "log"
28         "os"
29         "strings"
30         "time"
31 )
32
33 var (
34         forceProxy = flag.Bool("proxy", false, "Whether to start the binary in proxy mode")
35         proxyBin   = flag.String("proxy_bin", "git-remote-persistent-https--proxy", "Path to the proxy binary")
36         printLabel = flag.Bool("print_label", false, "Prints the build label for the binary")
37
38         // Variable that should be defined through the -X linker flag.
39         _BUILD_EMBED_LABEL string
40 )
41
42 const (
43         defaultMaxIdleDuration    = 24 * time.Hour
44         defaultPollUpdateInterval = 15 * time.Minute
45 )
46
47 func main() {
48         flag.Parse()
49         if *printLabel {
50                 // Short circuit execution to print the build label
51                 fmt.Println(buildLabel())
52                 return
53         }
54
55         var err error
56         if *forceProxy || strings.HasSuffix(os.Args[0], "--proxy") {
57                 log.SetPrefix("git-remote-persistent-https--proxy: ")
58                 proxy := &Proxy{
59                         BuildLabel:         buildLabel(),
60                         MaxIdleDuration:    defaultMaxIdleDuration,
61                         PollUpdateInterval: defaultPollUpdateInterval,
62                 }
63                 err = proxy.Run()
64         } else {
65                 log.SetPrefix("git-remote-persistent-https: ")
66                 client := &Client{
67                         ProxyBin: *proxyBin,
68                         Args:     flag.Args(),
69                 }
70                 err = client.Run()
71         }
72         if err != nil {
73                 log.Fatalln(err)
74         }
75 }
76
77 func buildLabel() string {
78         if _BUILD_EMBED_LABEL == "" {
79                 log.Println(`unlabeled build; build with "make" to label`)
80         }
81         return _BUILD_EMBED_LABEL
82 }