summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/optional/perforce/P4Labelsync.java
blob: 27a43fafa125476d8ee680e97d7024ca6d071b9b (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.
 *
 */
/*
 * Portions of this software are based upon public domain software
 * originally written at the National Center for Supercomputing Applications,
 * University of Illinois, Urbana-Champaign.
 */

package org.apache.tools.ant.taskdefs.optional.perforce;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.StringUtils;

/**
 *  This method syncs an existing Perforce label against the Perforce client
 *  or against a set of files/revisions.
 *
 *
 * Example Usage:
 * <pre>
 *   &lt;p4labelsync name="MyLabel-${TSTAMP}-${DSTAMP}"
 *   view="//depot/...#head;//depot2/file1#25" /&gt;
 * </pre>
 *
 * @ant.task category="scm"
 */
public class P4Labelsync extends P4Base {

    protected String name;
    private boolean add; /* -a */
    private boolean delete; /* -n */
    private boolean simulationmode;  /* -n */
    /**
     * -a flag of p4 labelsync - preserve files which exist in the label,
     * but not in the current view
     * @return  add attribute
     * if set to true the task will not remove any files from the label
     * only add files which were not there previously or update these where the revision has changed
     * the add attribute is the -a flag of p4 labelsync
     */
    public boolean isAdd() {
        return add;
    }
    /**
     * -a flag of p4 labelsync - preserve files which exist in the label,
     * but not in the current view
     * @param add  if set to true the task will not remove any files from the label
     * only add files which were not there previously or update these where the revision has changed
     * the add attribute is the -a flag of p4 labelsync
     */
    public void setAdd(boolean add) {
        this.add = add;
    }
    /**
     * -d flag of p4 labelsync; indicates an intention of deleting from the label
     * the files specified in the view
     * @return  delete attribute
     */
    public boolean isDelete() {
        return delete;
    }

    /**
     * -d flag of p4 labelsync; indicates an intention of deleting from the label
     *  the files specified in the view
     * @param delete indicates intention of deleting from the label
     * the files specified in the view
     */
    public void setDelete(boolean delete) {
        this.delete = delete;
    }


    /**
     * The name of the label; optional, default "AntLabel"
     * @param name of the label
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * -n flag of p4 labelsync - display changes without actually doing them
     * @return -n flag of p4 labelsync
     */
    public boolean isSimulationmode() {
        return simulationmode;
    }
    /**
     * -n flag of p4 labelsync - display changes without actually doing them
     * @param simulationmode display changes without actually doing them
     */
    public void setSimulationmode(boolean simulationmode) {
        this.simulationmode = simulationmode;
    }


    /**
     *  do the work
     * @throws BuildException if the label name is not supplied
     */
    public void execute() throws BuildException {
        log("P4Labelsync exec:", Project.MSG_INFO);

        if (P4View != null && P4View.length() >= 1) {
            P4View = StringUtils.replace(P4View, ":", "\n\t");
            P4View = StringUtils.replace(P4View, ";", "\n\t");
        }
        if (P4View == null) {
            P4View = "";
        }

        if (name == null || name.length() < 1) {
            throw new BuildException("name attribute is compulsory for labelsync");
        }

        if (this.isSimulationmode()) {
            P4CmdOpts = P4CmdOpts + " -n";
        }
        if (this.isDelete()) {
            P4CmdOpts = P4CmdOpts + " -d";
        }
        if (this.isAdd()) {
            P4CmdOpts = P4CmdOpts + " -a";
        }

        execP4Command("-s labelsync -l " + name + " " + P4CmdOpts + " " + P4View,
            new SimpleP4OutputHandler(this));


    }
}