View Javadoc
1   /**
2    * Copyright (c) 2012-2014, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.heroku.maven.plugin;
31  
32  import com.jcabi.aspects.Immutable;
33  import com.jcabi.log.Logger;
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.Date;
37  import javax.validation.constraints.NotNull;
38  import lombok.EqualsAndHashCode;
39  import lombok.ToString;
40  import org.apache.commons.io.FileUtils;
41  import org.apache.commons.lang3.CharEncoding;
42  
43  /**
44   * Local Git repository.
45   *
46   * @author Yegor Bugayenko (yegor@tpc2.com)
47   * @version $Id$
48   * @since 0.4
49   */
50  @Immutable
51  @ToString
52  @EqualsAndHashCode(of = { "git", "path" })
53  final class Repo {
54  
55      /**
56       * Git engine.
57       */
58      private final transient Git git;
59  
60      /**
61       * Location of repository.
62       */
63      private final transient String path;
64  
65      /**
66       * Public ctor.
67       * @param engine Git engine
68       * @param file Location of repository
69       */
70      public Repo(@NotNull final Git engine, @NotNull final File file) {
71          this.git = engine;
72          this.path = file.getAbsolutePath();
73      }
74  
75      /**
76       * Add new file.
77       * @param name Name of it
78       * @param content Content of the file to write (overwrite)
79       * @throws IOException If fails
80       */
81      public void add(@NotNull final String name, @NotNull final String content)
82          throws IOException {
83          final File dir = new File(this.path);
84          final File file = new File(dir, name);
85          FileUtils.writeStringToFile(file, content, CharEncoding.UTF_8);
86          this.git.exec(dir, "add", name);
87          Logger.info(
88              this,
89              "File %s updated, %[size]s",
90              file,
91              file.length()
92          );
93      }
94  
95      /**
96       * Commit changes and push.
97       */
98      public void commit() {
99          final File dir = new File(this.path);
100         this.git.exec(dir, "status");
101         this.git.exec(
102             dir,
103             "commit",
104             "-am",
105             new Date().toString()
106         );
107         this.git.exec(
108             dir,
109             "push",
110             "origin",
111             "master"
112         );
113         Logger.info(this, "Repository commited to Heroku");
114     }
115 
116 }